我必须在服务器上传图片。我有这个PHP代码:
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
if(isset($_FILES['photo'])){
//Getting actual file name
$name = $_FILES['photo']['name'];
//Getting temporary file name stored in php tmp folder
$tmp_name = $_FILES['photo']['tmp_name'];
//Path to store files on server
$path = 'images/testimonial/';
//checking file available or not
if(!empty($name)){
//Moving file to temporary location to upload path
move_uploaded_file($tmp_name,$path.$name);
//Displaying success message
echo "Upload successfully";
}else{
//If file not selected displaying a message to choose a file
echo "Please choose a file";
}
}
}
?>
我的AJAX代码:
$('#uploadImage').submit(function(e){
//Preventing the default behavior of the form
//Because of this line the form will do nothing i.e will not refresh or redirect the page
e.preventDefault();
//Creating an ajax method
$.ajax({
//Getting the url of the uploadphp from action attr of form
//this means currently selected element which is our form
url: $(this).attr('action'),
//For file upload we use post requestd
type: "POST",
//Creating data from form
data: new FormData(this),
//Setting these to false because we are sending a multipart request
contentType: false,
cache: false,
processData: false,
success: function(data){
//If the request is successfull we will get the scripts output in data variable
//Showing the result in our html element
$('#msg').html(data);
},
error: function(){}
});
});
我的HTML代码:
<form id='uploadImage' action='ajaxupload.php' method='post' enctype='multipart/form-data'>
<input id="im2" type="file" name='photo' class="dropify-fr" data-default-file="" data-max-file-size="200K" />
<button>Upload</button>
</form>
它在本地工作正常,并在特定文件夹中添加图像,但在服务器上我得到&#34;上传成功&#34;没有获取文件夹中的图像。请问有什么问题?