以下のコードにて自身のフォルダ(Test)に保存された test.img のファイル名や拡張子などを取得しSQLに保存するにはどうすればよいのでしょうか
PHP
1 try { 2 $dbh = new PDO('mysql:host=localhost;dbname=imagedb', 'root', ''); 3 $sql = "INSERT INTO images ( 4 id, title, description, userId,extention 5 ) VALUES ( 6 '1', 7 '2', 8 '3', 9 '4', 10 '5' 11 )"; 12 $res = $dbh->query($sql); 13 } catch(PDOException $e) { 14 echo $e->getMessage(); 15 die(); 16 } 17 $dbh = null;
//PHP 全文 <!DOCTYPE html> <html lang ="en"> <body> <form enctype="multipart/form-data" method="POST" action="index.php"> <input type ="file" name ="userfile"> <input type = "submit" value="upload"> <form action = "kensaku,php" method="post"> タイトル:<input type="text" name="nm"> <input type="submit" name="exec" value="検索"> </form> <?php if(isset($_FILES["userfile"]) and $_FILES["userfile"]["error"]==0){ $uploadDir = './userid/'; $uploadPath = $uploadDir . $_FILES['userfile']['name']; if(!isset($_POST['submit'])){ if(move_uploaded_file( $_FILES['userfile']['tmp_name'], $uploadPath )){ echo '成功です'; echo $uploadPath = __FILE__; echo $title = basename($uploadPath); try { $dbh = new PDO('mysql:host=localhost;dbname=imagedb', 'root', ''); $sql = "INSERT INTO images ( id, title, description, userId,extention ) VALUES ( '$uploadPath', '$title', 'description', 'userId', 'extention' )"; $res = $dbh->query($sql); } catch(PDOException $e) { echo $e->getMessage(); die(); } $dbh = null; }else{ echo '失敗です'; } } } echo "<br />\n"; $dir_path = './userid/'; if (is_dir($dir_path)){ if(is_readable($dir_path)){ $ch_dir = dir($dir_path); while (false !== ($file_name = $ch_dir -> read())){ $ln_path = $ch_dir -> path . "/" .$file_name; if (@getimagesize($ln_path)){ echo "<a href = \"imgview.php?d=" .urlencode(mb_convert_encoding($ln_path, "UTF-8")). "\" target = \"_blank\" >"; echo "<img src = \"" .$ln_path. "\" width=\"100\"></a> "; }} $ch_dir -> close();} else{ echo "<p>" .htmlspecialchars($dir_path)." は読み込みが許可されていません。";}} else{ echo 'DIR 画像がないよ';} ?> </body> </html>