質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

Q&A

解決済

1回答

624閲覧

PHPでjpgをダウンロードして表示できない

kokok

総合スコア145

PHP

PHPは、Webサイト構築に特化して開発されたプログラミング言語です。大きな特徴のひとつは、HTMLに直接プログラムを埋め込むことができるという点です。PHPを用いることで、HTMLを動的コンテンツとして出力できます。HTMLがそのままブラウザに表示されるのに対し、PHPプログラムはサーバ側で実行された結果がブラウザに表示されるため、PHPスクリプトは「サーバサイドスクリプト」と呼ばれています。

0グッド

0クリップ

投稿2019/11/08 05:27

編集2019/11/08 05:44

html

1//index.html 2 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6 <link rel="stylesheet" href="style.css"> 7 <meta charset="utf-8"> 8 <title>画像アップロード</title> 9</head> 10<body> 11<h1>画像登録</h1> 12<form action="Image-registration.php" method="post" enctype="multipart/form-data"> 13<div class="image-input"> 14<label for="title">画像タイトル:</label> 15<input type="text" name="title" required><br> 16<label for="image-info">画像情報:</label> 17<input type="text" name="image-info" required><br> 18<label for="photographer">撮影者:</label> 19<input type="text" name="photographer" required><br> 20<label for="file1">画像:</label> 21<input type="file" name="file1" required><br> 22<button class="image-registration-button" type="submit">登録</button> 23</div> 24</form> 25<form action="Image-list.php"> 26<button type="submit">画像一覧を見る</button> 27</form> 28</body> 29</html> 30 31

php

1//Image-registration.php 2 3<!DOCTYPE html> 4<html lang="ja"> 5<head> 6<meta charset="utf-8"> 7 <link rel="stylesheet" href="style.css"> 8<title>画像アップロード</title> 9</head> 10<body> 11<?php 12//header('Content-Type: text/html; charset=UTF-8'); 13 14require_once 'MyPDO.php'; 15$pdo = new DBIO\MyPDO(); 16 17//画像タイトルの受け取り 18 $title = $_REQUEST["title"]; 19//画像に関する情報を受け取り 20 $image_info = $_REQUEST["image-info"]; 21//撮影者の受け取り 22 $photographer = $_REQUEST["photographer"]; 23//画像の受け取り 24 25 26 //ファイルがあれば(index.htmlのinputの方で入力必須にしてはあります) 27 if(isset($_FILES["file1"])){ 28 //入力されたファイル名 29 $file = $_FILES["file1"]["name"]; 30 31 32 //文字コードをUTF-8からcp932に変換 33 $fileDir = mb_convert_encoding($file,"cp932","UTF-8"); 34 //アップロードされたファイルを専用のディレクトリに保存 35 move_uploaded_file($_FILES["file1"]["tmp_name"], 36 $_SERVER['DOCUMENT_ROOT']."/ImgManager/imgs/$fileDir"); 37 38 //保存先のパス 39 $filePath = "/ImgManager/imgs/$file"; 40 41 //画像情報をデータベースに保存($result はデータベースに保存成功すればtrue,失敗false) 42 $result = $pdo->insertImgInfo_recode($title,$image_info,$photographer, $filePath); 43 44 if( $result){ 45 echo "登録成功しました"; 46 47 }else{ 48 echo "登録失敗しました"; 49 } 50 } 51 52// header("location:index.html "); 53?> 54<form action="index.html"> 55<button type="submit">戻る</button> 56</form> 57</body> 58</html>

php

