简体   繁体   中英

move_uploaded_file() working on localhost but on server

The following code works perfect on my localhost but it shows the following errors on my live server

Warning: move_uploaded_file(.../uploads/76948893.jpeg): failed to open stream: No such file or directory

Warning: move_uploaded_file(): Unable to move '/tmp/phppxvRs8' to '.../uploads/76948893.jpeg'

What it does is simple, it takes the images on the array ["pictures"] which comes from a html form and save every image on the folder ".../uploads/" using a random numeric name as name of the file and keeping the original extension.

Any one knows how to make it work on my server?

   //Image Uploader
    $images=[];
    $directory = '.../uploads/';
    foreach ($_FILES["pictures"]["error"] as $key => $error) {
        $new_file_name = rand (10000000,99999999);
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES["pictures"]["tmp_name"][$key];
        $name = $_FILES["pictures"]["name"][$key];
       /* echo '<br>';
        echo $directory.$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1);*/

        if(move_uploaded_file($_FILES['pictures']['tmp_name'][$key], 
                            $directory
                            .$new_file_name
                            .".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1)))               {
            array_push($images,$new_file_name.".".substr($_FILES['pictures']['type'][$key],strpos($_FILES['pictures']['type'][$key], "/")+1));
            $images_validator=true;
        }else{
        //Error
        }

    }
}

There could be many reason for this, Check the following

  1. You need WRITE Permission for the uploads directory. I assume your local machine runs windows & your hosting environment is linux

  2. Like @Darren suggests, use absolute path. change the $directory to $directory = getcwd() . 'uploads/'; $directory = getcwd() . 'uploads/';

If this runs on your local machine but not on server there are 2 easy answers I can think off right off the back. 1. The folder doesnt exist on the server, 2. as someone comment you dont have permission to write/read to that folder on the server....I would check the server configuration to make sure your app pool or users have read/write permissions to the folder

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM