これは正しい方法ではないと言う人もいると思いますが、アプリケーションを完成させる締め切りが迫っているため、今のところ、戻ってコードを変更して画像をディレクトリに保存することはできません。
それがクリアされた今
私が持っていた質問は、これを入力してデータベースに画像を挿入したことです。
(クラス セキュリティ コールは気にしないでください。データが有効かどうかを確認するだけです)
$filename = $security->secure($_FILES['imgschool']['name']);
$tmpname = $security->secure($_FILES['imgschool']['tmp_name']);
$imgsize = $security->secure($_FILES['imgschool']['size']);
$imgtype = $security->secure($_FILES['imgschool']['type']);
$school = $security->secure($_POST['school']);
//begin upload
if($imgsize > 0) {
$handle = fopen($tmpname, "r");
$content = fread($handle, filesize($tmpname));
$content = addslashes($content);
//code to add all this to database
}
変数 $content は画像であり、その取得はすべて追加スラッシュです。base64と呼ばれるものでそれを行うと誰かが言ったことを覚えていますが、それがどのように書かれていたかはほとんど思い出せません。
これは、データベースから画像を呼び出す方法です
すべてのクエリを除いて、これは画像を呼び出す主要部分です
header("Content-length: ".$imgsize);
header("Content-type: ".$imgtype);
header("Content-Disposition: attachment; filename=".$imgname);
print $row['img'];
私が抱えている問題は、代わりに画像が表示されていることです。URLは表示されているだけなので、この場合はこれしか表示されません
http://localhost/admin/school-catalog.php?page=gallery&id=4
ページを開いて、URLに正しいパラメータが設定された画像を表示するとき。
画像などを保存するために行われているクエリを見たい人のために、セクション全体をコピーしました
//save image to db
if(isset($_POST['btnupload'])) {
$filename = $security->secure($_FILES['imgschool']['name']);
$tmpname = $security->secure($_FILES['imgschool']['tmp_name']);
$imgsize = $security->secure($_FILES['imgschool']['size']);
$imgtype = $security->secure($_FILES['imgschool']['type']);
$school = $security->secure($_POST['school']);
//begin upload
if($imgsize > 0) {
$handle = fopen($tmpname, "r");
$content = fread($handle, filesize($tmpname));
$content = base64_encode($content);
}
$save = mysql_query("insert into tbl_schoolgallery(id,hash,img,imgtype,imgsize) values(null,'$school','$content','$imgtype','$imgsize')") or die(mysql_error());
header("Location: school-catalog.php?page=school_gallery");
}
//call image from db
$query = mysql_query("select * from $tbl where id = '$id'") or die(mysql_error());
while($row = mysql_fetch_assoc($query)) {
$imgtypeget = explode("/", $row['imgtype']);
$imgname = "img.".$imgtypeget[1];
$imgtype = $row['imgtype'];
$imgsize = $row['imgsize'];
header("Content-length: ".$imgsize);
header("Content-type: ".$imgtype);
print base64_decode($row['img']);
print $row['img'];
}