当涉及到实际保存上传文件到服务器,在服务器端上传脚本将在后端处理所有的工作。这里是一个比特的信息,应该帮助创建一个自定义的服务器端上传脚本在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方法
下一篇:无
