2017-06-16 209 views
0

这是一个简单的文件上传script.I只需要我上传的文件的名称,因为我需要在PHP部分上传的文件[连同其路径]将其插入到数据库中。 这是一个脚本。如何获得上传的文件名在PHP中,如果上传的文件使用AJAX jQuery文件上传

的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 

?> 
+1

你的问题与你从ajax发送和接收数据的方式相关。你必须创建一个成功的函数来接收你的Ajax数据。 –

+0

由于您使用的是Jquery Ajax来上传文件,因此您不会在$ _FILES变量中获取该文件。该文件将在$ _POST变量中发送一个二进制数据 您应该使用 file_put_contents()函数将内容写入文件。 E.G. 'file_put_contents('img.jpg',$ _POST ['file']); ' –

+0

我尝试了下面提到的php代码。在那个代码中我打印$ _POST.It也不起作用。 – Manasa

回答

1

对于执行sucess函数使用此代码。

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误解返回的数据。为了不存在此问题,请声明您希望接收的数据类型。

+0

是的写作成功后,我可以看到输出语句。我也得到路径 – Manasa

+0

非常感谢你 – Manasa

+0

@Manasa不客气。 –

1

我的事情你可以在你的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) 

这里$目标包含您可以存储在我们的文件路径D B。

+0

这也适用,因为它包含路径名称 – Manasa

+0

非常感谢你 – Manasa

+0

@Manasa很高兴听到我们的问题得到解决。 –