jquery uploadify修改上传的文件名和显示

时间:2023-03-09 00:07:22
jquery uploadify修改上传的文件名和显示

如果觉得看文章太麻烦,可以直接看参考:http://*.com/questions/7707687/jquery-uploadify-change-file-name-as-it-is-being-uploaded

贴上jquery uploadify官网地址:http://www.uploadify.com/

哎呀,纠结了一上午,我说怎么file.name怎么都是我上传的名字,后来一查,我的天,原来是这个,data,我一直用的是file.name来显示的,才发现是用data来获取新的文件名称

'onUploadSuccess' : function(file, data, response) {
alert('Renamed file name is - ' + data);
}

我用的是thinkphp,所以修改的文件,麻烦哟,我的uploadify的文件路径是放在admin/Tpl/Index/uploadify这个路径下的,所以我的文件就变成了

uploadify.php

<?php
/*
Uploadify
Copyright (c) 2012 Reactive Apps, Ronnie Garcia
Released under the MIT License <http://www.opensource.org/licenses/mit-license.php>
*/ // Define a destination
//$targetFolder = 'stStone/Tpl/images/'; // Relative to the root $verifyToken = md5('unique_salt' . $_POST['timestamp']); if (!empty($_FILES) && $_POST['token'] == $verifyToken) {
$tempFile = $_FILES['Filedata']['tmp_name'];
   //由于是要前台显示,所以用了很多个dirname()
$targetPath = dirname(dirname(dirname(dirname(dirname(__FILE__))))).'/Tpl/Index/images/';
//获取文件的一些信息
$fileParts = pathinfo($_FILES['Filedata']['name']);
//图片名称规则是 当前时间的时分秒.类型 如:20131221030820.jpg
$filenames = date("YmdHis").".".$fileParts['extension'];
//图片的完整路径咯
$targetFile = rtrim($targetPath,'/') . '/' .$filenames;
// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions if (in_array($fileParts['extension'],$fileTypes)) {
if(!file_exists($targetFile)){
move_uploaded_file($tempFile,$targetFile);
chmod($targetFile,0777);
}else{
unlink($targetFile);
move_uploaded_file($tempFile,$targetFile);
chmod($targetFile,0777);
}
//必须得返回这个图片名称哦
echo $filenames;
} else {
echo 'Invalid file type.';
}
}
?>

页面调用方法:

<?php $timestamp = strtotime(date("YmdHis")); ?>
jQuery('#scroll_upload').uploadify({
'multi':false,
'formData' : {
'timestamp' : '<?php echo $timestamp;?>',
'token' : '<?php echo md5('unique_salt' . $timestamp);?>'
},
'swf':'<?php echo APP_PATH?>Tpl/Index/uploadify/uploadify.swf',
'uploader': '<?php echo APP_PATH?>Tpl/Index/uploadify/uploadify.php',
'buttonText':'上传图片',
'auto': false,
'multi': false,
'onUploadSuccess' : function(file, data, response) {
          alert("新图片名字是"+data);
}
});

参数说明:

#scroll_upload 是一个input type=file的按钮的id
$timestamp 是当前时间的时间戳
APP_PATH是当前项目的根目录,如后台使用,就是"./admin/"这样的(这里我也不是很懂啊,个人见解)