當涉及到實際保存上傳檔到伺服器,在伺服器端上傳腳本將在後端處理所有的工作。這裏是一個比特的資訊,應該幫助創建一個自定義的伺服器端上傳腳本在PHP中。
通過額外的數據到伺服器端腳本
額外的數據可以傳遞到腳本無論是作為查詢字串附加到上傳選項,或通過參數formdata選項。根據所設定的方法選項('POST'或'GET'),可以檢索使用$_POST或$_GET數組中的伺服器端腳本發送的參數formdata選項的資訊。
當初始化Uploadify:
$('#file_upload').uploadify({
// Some options
'method' : 'post',
'formData' : { 'someKey' : 'someValue' }
});
在伺服器端腳本:
// Set $someVar to 'someValue'
$someVar = $_POST['someKey'];
將上面的檔保存為不同名稱的檔。 如果想傳遞在頁面上設置的上傳開始前的資訊,那麼最好使用 settings 方法在 onUploadStart 事件,這樣的參數 formdata 在檔上傳前正確設置。
從伺服器端腳本返回的數據
在uploadify.php腳本返回的數據可通過 onUploadSuccess 事件作為第二個參數(數據)進行訪問。
從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.';
}
}
上傳返回調用:
$('#file_upload').uploadify({
// Some options
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
}
});
上一篇:
使用Uploadify方法
下一篇:無
