標題:
ajax upload image to php server
[打印本頁]
作者:
ray
時間:
2020-9-12 10:25
標題:
ajax upload image to php server
https://www.ucamc.com/e-learning ... p-ajax-image-upload
作者:
ray
時間:
2020-9-12 11:46
本帖最後由 ray 於 2020-9-12 11:59 編輯
<?php
if(is_array($_FILES)) {
if(is_uploaded_file($_FILES['userImage']['tmp_name'])) {
$sourcePath = $_FILES['userImage']['tmp_name'];
if(file_exists("images"))
mkdir("images");
$targetPath = "images/".date("Y-m-d_H_i_s").".".substr($_FILES['userImage']['name'],-3);//$_FILES['userImage']['name'];
if(move_uploaded_file($sourcePath,$targetPath)) {
?>
<img class="image-preview" src="/<?php echo $targetPath; ?>" class="upload-preview" />
<?php
}
}
}
?>
複製代碼
作者:
ray
時間:
2020-9-12 11:55
date_default_timezone_set("Asia/Taipei");
複製代碼
作者:
ray
時間:
2020-9-12 11:57
<form id="uploadForm" action="upload.php" method="post">
<label>Upload Image File:</label><br/>
<input name="userImage" type="file" class="inputFile" />
<input type="submit" value="Submit" class="btnSubmit" />
</form>
<div id="targetLayer"></div>
<script
src="https://code.jquery.com/jquery-3.5.1.js"
integrity="sha256-QWo7LDvxbWT2tbbQ97B53yJnYU3WhH/C8ycbRAkjPDc="
crossorigin="anonymous">
</script>
<script src="image.js"></script>
複製代碼
作者:
ray
時間:
2020-9-12 11:57
$(document).ready(function (e){
$("#uploadForm").on('submit',(function(e){
e.preventDefault();
$.ajax({
url: "upload.php",
type: "POST",
data: new FormData(this),
contentType: false,
cache: false,
processData:false,
success: function(data){
$("#targetLayer").html(data);
},
error: function(data){
$("#targetLayer").html(data);
}
});
}));
});
複製代碼
歡迎光臨 種子論壇 | 高雄市資訊培育協會學員討論區 (http://istak.org.tw/seed/)
Powered by Discuz! 7.2