move_uploaded_file() failed to open stream

2020-02-05 13:11发布

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 HostGator control panel. This code generates two warnings:

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'

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

5条回答
Melony?
2楼-- · 2020-02-05 13:43

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

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:

/home/cpanelusername/public_html/upload
查看更多
相关推荐>>
3楼-- · 2020-02-05 13:44

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

$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

查看更多
爷、活的狠高调
4楼-- · 2020-02-05 13:48

Solution for Windows and ISS:

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

查看更多
ら.Afraid
5楼-- · 2020-02-05 13:50

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.

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/testupload as destination folder, then I need config bellow.

  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"])
    
查看更多
Anthone
6楼-- · 2020-02-05 13:55

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.

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

查看更多
登录 后发表回答