2015-10-07 144 views
0

嗨,我是jquery的新手。我想要重命名upload..but文件我不能這樣做如何在jquery文件上傳中重命名文件

我使用文件中的代碼上傳

$(function() { 
var btnUpload=$('#upload'); 

var status=$('#status'); 
    new AjaxUpload(btnUpload, { 
action: 'upload-file.php', 
name: 'uploadfile', 
    onSubmit: function(finalname, ext){ 
if (! (ext && /^(pdf|doc|docx|xls|xlsx|text|)$/.test(ext))){ 
status.text('Only pdf, xls,doc,docs,xlsx and text files are allowed'); 
return false; 
} 
status.text('Uploading...'); 
}, 
onComplete: function(finalname, response){ 
status.text(''); 
if(response==="success"){ 
$('#head').val(finalname); 
} else{ 
status.text('Upload Failed'); 
} 
    } 
}); 

PHP代碼是

$uploaddir = 'uploads/files/'; 
$file = $uploaddir . basename($_FILES['uploadfile']['name']); 
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
    echo "success"; 
} else { 

    echo "error"; 
} 

的Html

<div id="upload" ><span>Browse<span></div><span id="status" ></span> 
<input type="text" id="head" name="head" value=""> 

我能夠用PHP文件重命名......但不是在jQuery代碼它給我錯誤的文件名好心幫

+0

人們來這裏不是爲了幫助 – RItika

回答

1

我設法重新命名之前上傳的文件名。儘管有幫助,但是這個極低的投票表決。該改變了我對我的代碼,這樣或許是有用的爲別人我在PHP文件提出

$(function() { 
    var btnUpload=$('#upload'); 
var status=$('#status'); 
var mm=Math.random().toString(36).substring(7) + new Date().getTime(); //to add new name of file 
new AjaxUpload(btnUpload, { 
action: 'upload-file.php?name='+mm, // gave a action to php file so i can use the same name 
name: 'uploadfile', 
onSubmit: function(file, ext){ 
if (! (ext && /^(pdf|doc|docx|xls|xlsx|text|)$/.test(ext))){ 
status.text('Only pdf, xls,doc,docs,xlsx and text files are allowed'); 
return false; 
} 
status.text('Uploading...'); 
}, 
onComplete: function(file, response){ 
var fileExtension = '.' + file.split('.').pop(); //got the file extestion 
var outputfile = file.substr(0, file.lastIndexOf('.')) || file; //got the file name 
var spaceremoved=outputfile.replace(/\s/g, '');//removed the space from file 
var filename=mm+spaceremoved+fileExtension; //merged all to one 
    status.text(''); 
if(response==="success"){ 
    $('#head').val(filename); 
    } else{ 
} 
    } 
    }); 

變化

$uploaddir = 'uploads/files/'; 
if(isset($_GET['name'])){ 
$filena=$_GET['name']; 
} 
$basename=$filena.basename($_FILES['uploadfile']['name']);//merged the name 
$finalna=preg_replace('/\s+/', '', $basename);//removed the space 
$file = $uploaddir .$finalna;// merged to final 
if (move_uploaded_file($_FILES['uploadfile']['tmp_name'], $file)) { 
    echo "success"; 
} else { 
    echo "error"; 
}