1//Image-list.php 2<!DOCTYPE html> 3<html lang="ja"> 4<head> 5<meta charset="utf-8"> 6<link href="https://fonts.googleapis.com/css?family=Lato&display=swap" rel="stylesheet"> 7<link rel="stylesheet" href="style.css"> 8<title>画像一覧</title> 9</head> 10<body> 11<form action="index.html"> 12 13</form> 14<?php 15require_once 'MyPDO.php'; 16$pdo = new DBIO\MyPDO(); 17 18$recodes = $pdo-> getRecodeAll(); 19 20?> 21<table class="image-list-table"> 22 23<tr> 24<th colspan="3" align="center" class="image-list-th">画像リスト</th> 25<th colspan="1" valign="middle"><form action="index.html"><button type="submit" class="back-button">戻る</button></form></th> 26</tr> 27<tr> 28<?php 29//何列で表示するかを指定 30$row_length = 4; 31$count = 0; 32foreach ($recodes as $value) { 33$id =$value["ID"]; 34$title = $value['TITLE']; 35$path = $value['PATH']; 36$info = $value['INFO']; 37$phote = $value['PHOTOGRAPHER']; 38$date = $value['DATE']; 39//文字コードをcp932からにUTF-8変換 40//$path = mb_convert_encoding($path,"UTF-8" ,"cp932"); 41if($count === 0){ 42 echo "<tr>"; 43} 44$count++; 45?> 46<td class="title-td" colspan="1" ><h2 class="images-title1">TITLE</h2><h2 class="images-title2"> 47<?php echo "$title"?></h2><img class="images" height="200" width="200" src="<?php echo $path ?>"> 48<h2 class="images-info-h2">INFO</h2><p class="image-info"><?php echo "$info(撮影者:$phote : 投稿日$date)"?></p> 49<form action="Image-info-update.php"><button type="submit" class="image-info-update-button" name ="info-update" value="<?php echo $id?>">画像情報の更新</button></form> 50<form action="Download.php"><button type="submit" class="image-info-update-button" name ="image-save" value="<?php echo $path?>">画像を保存する</button></form> 51<hr width="200" color="#555"> 52</td> 53<?php 54if($count % $row_length === 0){ 55 echo "</tr>"; 56 $count = 0; 57} 58} 59?> 60</tr> 61</table> 62</body> 63</html> 64

php

1 2//Download.php 3 4<?php 5 6$path = $_SERVER['DOCUMENT_ROOT'].$_REQUEST['image-save']; 7 8//画像のパスとファイル名 9$fpath = "$path"; 10$fname = pathinfo($path, PATHINFO_BASENAME); 11 12//画像のダウンロード 13header('Content-Type: application/octet-stream'); 14header('Content-Length: '.filesize($fpath)); 15header('Content-disposition: attachment; filename="'.$fname.'"'); 16readfile($fpath);

イメージ説明

os:windows10
開発環境:Apache+PHP+MySQL

サーバにアップロードした画像をwebページに表示してその画像をダウンロードすると、ダウンロードは出来るのですが

表示しようとすると

windowsフォトビューアーで開こうとすると
windowsフォトビューアーでこの画像を開けません。ファイルが破損しているか、大きすぎる可能性があります。
となります。

フォトで開こうとすると
このファイルはサポートされていない形式のようです。となります。

サーバにアップロードする前には、開けるので 自分の書いたどこかのプログラムがおかしいとは思うのですが、 原因が特定できなかったのでアドバイス頂けると助かります。

--追記--

php

