Search code examples
phpjavascriptjqueryuploadify

Upload video using uploadify latest version


I want to upload video using uploadify newest version, but I failed.

My code:

HTML:

<form  enctype="multipart/form-data" class="form-part" action="form.php" method="post">
  <input  class="uplodify" id="file_upload" type="file"/>
</form>

jQuery:

$('#file_upload').uploadify({
        swf: 'uploadify/uploadify.swf',
        uploader: 'uploadify/uploadify.php',
        multi: true,
        //queueSizeLimit: 3,
        fileExt: '*.jpg;*.gif;*.png;*.mp4;*.mp3;*.avi;*.wmv;*.flv;',
        auto: true,
        onUploadStart: function(file) {
            console.log(file);
        },
        onUploadComplete: function(event, ID, fileObj, response, data) {

        },
        onUploadError : function(file, errorCode, errorMsg, errorString) {
            alert('The file ' + file.name + ' could not be uploaded: ' + errorString);
        },
        onUploadSuccess : function(file, data, response) {
            console.log('The file ' + file.name + ' was successfully uploaded with a response of ' + response + ':' + data);
        }
    });

PHP:

error_reporting(0);
$targetFolder = '/uploads'; // Relative to the root

if (!empty($_FILES)) {
    $tempFile = $_FILES['Filedata']['tmp_name'];
    $targetPath = dirname(dirname(__FILE__)) . $targetFolder;
    $targetFile = rtrim($targetPath,'/') . '/' . $_FILES['Filedata']['name'];

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png', 'mp3', 'mp4', 'avi', 'flv', 'wmv'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);
    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);
        echo '1';
    } else {
        echo 'Invalid file type.';
    }
}

NOTE: No error in the console

onUploadSuccess() log is : The file easyhtm-wp.FLV was successfully uploaded with a response of true:Invalid file type.

Just immediate after this line a warning shown in console as:

Please help me with this.

Thanks.


Solution

  • You're comparing the extensions case-sensitive. FLV is not the same as flv. Convert the file's extension to lowercase before comparison.

    if( in_array( strtolower( $fileParts['extension'] ), $fileTypes ) ) {
        ...