[英]Error in my php file I can't seem to find? Perhaps caused by ajax?
首先,我剛接觸過ajax和jquery,我必須承認它們看起來很有趣。 但是我花了很多時間弄清楚為什么上傳img-s的結果總是一樣的。我的想法是創建一個頁面,在其中可以導入具有大小和擴展名等限制的多個圖像,但由於某些原因,錯誤只是不打印。 無論結果如何,它只會打印alert(“ Image Uploaded”)。 這是我的html的ajax部分:
<script>
$(document).ready(function(){
$('#uploadForm').on('submit', function(e){
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
processData:false,
success: function(data)
{
$("#gallery").html(data);
alert("Image Uploaded");
}
});
});
});
</script>
這是我只是在html文件中調用的upload.php:
<?php
//upload.php
$output = '';
if(is_array($_FILES))
{
foreach ($_FILES['files']['name'] as $name => $value)
{
$totalImageIndex = ($name+1);
$file_name = explode(".", $_FILES['files']['name'][$name]);
$file_size = $_FILES['files']['size'][$name];
$allowed_ext = array("png", "gif");
if(in_array($file_name[1], $allowed_ext))
{
if($totalImageIndex <= 5 ) {
// 2 MB is 2097152 bytes.
if($file_size < 2097152){
$new_name = $totalImageIndex . '.' . $file_name[1];
$sourcePath = $_FILES['files']['tmp_name'][$name];
$targetPath = "slike/".$new_name;
if(move_uploaded_file($sourcePath, $targetPath))
{
$output .= '<img src="'.$targetPath.'" width="150px" height="180px" />';
}
}
else { continue ; }
} else echo 'file is too big!';
} else echo 'wrong file format!';
}
echo $output;}
?>
任何想法或建議都會被采納,在此先感謝您!
在成功的Ajax中,您將獲得在php文件中回顯的數據,因此,無論是獲取圖像還是返回錯誤,您都可以簡單地alert(data); 僅查看您在ajax代碼中得到的內容,您將向警報(圖像上浮)發出警報(當您收到錯誤數據時將始終調用該警報),因為成功只需刪除該警報並僅執行alert(data),您將看到錯誤如果有的話
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.