服务器端上传脚本是用于上传文件的后台程序。下面是基于PHP自定义服务器端上传脚本的一些信息。
传递额外的数据传递到服务器
根据method方式(post或get),额外的数据作为查询字符串附加到上传参数中,或通过参数formdata选项传送。服务器端通过$_POST或$_GET得到formData。
初始化Uploadify:
$('#file_upload').uploadify({
// Some options
'method' : 'post',
'formData' : { 'someKey' : 'someValue' }
});
在服务器端脚本中:
// Set $someVar to 'someValue'
$someVar = $_POST['someKey'];
如果你希望保存不同的文件名,这是非常有用的。如果你想在上传开始前在页面上设置些信息,最好是在onUploadStart事件中使用settings方法在上传前设置好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.';
}
}
在onUploadSuccess中接收返回的数据:
$('#file_upload').uploadify({
// Some options
'onUploadSuccess' : function(file, data, response) {
alert('The file was saved to: ' + data);
}
});