PHP: Error when user uploads image to site

I am trying to let a user upload an image and that image will stay in a folder called, "Images." I am not sure if I am formatting this correctly because the tutorial I am following isn't using a server.

Here are the errors that I currently get:

Warning: move_uploaded_file(/student/globalit/2019/GamerMedia/pages/images/TrollFace.jpg): failed to open stream: No such file or directory in /home/benrud/public_html/student/globalit/2019/GamerMedia/pages/account.php on line 13

Warning: move_uploaded_file(): Unable to move '/tmp/phpVzhJCX' to '/student/globalit/2019/GamerMedia/pages/images/TrollFace.jpg' in /home/benrud/public_html/student/globalit/2019/GamerMedia/pages/account.php on line 13

Here is the link to the folder

Below is my code.Any help is appreciated. Thanks.

<?php
    if(isset($_POST['submit'])){
        move_uploaded_file($_FILES['file']['tmp_name'],'/student/globalit/2019/GamerMedia/pages/images/'.$_FILES['file']['name']);
    }
?>

2 answers

  • answered 2019-10-08 05:59 Ajowi

    You can try the following:

    //images may be many, just load into a named array 
    $filesArray = $_FILES;
    
    //first properly target your directory (which should have write permission)
    $ximage = "ppsize_photo";   //actual name or id as in html 
    $target_dir = $_SERVER['DOCUMENT_ROOT'] ."/student/globalit/2019/GamerMedia/pages/images/"; 
    $target_file = $target_dir . basename($filesArray[$ximage]["name"]);
    $ext1 = pathinfo($target_file, PATHINFO_EXTENSION);
    $imageFileType = $ext1;     //you may use this later to reject some types 
    
    //ensure that your file names are unique (use any means possible)
    $unique_id = substr( base_convert( time(), 10, 36 ) . md5( microtime() ), 0, 16 );
    $unique_id1 = $unique_id . "." . $ext1; 
    $target_file = $target_dir . $unique_id1;   //complete file name; maybe here is where your error was 
    
    //you may also validate the extensions; it may not be an image 
    if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
        echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
        exit();
    }else {
        //fine
    }
    
    //now move the image 
    $uploadFlag = false;
    if (move_uploaded_file($filesArray[$ximage]["tmp_name"], $target_file)) {
        //it went fine 
        $uploadFlag = true;             
    } else {
        //echo "Sorry, there was an error uploading your file.";
        $uploadFlag = false;
    }
    

  • answered 2019-10-08 05:59 Aditya Singh

    Please check your destination path... I believe Your destination path is invalid