[英]how to get the image using Jasny image upload and post in PHP
有一個類似的問題,但說實話,我仍然不明白它是如何工作的。
這是代碼:
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;">
</div>
<div>
<span class="btn btn-default btn-file"><span class="fileupload-new">Select image</span>
<span class="fileupload-exists">Change</span><input type="file" name="myimage" accept="image/*"></span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
我的問題是:1。)我將如何使用$ _POST []或$ _FILES []傳遞圖像?
2.)當用戶點擊“選擇圖像”和“更改”時, <input type="file" name="myimage" accept="image/*">
處理?
3.)或者我可以用什么方式傳遞圖像並使用PHP將其上傳到服務器上?
使用帶有enctype的標記作為此代碼周圍的multipart / form-data來發布您的文件。
首先,您需要通過將fileupload更改為fileinput來將上面的代碼版本更改為最新版本。 否則你將無法顯示。
這是一個簡單的例子:
<form method='post' action='upload.php' enctype='multipart/form-data'>
<div class="fileinput fileinput-new" data-provides="fileinput">
<div class="fileinput-new thumbnail" style="width: 277px; height: 220px;">
<img src="../images/default_image.png" alt="...">
</div>
<div class="fileinput-preview fileinput-exists thumbnail" style="max-width: 277px; max-height: 220px;"></div>
<div>
<span class="btn btn-default btn-file"><span class="fileinput-new">Select image</span><span class="fileinput-exists">Change</span><input type="file" name="file[]"></span>
<a href="#" class="btn btn-default fileinput-exists" data-dismiss="fileinput">Remove</a>
</div>
</div>
<input type='submit' name='submit' value='SUBMIT' />
</form>
因此,當您提交表單時,這將處理upload.php。 因此,要將圖像上傳到服務器,您需要在此文件中編寫代碼來處理它。 一個簡單的代碼可能是:
<?php
if(isset($_POST['submit'])){ // When you click submit the form
if(isset($_FILES['file']['tmp_name'])){ //check if there is a file has been submitted.
// count and loop through array of files(if multiple images are uploaded)
for($i=0; $i < count($_FILES['file']['tmp_name']);$i++)
{
// check if there is a file in the array
if(!is_uploaded_file($_FILES['file']['tmp_name'][$i]))
{
echo 'No file is uploaded';
}
else{
Code to handle image upload and store image path to array
}
All images path are in array, you need to serialize() it and save to database as normal
}
?>
希望您可以了解如何使用Jasny Fileinput來處理圖像上傳。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.