简体   繁体   中英

PHP error saying I cant upload image move_upload_file

I'm working on a PHP website that sells cars. When a user wants to put his car for sale, he needs to add an image. When I try to do that, the following error occurs: Warning: move_uploaded_file(C:/xampp/htdocs/carMela/mainSite/assets/image/4-BMW-E90AC-Kit-by-Xclusive-Customz-Sheffield_16959173929_l.jpg): failed to open stream: No such file or directory in C:\\xampp\\htdocs\\cars\\class\\userPost\\userpost.php on line 165

Warning: move_uploaded_file(): Unable to move 'C:\\xampp\\tmp\\phpCEDC.tmp' to 'C:/xampp/htdocs/carMela/mainSite/assets/image/4-BMW-E90AC-Kit-by-Xclusive-Customz-Sheffield_16959173929_l.jpg' in C:\\xampp\\htdocs\\cars\\class\\userPost\\userpost.php on line 165 Sorry, there was a problem uploading your file.

And that line is:

if(move_uploaded_file($_FILES['car_image']['tmp_name'], $target))

And this is my code for image upload:

/*this function for image uploaded */
public function save_image_info(){

    $image_extensions_allowed = array("gif", "jpeg", "jpg", "png");
    $file_name = $_FILES["car_image"]["name"];
    $ext = strtolower(substr(strrchr($file_name, "."), 1));
    $directory = $this->uploadFilePath.'mainSite/assets/image/';
    $target = $directory . basename( $file_name) ;
    $image_name     = basename($file_name);
    $new_img_url    = $image_name;
    $image_size = $_FILES['car_image']['size'];

    if (!empty($_FILES)) {
        if ($image_size > 5000000) {
            die('File size is too large! Please upload Small file.');
        } elseif(!in_array($ext, $image_extensions_allowed)) {
            die("You must upload a file with one of the following extensions: ".$ext);
        }
        else{
            //Now upload here
            if(move_uploaded_file($_FILES['car_image']['tmp_name'], $target))
            {
                return $new_img_url;
            }
            else {
                die('Sorry, there was a problem uploading your file.');
            }
        }
    } else {
        die('The file you Upload is not an image! Please upload a Valid Image.');
    }
}

My path to wasnt right,

$this->uploadFilePath = $_SERVER["DOCUMENT_ROOT"].'/cars/';

This was it so i changed and it worked, thanks for help

I think the problem is with your forward and backslashes, windows uses backslashes and you are using forward slashes which are used in linux, try to change them to "\\" instead of "/". otherwise if thats not the answer then you will have to edit your question and show us your directory structure, the problem could be as simple as a spelling mistake

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