[英]How to get the Uploaded file name in php if the file is uploaded by using ajax jquery file upload
這是一個簡單的文件上傳腳本。我只需要在php部分中上傳的文件名即可,因為我需要該上傳文件(及其路徑)將其插入數據庫。 這是一個腳本。
index.html
<form enctype="multipart/form-data" id="form1">
<input name="file" type="file" id="id1" />
<input type="button" value="Upload" />
</form>
<progress></progress>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script>
$(':file').on('change', function() {
var file = this.files[0];
if (file.size > 10024000000) {
alert('max upload size is 1k')
}
// Also see .name, .type
});
</script>
<script>
$(':button').on('click', function() {
$.ajax({
// Your server script to process the upload
url: 'upload.php',
type: 'POST',
// Form data
data: new FormData($('form')[0]),
// Tell jQuery not to process data or worry about content-type
// You *must* include these options!
cache: false,
contentType: false,
processData: false,
// Custom XMLHttpRequest
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
// For handling the progress of the upload
myXhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
$('progress').attr({
value: e.loaded,
max: e.total,
});
}
} , false);
}
return myXhr;
},
});
});
</script>
upload.php
<?php
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$_FILES['file']['name']);
$name = $_FILES['file']['tmp_name'];
echo $name;//i tried this but i know as i uploaded file using ajax it will not work
?>
我想您可以在您的php代碼中獲取路徑做這樣的事情
$info = pathinfo($_FILES['userFile']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = "newname.".rand(0,999).$ext;
$target = 'images/'.$newname;
move_uploaded_file( $_FILES['userFile']['tmp_name'], $target)
$ target包含您可以存儲在數據庫中的文件路徑。
為了實現成功功能,請使用此代碼。
JS:
$(':button').on('click', function() {
$.ajax({
// Your server script to process the upload
url: 'upload.php',
type: 'POST',
// Form data
data: new FormData($('form')[0]),
// Tell jQuery not to process data or worry about content-type
// You *must* include these options!
cache: false,
contentType: false,
processData: false,
// Custom XMLHttpRequest
xhr: function() {
var myXhr = $.ajaxSettings.xhr();
if (myXhr.upload) {
// For handling the progress of the upload
myXhr.upload.addEventListener('progress', function(e) {
if (e.lengthComputable) {
$('progress').attr({
value: e.loaded,
max: e.total,
});
}
} , false);
}
return myXhr;
},
success: function(data)
{
alert(data);//Note that sometimes ajax misinterprets the data that is returned. To not have this problem, declare the type of data you expect to receive.
}
});
});
PHP:
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$_FILES['file']['name']);
$name = $_FILES['file']['tmp_name'];
echo $name;
如果要發送回多個值,請使用Json編碼發送回數據。
代碼如下:
PHP:
move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/'.$_FILES['file']['name']);
$name = $_FILES['file']['tmp_name'];
$path = 'uploads/'.$_FILES['file']['name'];
echo json_encode(array(
'name'=> $name,
'path'=> $path
));
JS:
...
success: function(data){
alert("Name: "+data.name);
alert("Path: "+data.path);
}
請注意,有時ajax會誤解返回的數據。 為避免出現此問題,請聲明您希望接收的數據類型。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.