有一个类似的问题,但说实话,我仍然不明白它是如何运作的。
这是代码:
<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将其上传到服务器上?
答案 0 :(得分:1)
在此代码周围使用带enctype的标签作为multipart / form-data来发布文件。
答案 1 :(得分:0)
首先,您需要通过将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处理图片上传。