鍍金池/ 教程/ HTML5/ uploadify自定義服務器端上傳腳本
uploadify自定義服務器端上傳腳本
Uploadify教程
使用Uploadify方法
實現(xiàn)Uploadify

uploadify自定義服務器端上傳腳本

當涉及到實際保存上傳文件到服務器,在服務器端上傳腳本將在后端處理所有的工作。這里是一個比特的信息,應該幫助創(chuàng)建一個自定義的服務器端上傳腳本在PHP中。

通過額外的數(shù)據(jù)到服務器端腳本

額外的數(shù)據(jù)可以傳遞到腳本無論是作為查詢字符串附加到上傳選項,或通過參數(shù)formdata選項。根據(jù)所設(shè)定的方法選項('POST'或'GET'),可以檢索使用$_POST或$_GET數(shù)組中的服務器端腳本發(fā)送的參數(shù)formdata選項的信息。

當初始化Uploadify:

$('#file_upload').uploadify({
    // Some options
    'method'   : 'post',
    'formData' : { 'someKey' : 'someValue' }
});


在服務器端腳本:

// Set $someVar to 'someValue'
$someVar = $_POST['someKey'];


將上面的文件保存為不同名稱的文件。 如果想傳遞在頁面上設(shè)置的上傳開始前的信息,那么最好使用 settings 方法在 onUploadStart 事件,這樣的參數(shù) formdata 在文件上傳前正確設(shè)置。
 

從服務器端腳本返回的數(shù)據(jù)

在uploadify.php腳本返回的數(shù)據(jù)可通過 onUploadSuccess 事件作為第二個參數(shù)(數(shù)據(jù))進行訪問。

從uploadify.php文件返回文件名:

$targetFolder = '/uploads'; // Relative to the root

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

    // Validate the file type
    $fileTypes = array('jpg','jpeg','gif','png'); // File extensions
    $fileParts = pathinfo($_FILES['Filedata']['name']);

    if (in_array($fileParts['extension'],$fileTypes)) {
        move_uploaded_file($tempFile,$targetFile);
        echo $targetFolder . '/' . $_FILES['Filedata']['name'];
    } else {
        echo 'Invalid file type.';
    }
}

上傳返回調(diào)用:
 

$('#file_upload').uploadify({
    // Some options
    'onUploadSuccess' : function(file, data, response) {
        alert('The file was saved to: ' + data);
    }
});