我有这个PHP上传脚本。我可以找到JPG,但不会上传图片。我试图在Linux mint机器上的localhost上执行此操作,因此我的代码位于/ var / www / html / uploads。这是我的upload.php代码。
<?php
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["fileToUpload"] ["name"]);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
// Check if image file is a actual image or fake image
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;
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 500000) {
echo "Sorry, your file is too large.";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} 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.";
}
}
?>
这是我的media_upload.php代码
<!DOCTYPE html>
<html>
<body>
<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>
</body>
</html>
以下是来自我的apache2错误日志的错误消息。我认为我的问题可能是权限或路径问题。所有帮助非常感谢。
PHP警告:move_uploaded_file(uploads / bedshaper_1021-36.jpg):无法打开流:第38行/var/www/html/uploads/upload.php中没有此类文件或目录,引用:{{3} }
PHP警告:move_uploaded_file():无法在第38行的/var/www/html/uploads/upload.php中将'/ tmp / phpXeCU8c'移动到'uploads / bedshaper_1021-36.jpg',referer:{{ 3}}
答案 0 :(得分:1)
您必须创建文件夹&#34;上传&#34; in&#34; / var / www / html / uploads&#34;那它应该有效。 你也可以改变
$target_dir = "uploads/";
到
$target_dir = "";
如果您想将图片保存在&#34; / var / www / html / uploads&#34;你应该做一条直接的道路。
$target_dir = "var/www/html/uploads";
修改强> 由于您的上传脚本位于您所指的上传文件夹中,因此该脚本无法找到该文件夹。
$target_dir = "../uploads"
也可以使用
答案 1 :(得分:1)
/var/www/html/uploads
将这两个.php文件移到此文件夹之外,如果没有在下面阅读,它将正常工作。
您不必更改脚本中的任何内容,只需授予“uploads”文件夹的权限即可。刚测试它,它工作正常。
这里有很好的说明如何做到: enter link description here
答案 2 :(得分:0)
我通过将文件夹重命名为产品并重置产品文件夹和upload.php上的权限来解决问题我更改了tar_direct = / var / www / html / uploads / products,一切正常。谢谢你的建议问题解决了。