php move_uploaded_file() 无法打开流:没有这样的文件或目录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14596832/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:36:33  来源:igfitidea点击:

move_uploaded_file() failed to open stream: no such file or directory

phpfile-upload

提问by Fibericon

I've done your standard checks (is the directory there, are lax enough permissions set), and I'm pretty sure I've covered your standard stupid human tricks. Here's the code that's failing:

我已经完成了你的标准检查(目录是否在那里,权限设置是否足够宽松),而且我很确定我已经涵盖了你标准的愚蠢的人类技巧。这是失败的代码:

move_uploaded_file($_FILES['image1']['tmp_name'], "/public_html/flashsale/assets/img/products/T".$_FILES['image1']['name']);

The directory is there - I copied the path from FileZilla. I even set the permissions to 777, both in FileZilla and in the file manager on the HostGatorcontrol panel. This code generates two warnings:

目录在那里 - 我从FileZilla复制了路径。我什至在 FileZilla 和HostGator控制面板上的文件管理器中将权限设置为777。此代码生成两个警告:

Message: move_uploaded_file(/public_html/flashsale/assets/img/products/Tsirloin.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory

Message: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpI5GZ3S' to '/public_html/flashsale/assets/img/products/Tsirloin.jpg'

消息:move_uploaded_file(/public_html/flashsale/assets/img/products/Tsirloin.jpg) [function.move-uploaded-file]:无法打开流:没有这样的文件或目录

消息:move_uploaded_file() [function.move-uploaded-file]:无法将“/tmp/phpI5GZ3S”移动到“/public_html/flashsale/assets/img/products/Tsirloin.jpg”

In that order. So, the file is being uploaded, the directory exists and is set to 777, what else could I be missing?

以该顺序。那么,文件正在上传,目录存在并设置为 777,我还能缺少什么?

回答by syrkull

you do not need to put the full directory to the file. try to remove /public_html/flashsale/from your link and see if that will work. In addition, the file does not need to have 777 permission, I myself upload files to folders with 755 permissions.

您不需要将完整目录放入文件中。尝试/public_html/flashsale/从您的链接中删除,看看这是否有效。另外文件不需要777权限,我自己上传文件到755权限的文件夹。

also, you can use getcwd();in the directory your aiming to. the function will give you the directory that you need to use for moving your file. source

此外,您可以getcwd();在目标目录中使用。该函数将为您提供移动文件所需的目录。来源

回答by UWU_SANDUN

The Problem

问题

$dirpath = dirname(getcwd())

This is what I used initially to get the directory path to my /public_html/upload folder. $dirpath will contain

这是我最初用来获取我的 /public_html/upload 文件夹的目录路径的方法。$dirpath 将包含

/public_html/upload

The Solution(On server)

解决方案(在服务器上)

$dirpath = realpath(dirname(getcwd()))

Since I'm on a shared hosting environment, the right way of getting move_uploaded_file to work is using this as the destination: realpath(dirname(getcwd())) returns something like:

由于我在共享托管环境中,让 move_uploaded_file 工作的正确方法是使用它作为目标: realpath(dirname(getcwd())) 返回如下内容:

/home/cpanelusername/public_html/upload

回答by Pao Im

For Ubuntu 14.04 with XAMPP, I also have problem with upload but after I have fixed with sudo chmod -R 777 destination, it works well.

对于带有 XAMPP 的 Ubuntu 14.04,我也有上传问题,但在我用 修复后sudo chmod -R 777 destination,它运行良好。

Here is what I did:

这是我所做的:

Temporary folder to upload in my Ubuntu 14.04 XAMPP is /opt/lampp/temp/. If I want my upload files to /opt/lampp/temp/testuploadas destination folder, then I need config bellow.

在我的 Ubuntu 14.04 XAMPP 中上传的临时文件夹是 /opt/lampp/temp/。如果我希望我的上传文件/opt/lampp/temp/testupload作为目标文件夹,那么我需要下面的配置。

  1. Go to temp folder

    cd /opt/lampp/temp/
    
  2. Create 'testupload' folder under /opt/lampp/temp/

    sudo mkdir testupload
    
  3. Change permission 777

    sudo chmod -R 777 /opt/lampp/temp/testupload/
    
  4. PHP code

    move_uploaded_file($_FILES["file"]["tmp_name"], "/opt/lampp/temp/testupload/" . $_FILES["file"]["name"])
    
  1. 转到临时文件夹

    cd /opt/lampp/temp/
    
  2. 在 /opt/lampp/temp/ 下创建“testupload”文件夹

    sudo mkdir testupload
    
  3. 更改权限 777

    sudo chmod -R 777 /opt/lampp/temp/testupload/
    
  4. PHP代码

    move_uploaded_file($_FILES["file"]["tmp_name"], "/opt/lampp/temp/testupload/" . $_FILES["file"]["name"])
    

回答by Ramaraju.d

Make sure the path you are traversing. public_html is not required

确保您正在穿越的路径。public_html 不是必需的

$image=basename($_FILES['file']['name']);
$image=str_replace(' ','|',$image);

$tmppath="images/".$image;

        if(move_uploaded_file($_FILES['file']['tmp_name'],$tmppath))
        {
         echo "success";
        }
        else
        {
         echo "fail";
        }

Hope this helps

希望这可以帮助

回答by Hackmodford

Solution for Windows and ISS:

适用于 Windows 和 ISS 的解决方案:

The IUSR account needed permissions in the destination directory. Not the ISS_IUSR account, just the IUSR account.

IUSR 帐户需要目标目录中的权限。不是 ISS_IUSR 帐户,只是 IUSR 帐户。