Delete the temp file, if it is invalid

Thanks again very much for helping me resolve this.
In my original posted code, the error messages and NoInfoAvailable.png.were working successfully. If we don’t “set them(errors) to a variable” (#8), and just add ‘move only approved format/size files from temp to /upload’, do you think that might be a solution?

Wow, it really depends on what you wish to do.
If you’ve added

if(isset($error)){echo $error;}

after processing like in POST#18

And if you Remove

$procede = false;

from the processing section so the next template loads, do the error messages still show?

I would continue to set those messages to the error variable as that is used for the final move/copy condition.

What exactly is the “Solution” you would like to see?

Thanks again for your reply. Much appreciated.
When I add
if(isset($error)){echo $error;}
and change false to true,
I see “Error: Invalid File Name” when an incorrect file type is Submitted.
However, if I proceed anyway, with uploading a video, and select the file link (next to the uploaded video), the file that appears with the video is NoInfoAvailable.png. That’s a good outcome.

When I Submit without choosing a file to upload, I see “Error: Unable to upload your file”, and I proceed to upload a video, and select the file link (next to the uploaded video), I see no file, but I see the script’s “Access Forbidden” message.

The outcome I’d be grateful to see there is when I Submit without choosing a file to upload, that “Error: Unable to upload your file” message doesn’t appear, and then proceed with uploading a video, and select the file link (next to the video), the file that appears with the video is NoInfoAvailable.png

Thanks again for all of your assistance here. Any help with the ‘not choosing a file = NoInfoAvailable.png appearing’ scenario will be greatly appreciated.

OK, then it seems we need to SET that first error variable so the move/copy code works but without a message.

EDITED: Please use this copy, not version that may have been sent by email.

if (isset($_POST['form_submitted'])):

    // $randomString needed regardless of passing tests so put outside error condition
    $randomString = time();

    if((isset($_FILES) && $_FILES['file']['error'] != 0) || !isset($_FILES)){

        //Unable to upload file to temp
        //Set variable for final move/copy condtion with no message
        $error = '';

    }else{

        $allowedExts = array("doc", "docx", "gif", "jpeg", "jpg", "txt", "rtf", "pdf", "png", "txt");
        $temp = explode(".", $_FILES['file']['name']);
        $extension = strtolower( end($temp) );

        if(!in_array($extension,$allowedExts)){

            $error = '<div id="errorMessage">Error: Invalid File Name </div>';

        }elseif($_FILES['file']['size'] >= 100000){

            $error = '<div class="errorMessage1">Error: Image File Size Exceeds 100 KB Limit </div>';

        }         
    }

        if(!isset($error)){
            $uploadedFile = $_FILES['file']['tmp_name'];
            $thumbnail = $_SESSION['user_id'] . '-' . $randomString . "." . $extension;
            move_uploaded_file($uploadedFile, "upload/" . $thumbnail);
        }else{
            //Make sure NoInfo image has png extension
            $thumbnail = $_SESSION['user_id'] . '-' . $randomString . ".png";
            copy("upload/NoInfoAvailable.png", "upload/" . $thumbnail);
        }
            $_SESSION['thumbnail'] = $thumbnail;
            $file_location = '<a href="http://www....com/upload/' . $thumbnail . '">' . $thumbnail . '</a>';

endif; 
if(isset($error)){echo $error;}

Thank you Drummin for all of you help it works successfully. Much thanks again.

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