使用TP做附件上传部分,找了很多插件,最后用了uploadify插件,使用中有这样一个问题.
后台处理代码:
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,'/') . '/' . md5(time()).$_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 $targetFile;
} else {
echo 'Invalid file type.';
}
}
}
前台页面:
<form id="uploads">
<div id="queue"></div>
<input id="file_upload" name="file_upload" type="file" multiple="true">
<input type="submit" value="提交">
</form>
<script type="text/javascript">
<?php $timestamp = time();?>
$(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',
'onUploadSuccess' : function(file, data, response) {
★ alert(data);
}
});
});
</script>
噜噜哒
慕的地8271018