做php项目的附件上传,使用了jquery uploadify插件.
前台代码:
......
$(function() {
$('#file_upload').uploadify({
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>'
},
'swf' : '/oschina/Addons_Project/Addons/Public/Org/uploadify/uploadify.swf',
'uploader' : 'http://localhost/oschina/Addons_Project/Addons/index.php/demo/index/uploads'
});
});
后台代码:
public function index(){
dump($_FILES);
$this->display();
}
//附件文件上传
public function uploads(){
//date("Y/m/d")和date("Y-m-d")生成路径的不同,/直接被转换成了目录层级
$targetFolder = '/oschina/Addons_Project/Addons/Public/Uploads/Files/'.date("Y/m/d"); // Relative to the root
$verifyToken = md5('unique_salt' . $_POST['timestamp']);
if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $targetFolder;
// 如果没有文件夹则进行创建,默认代码中没有
is_dir($targetPath)||mkdir($targetPath,0777,true);
$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 '1';
} else {
echo 'Invalid file type.';
}
}
}
文件上传没问题,我想在index方法中,将前台表单提交的数据(包括文件上传的表单)提交到数据库,但是我dump($_FILES);一直是empty.怎么样才能获取uploadify上传的文件信息,并保存到数据库中,包括路径信息.