1<?php 2namespace DBIO; 3 4use PDO; 5use PDOException; 6 7class MyPDO{ 8 9 private $dsn; 10 private $user; 11 private $pass; 12 private $pdo; 13 14 function __construct(){ 15 16 $this->dsn = "mysql:dbname=test;host=localhost;charset=utf8mb4"; 17 $this->user = "root"; 18 $this->pass = ""; 19 $this->connect(); 20 } 21 22 function connect(){ 23 24 $this->pdo = new PDO($this->dsn, $this->user, $this->pass, array( 25 PDO::ATTR_PERSISTENT => true 26 )); 27 } 28 29 function close(){ 30 31 $this->pdo = null; 32 } 33 34 function getAll(){ 35 36 $sql = "SELECT * FROM images"; 37 $this->connect(); 38 $tmp = $this->pdo->query($sql); 39 40 // 全部の行を配列して返す 41 $result = $tmp->fetchAll(); 42 $this->close(); 43 44 return $result; 45 } 46 47 function getInfo($id){ 48 49 $sql = "SELECT * FROM images WHERE id=?"; 50 $this->connect(); 51 $stmt = $this->pdo->prepare($sql); 52 $stmt->bindParam(1, $id); 53 54 // 戻り値 成功でtrueが返ってくる 55 $stmt->execute(); 56 $result = $stmt->fetchAll(); 57 58 $this->close(); 59 60 return $result; 61 } 62 63 // データベースに画像情報一行分のレコードを追加 64 function insertImgInfo_recode($title, $info, $photographer, $filePath){ 65 try { 66 67 // SQLの実行結果の判断 68 // $result = false; 69 70 $sql = "INSERT INTO images(TITLE, INFO, PHOTOGRAPHER, PATH, DATE) VALUES(?,?,?,?,?)"; 71 $date = date("Y/m/d G:i:s"); 72 $this->connect(); 73 $stmt = $this->pdo->prepare($sql); 74 $stmt->bindParam(1, $title); 75 $stmt->bindParam(2, $info); 76 $stmt->bindParam(3, $photographer); 77 $stmt->bindParam(4, $filePath); 78 $stmt->bindParam(5, $date); 79 // 実行結果 80 $result = $stmt->execute(); 81 } catch (PDOException $e) { 82 print('PDOException:' . $e->getMessage()); 83 } finally{ 84 85 $this->close(); 86 } 87//実行結果が成功か失敗かを返す: true/flse 88 return $result; 89 } 90 91 92 //データベースimagesテーブルの全てのデータ取得 93 94 function getRecodeAll(){ 95 96 $sql = "SELECT * FROM images"; 97 $this->connect(); 98 $stmt = $this->pdo->prepare($sql); 99 100 $stmt->execute(); 101 $recode = $stmt->fetchAll(); 102 $this->close(); 103 104 //テーブルの全てのデータを返す 105 return $recode; 106 } 107 108 109 //idで一行分のレコード取得 110 function getRecode($id){ 111 112 $sql = "SELECT * FROM images WHERE ID = ?"; 113 $this->connect(); 114 $stmt = $this->pdo->prepare($sql); 115 $stmt->bindParam(1, $id); 116 $stmt->execute(); 117 $recode = $stmt->fetchAll(); 118 $this->close(); 119 120 //一行分のデータを返す 121 return $recode; 122 } 123 124 125 function updateRecode($id,$title,$image_info,$photographer){ 126 127 128 $sql = "UPDATE images SET TITLE=?, INFO=?, PHOTOGRAPHER=?, DATE=? where ID = ?"; 129 130 $date = date("Y/m/d G:i:s"); 131 $this->connect(); 132 $stmt = $this->pdo->prepare($sql); 133 134 $stmt->bindParam(1, $title); 135 $stmt->bindParam(2, $image_info); 136 $stmt->bindParam(3, $photographer); 137 $stmt->bindParam(4, $date); 138 $stmt->bindParam(5, $id); 139 $result = $stmt->execute(); 140 141 $this->close(); 142 143 //更新結果が成功ならtrue/失敗ならfalse 144 return $result; 145 } 146} 147 148

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2019/11/08 05:35

アップしたサーバー上のファイルはどうなってますか?
kokok

2019/11/08 05:40

アップしたサーバーの方は、問題なく表示できます。
Y.H.

2019/11/08 05:52

> windowsフォトビューアーでこの画像を開けません。ファイルが破損しているか、大きすぎる可能性があります。 メモ帳で開いてみましょう。多分404 Not FoundなどのエラーページのHTMLになっていると思います。
m.ts10806

2019/11/08 05:54

「問題なく表示させた」手順を教えてください。なんとなく私が想定している内容ではないと思っています。
kokok

2019/11/08 06:04

Y.H.さん >メモ帳で開いてみましたが、404 Not Found などエラーは出ませんでした。 m.ts10806さん >\xampp\htdocs/フォルダ/imasの画像を保存していたので、そこのファルダに保存された画像をファトなので開きました。
guest

回答1

0

自己解決

php

1//Download.php 2 3$fpaths = "$path"; 4$fpath = mb_convert_encoding($fpaths,"cp932","UTF-8"); 5$fnames =pathinfo($path, PATHINFO_BASENAME); 6$fname = mb_convert_encoding($fnames,"cp932","UTF-8");

と 画像のパスとファイル名 の文字コードを変更すると表示できました。

投稿2019/11/08 05:59

編集2019/11/08 06:07
kokok

総合スコア145

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

m.ts10806

2019/11/08 06:01

コードはマークダウンで。
kokok

2019/11/08 06:08

何度もすみません。 ありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問