2015-10-07 2 views
0

Привет, я новичок в jquery. я пытаюсь переименовать файл в upload..but я не в состоянии сделать этоКак переименовать файл в файл jquery upload

Кодекс я использую для файла загрузить

$(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

мне удалось переименовать имя файла перед загрузкой. ..pepole down проголосовал за это, несмотря на помощь. Изменился я сделал, чтобы мой код может быть полезным для кого-то другого

$(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{ 
} 
    } 
    }); 

изменения я сделал в PHP файле

$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"; 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^