お世話になっております。phpの初心者です。
htmlのフォームで画像を選択して、「送信」ボタンを押して、mysqlへのINSERTを実行する
phpファイルに、フォームで選択した画像データを渡したいのですがうまくいかずに困って
おります。
まず、画像を選択するフォームがあるhtmlファイルのソースを示します。
form_pic.html
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" lang="ja" xml:lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> <meta http-equiv="Content-Style-Type" content="text/css" /> <title></title> </head> <body> <form enctype="multipart/form-data" action="insert2.php" method="POST"> <input type="hidden" name="MAX_FILE_SIZE" value="300000" /> <input type="file" name="picture" /><input type="submit" name="send" /><input type="reset" name="reset" />
</form> </body> </html>次に、フォームで選択した画像データをわたさないで、単体で画像データをmysqlへ
INSERTするphpファイルのソースを示します。(このphpファイルを改造してフォーム
からの画像データを受け取れるようにしたいのです)
insert.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="ja" xml:lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <title></title> </head> <body></body> </html><?php // 画像と拡張子を取得 $img_path = 'C:/Program Files/Apache Software Foundation/Apache2.2\htdocs/img/ApacheMonitor.jpg'; $img = file_get_contents($img_path); $ext = pathinfo($img_path, PATHINFO_EXTENSION); // データベースに保存 $pdo = new PDO('mysql:host=localhost; dbname=tada', 'root', 'pass'); $stmt = $pdo->prepare('INSERT INTO images VALUES(0, :ext, :img)'); $stmt->bindParam(':ext', $ext); $stmt->bindParam(':img', $img); $stmt->execute(); ?>
↑上記のinsert.phpではうまく「ApacheMonitor.jpg」がmysqlへ保存されます。
で、このinsert.phpを改造してinsert2.phpを作成しました。
insert2.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html lang="ja" xml:lang="ja"> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <meta http-equiv="Content-Style-Type" content="text/css"> <title></title> </head> <body><?php // 画像と拡張子を取得
// $img_path = 'C:/Program Files/Apache Software Foundation/Apache2.2\htdocs/img/ApacheMonitor.jpg';
$img_path = $_FILES['picture']['name'];
// $img_path = $_FILES['picture']['tmp_name'];
$img = file_get_contents($img_path);
$ext = pathinfo($img_path, PATHINFO_EXTENSION);
</body> </html>// データベースに保存 $pdo = new PDO('mysql:host=localhost; dbname=tada', 'root', 'pass'); $stmt = $pdo->prepare('INSERT INTO images VALUES(0, :ext, :img)'); $stmt->bindParam(':ext', $ext); $stmt->bindParam(':img', $img); $stmt->execute(); ?>
$img_path = 'C:/Program Files/Apache Software Foundation/Apache2.2\htdocs/img/ApacheMonitor.jpg';の代わりに
$img_path = $_FILES['picture']['name'];
と記入するとform_pic.htmlで画像を選択して送信ボタンを押すとinsert2.phpを読み込ん
で次のようなエラー文が発生します。
Warning: file_get_contents(ApacheMonitor.jpg) [function.file-get-contents]: failed to open stream: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\musen\insert2.php on line 15
line15は $img = file_get_contents($img_path);
の部分です。
代わりに$img_path = $_FILES['picture']['tmp_name'];
と記入しますと、画像データはmysqlへ保存されるのですが、mysqlの拡張子名の保存
部分が「jpg」ではなく、「tmp」と表示されます。ちなみにテーブルは次のsql文で作成
しました。extに拡張子名が保存されます。画像データはcontentsです。
CREATE TABLE images ( id int NOT NULL AUTO_INCREMENT, ext varchar(5), contents blob, PRIMARY KEY (id) );
何かうまい具合にinsert2.phpでフォームからの画像データの受け渡しが出来ないもの
かと悩んでおります。
どなたかアドバイスをよろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2015/08/14 08:51
2015/08/14 13:31
退会済みユーザー
2015/08/14 13:38 編集
2015/08/14 13:59
退会済みユーザー
2015/08/14 14:06 編集
2015/08/14 14:13 編集
2015/08/14 14:14
退会済みユーザー
2015/08/14 14:56
2015/08/14 15:50
退会済みユーザー
2015/08/15 05:07 編集
2015/08/15 06:37
退会済みユーザー
2015/08/15 06:44
2015/08/15 08:05