图片上传错误无法打开流:没有这样的文件或目录
Posted
技术标签:
【中文标题】图片上传错误无法打开流:没有这样的文件或目录【英文标题】:Image upload error failed to open stream: No such file or directory 【发布时间】:2018-08-10 06:57:14 【问题描述】:我正在尝试上传图片,但在下方收到此错误消息,我该如何解决? 警告:move_uploaded_file(images/homepage2.jpg): failed to open stream: No such file or directory in D:\www\mdx\upload_image.php on line 32
警告:move_uploaded_file(): Unable to move 'D:\XAMPP\tmp\phpA29E.tmp' to 'images/homepage2.jpg' in D:\www\mdx\upload_image.php on line 32 抱歉,上传您的文件时出错。
<!DOCTYPE html>
<html>
<head>
<title>Image Upload Demo</title>
</head>
<body>
<form action="upload_image.php" method="post" enctype="multipart/form-
data">
Select image to upload:
<input type="file" name="imageToUpload">
<input type="submit" value="Upload Image" name="submit">
</form>
</body>
</html>
单独的php文件
<?php
//Check file data has been sent
if(!array_key_exists("imageToUpload", $_FILES))
echo 'File missing.';
return;
if($_FILES["imageToUpload"]["name"] == "" || $_FILES["imageToUpload"]
["name"] == null)
echo 'File missing.';
return;
$uploadFileName = $_FILES["imageToUpload"]["name"];
/* Check if image file is a actual image or fake image
tmp_name is the temporary path to the uploaded file. */
if(getimagesize($_FILES["imageToUpload"]["tmp_name"]) === false)
echo "File is not an image.";
return;
// Check that the file is the correct type
$imageFileType = pathinfo($uploadFileName, PATHINFO_EXTENSION);
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType !=
"jpeg" && $imageFileType != "gif" )
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
return;
//Specify where file will be stored
$target_file = 'images/' . $uploadFileName;
/* Files are uploaded to a temporary location.
Need to move file to the location that was set earlier in the script */
if (move_uploaded_file($_FILES["imageToUpload"]["tmp_name"], $target_file))
echo "The file ". basename( $_FILES["imageToUpload"]["name"]). " has
been uploaded.";
echo '<p>Uploaded image: <img src="' . $target_file . '"></p>';//Include
uploaded image on page
else
echo "Sorry, there was an error uploading your file.";
【问题讨论】:
D:\www\mdx\images\
处真的有写权限的目录吗?
不确定。我该如何检查?图像、html 和 php 文件都在名为 mdx 的文件夹中,该文件夹位于名为 www 的文件夹中
【参考方案1】:
改变这一行:
$target_file = 'images/' . $uploadFileName;
收件人:
$target_file = 'images/' . basename( $_FILES["imageToUpload"]["name"]);
还要确保您拥有images
文件夹。
【讨论】:
没有工作...尝试上传图片时出现同样的错误 您真的有名为images
的文件夹吗?您的 php 脚本是否与文件夹在同一页面上?
哇,我真是个菜鸟。我刚刚创建了 images 文件夹,它就成功了!【参考方案2】:
见:
//Specify where file will be stored
$target_file = 'images/' . $uploadFileName;
您应该将目标文件更改为绝对路径,或者使用您的 php 脚本文件将其更改为相对路径:
$target_file = './images/' . $uploadFileName;
如果您的代码仍然无法正常工作,请尝试:
try
/* Files are uploaded to a temporary location.
Need to move file to the location that was set earlier in the script */
if (move_uploaded_file($_FILES["imageToUpload"]["tmp_name"], $target_file))
echo "The file " . basename($_FILES["imageToUpload"]["name"]) . " has been uploaded.";
echo '<p>Uploaded image: <img src="' . $target_file . '"></p>';
catch (Exception $e)
echo $e->getMessage();
...知道你得到了什么。
【讨论】:
你得到了什么?以上是关于图片上传错误无法打开流:没有这样的文件或目录的主要内容,如果未能解决你的问题,请参考以下文章
require_once:无法打开流错误:没有这样的文件或目录
将 laravel 部署到 Nginx 中出现错误“无法打开流:没有这样的文件或目录”
无法打开流:没有这样的文件或目录,但目录存在。在本地主机上工作但不在实时站点上工作[重复]
Laravel 5.5:Storage::put,file_put_contents 错误:无法打开流:没有这样的文件或目录