-2
私はこのコードで一晩中私の頭を傷つけています。問題は、私が自分のデータベースにブログ画像をアップロードしようとすると、そこに現れないということです。それは私のアップロードフォルダに表示されます。ときにはコードをぶち壊してアップロードするようになったが、画像そのものは空に見えた。名前/サイズやファイルタイプのような他の情報はすべて私のデータベースに表示されます。私はどんなアイデアが間違っているのでしょうか?イメージはMYSQLデータベースにはアップロードされませんがアップロードフォルダーに表示されます
<?php
require('config.php');
session_start();
if(isset($_POST['save']))
{
$target_dir = "upload/img/";
$filename = explode('.',$_FILES['image']['name']);
$ext = $filename[1];
$imgname = time().'.'.$ext;
$target_file = $target_dir . $imgname ;
$imgData =addslashes(file_get_contents($_FILES['image']['tmp_name'], $target_file));
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
$check = getimagesize($_FILES["image"]["tmp_name"]);
if($check !== false) {
$text="File is an image - " . $check["mime"] . ".";
$uploadOk = 1;
} else {
$text="File is not an image.";
$uploadOk = 0;
}
// Check if file already exists
if(file_exists($target_file)) {
$text="Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if($_FILES["image"]["size"] > 2000000) {
$text="Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" && $imageFileType != "bmp") {
echo "Sorry, only JPG, JPEG, PNG, GIF & BMP files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if($uploadOk == 0) {
$_SESSION["error"]=$text;
header("Location:index.php?id=$id"); /* Redirect browser */
exit();
// if everything is ok, try to upload file
}else{
if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
$path=$imgname;
$finfo = new finfo();
$array = explode('.', $_FILES['image']['name']);
$extension = mime_content_type($target_file);
$filesize=$_FILES["image"]["size"];
$filedata=$imgData;
$conn->query("INSERT INTO images (image, name,filetype,size) VALUES (LOAD_FILE('$filedata','$path','$extension','$filesize')");
$_SESSION["Success"]='Upload success';
header("Location:index.php"); /* Redirect browser */
exit();
} else {
$_SESSION["err"]=$text;
header("Location:index.php"); /* Redirect browser */
exit();
}
}
}
?>
'config.php'が接続を作成し、' $ conn'に設定していますか? – FirstOne
まだ投稿していない場合は、https://stackoverflow.com/questions/8229951/load-file-doesnt-workこちらの記事からアドバイスをお試しください。 – Azuloo
**警告**:ユーザーデータがクエリ内で使用されるため、これには重大な[SQLインジェクションのバグ](http://bobby-tables.com/)があります。可能な限り**準備文**を使用してください。これらは[mysqli](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php)と[PDO](http://php.net/manual/)で行うのはかなり簡単です。 en/pdo.prepared-statements.php)ここで、ユーザが提供するデータは '?'または ':name'のインジケータで指定され、後で' bind_param'や 'execute'を使ってデータを入力します。 ** '$ _POST'、' $ _GET'、または任意のユーザーデータを直接クエリに入れないでください。 – tadman