20

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?

Vic Seedoubleyew
  • 9,888
  • 6
  • 55
  • 76
Fibericon
  • 5,684
  • 12
  • 37
  • 64
  • Don't always use absolute path. Or make sure the path from root folder to the images folder is correct. – Ramaraju.d Jan 30 '13 at 04:53
  • Possible duplicate of [Failed to open stream : No such file or directory](http://stackoverflow.com/questions/36577020/failed-to-open-stream-no-such-file-or-directory) – Vic Seedoubleyew Sep 10 '16 at 10:48
  • Possible duplicate of [PHP: move\_uploaded\_file() failed to open stream: No such file or directory](https://stackoverflow.com/questions/10951334/php-move-uploaded-file-failed-to-open-stream-no-such-file-or-directory) – Sunil B N Jul 27 '17 at 10:51

5 Answers5

20

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

syrkull
  • 2,295
  • 4
  • 35
  • 68
11

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
UWU_SANDUN
  • 1,123
  • 13
  • 19
  • 1
    Thanks this saved me :) (y) – Ajmal Jamil Dec 05 '15 at 12:09
  • gud luck programming Ajmal...(Y) – UWU_SANDUN Dec 05 '15 at 12:22
  • You explained this well but you didn't put the sample code, where should I put the $dirpath variable? – samouray Mar 08 '16 at 09:39
  • When do you want to upload files into your server..Then you need a file uploading path(Folder path in server).So If you can not get it correctly Then you should be errors on your page..So I have mentioned here to get real folder path method..I got a solution by this methode..You also can try this friend.. :) @samouray – UWU_SANDUN Mar 10 '16 at 08:31
2

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

Ramaraju.d
  • 1,301
  • 6
  • 26
  • 46
  • I was searching like several hours for this solution, didn't know why $_FILES['file']['name'] was empty all the time when I was trying to push the file name. Thanks sir !! – que1326 Aug 17 '16 at 09:48
2

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"])
    
ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
Pao Im
  • 337
  • 3
  • 8
0

Solution for Windows and ISS:

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

Hackmodford
  • 3,901
  • 4
  • 35
  • 78