After successful php upload -- file not found

I’m having inconsistent results with the php file upload script below.

After each upload attempt the $statusMsg tells me the upload was completed successfully but when I look for the file in the target directory nothing is found about half the time.

phpinfo() has no entry for upload_tmp_dir. Could that be the problem even if it works 50% of the time?

$targetDir = $_POST['path'];
$fileName = basename($_FILES["file"]["name"]);
$targetFilePath = $targetDir . $fileName;
$fileType = pathinfo($targetFilePath,PATHINFO_EXTENSION);

if(isset($_POST["submit"]) && !empty($_FILES["file"]["name"])){
    // Allow certain file formats
    $allowTypes = array('jpg','jpeg');
    if(in_array($fileType, $allowTypes)){
        // Upload file to server
        if(move_uploaded_file($_FILES["file"]["tmp_name"], $targetFilePath)){
            $statusMsg = "The file ".$fileName. " has been uploaded successfully to ".$targetFilePath ;
        }else{
            $statusMsg = "Sorry, there was an error uploading your file.";
        }
    }else{
        $statusMsg = 'Sorry, only JPG, JPEG files are allowed to upload.';
    }
}else{
    $statusMsg = 'Please select a file to upload.';
}

Here’s what var_dump($_FILES) shows:

array(1) { [β€œfile”]=> array(5) { [β€œname”]=> string(13) β€œworldMap4.jpg” [β€œtype”]=> string(10) β€œimage/jpeg” [β€œtmp_name”]=> string(14) β€œ/tmp/phpD9lY4v” [β€œerror”]=> int(0) [β€œsize”]=> int(3247134) } }

And $statusMsg shows: β€œThe file worldMap4.jpg has been uploaded successfully to …/LIBRARY/clientID/images/worldMap4.jpg”

The most common faults in PHP upload are memory issues or the server /tmp directory is filled.

You can set the upload_tmp_dir to a user location to eliminate the second potential issue by editing the phprc file for the version of PHP you are using on the domain.
e.g. upload_tmp_dir = "/home/[user]/tmp"

If file uploads are a critical component to your website then I’d suggest that you to stop relying on PHP to provide that functionality. It has never been a reliable option.

This was indeed the problem, a full upload_tmp_dir. Tech support stepped in and corrected my incorrect phprc edit attempt. Works well now. Thanks!

1 Like

β€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Žβ€Ž :+1:
β€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Žβ€β€β€Ž β€Ž

so there is just miss placed ehm…
i do have maybe similar problem, but i don’t know where to start…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.