将图像上传到MYSQL数据库

时间:2017-11-28 12:20:11

标签: php mysql

我想将图像存储到MySQL数据库中。但显然我收到的只是这个[Secreenshot 1]。而且,我如何将图像上传到MySQL数据库? (我知道我需要在$ target_dir进行编辑但是我应该把它放在哪里?

用于上传的HTML代码。

<form action="upload.php" method="post" enctype="multipart/form-data">
Select image to upload:
<input type="file" name="fileToUpload" id="fileToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>

upload.php的PHP代码

<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
    echo "File is an image - " . $check["mime"] . ".";
    $uploadOk = 1;
} else {
    echo "File is not an image.";
    $uploadOk = 0;
}
}
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
    echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
} else {
    echo "Sorry, there was an error uploading your file.";
}
}
?>

预览! Secreenshot 1

1 个答案:

答案 0 :(得分:-1)

可能有两个问题

  1. 目录丢失:您尝试上传的目录可能会丢失。在上传文件之前确保目标目录存在
  2. 权限:Apache可能没有适当的权限将文件从tmp目录移动到目标目录。请为目标目录提供适当的文件上载权限。如果您使用的是Linux环境,则可以运行chmod或chown命令来实现此目的。