I see "No upload" after the file uploads successfully

Upon uploading a file from my web page, the dialog appears “No upload”, after the file uploads successfully. Any help with showing the correct message is appreciated.

    <?php
    foreach (array('video', 'audio') as $type) {
    if (isset($_FILES["${type}-blob"])) {

        $fileName = $_POST["${type}-filename"];

       $uploadDirectory = 'uploads/' . $fileName;

            // make sure that one can upload only allowed audio/video files
            $allowed = array(
                'webm',
                'wav',
                'mp4',
                'mov'
    );

    $extension = pathinfo($uploadDirectory, PATHINFO_EXTENSION);
    if (!$extension || empty($extension) || !in_array($extension, $allowed)) {
        echo 'Invalid file extension: '.$extension;
        return;
    }

    $new_filepath = "uploads/" . uniqid() . ".". $extension;

    if (!move_uploaded_file($_FILES["${type}-blob"]["tmp_name"], $new_filepath)) {

         echo (" problem moving uploaded file");
       }

    if(!file_exists($_FILES["${type}-blob"]["tmp_name"]) || 
    !is_uploaded_file($_FILES["${type}-blob"]["tmp_name"])) {
    echo 'No upload';
    }
    }
    }
    ?>

And here’s the corresponding js:

                         function uploadFile() {
                          // create FormData
                           var fileType = 'video'; // or "audio"
                           var fileName = 'vid.webm';
                           var formData = new FormData();
                           var request = new XMLHttpRequest;
                           formData.append(fileType + '-filename', fileName);
                           formData.append(fileType + '-blob', blob);
                          request.open("POST", "/save1.php");

                      request.onreadystatechange = function() {
    		 if(request.readyState==4) {
    		 alert(request.responseText);
    		 }
    		}

                request.send(formData);
           }

After you move the uploaded file, the temporary uploaded file doesn’t exist anymore. Why are you trying to test the temporary uploaded file after you have already moved it to a permanent file?

Your post method form processing code must -

  1. Detect if a post method form was submitted before referencing any of the form data.
  2. Detect if the $_FILES and/or $_POST arrays are empty. If the total size of the form data exceeds the post_max_size setting on the server, both the $_FILES and $_POST arrays will be empty. This condition commonly occurs when uploading files. To provide a good User eXperience (UX) you must detect this condition and set up a message for the user telling them that they tried to submit data that was too large. You would also log all the information about the problem so that you, the programmer/developer, will know it is occurring and you can take an action such as increasing the post_max_size setting and doing things like listing on the web page the maximum size of a file that someone can successfully upload.
  3. After you have determined that there is $_POST/$_FILES data, you must test the [‘error’] element in the $_FILES data to make sure that the upload was successful. For the upload errors that the user has control over, you need to set up a unique message telling the user what was wrong with the upload. For the errors that the user cannot do anything about, you need to set up a general failure message and then log all the information about the problem so that you, the programmer/developer, will know it is occurring and you can find and fix what’s causing the problem.
  4. Only after you know that there was a successfully uploaded file (no upload error), can you then reference the uploaded file information in order to validate things like the extension. Note: you must perform any comparisons on strings in a case-insensitive way, so that if a user sent a file with an extension of Mov, or MOV, or any other combination, that it will match your test for mov.
  5. After you have validated the successfully uploaded file information, you can try to move the temporary file to a permanent file. Moving an uploaded file will fail for two reasons, neither of which the user can correct - 1) the path/file doesn’t exist or 2) there’s a permission problem with the folder. These are programming/configuration mistakes. If they occur on a live web site, you would set up a generic failure message for the user and log all the information about the problem so that you, the programmer/developer, will know it is occurring and you can find and fix what’s causing the problem.

Lastly, doing this fileType + ‘-blob’ and this $_FILES["${type}-blob"] combining and deconstruction is unnecessary ‘data churn’, especially since you are hard-coding the type now to make this work at all. At the point of letting a user pick a video to upload, use a form field with the name ‘video’. At the point of letting a user pick an audio file to upload, use a form field with the name ‘audio’.

What does that do? You should create this variable then use it.
$type is already either video or audio. So, $filename=$type . “-blob” would create a valid filename.
Then, where you have braces, just us the variable $filename. Using braces inside a function inside a posted value can sometimes cause issues with the results. You can debug it by displaying what is being placed into the functions by: die("${type}-blob"); But, I doubt it is correct ! Or just remove the braces!

As PHDR said, if you want to check the extensions of input files, do it BEFORE you upload it.
Just check the posted values of the files before you allow it to upload…

Sponsor our Newsletter | Privacy Policy | Terms of Service