我使用Bootstrap中的文件上传选项创建了一个图像上传器。我想将图像上传到单独的文件夹,并将路径名保留在数据库中。当我单击上传按钮时,代码不会显示任何错误消息,但它不起作用。你能帮我解决这个问题吗?我在这里附上了我的代码:
image_upload_form_ui.php
<form id="fileupload" class="form-horizontal" method="POST" action="phpscripts/test.php" enctype="multipart/form-data">
<div class="control-group "> <!-- start of image -->
<label class="control-label">Avatar</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on"><i class="icon-camera"></i></span>
<!-- file upload-->
<div class="fileupload fileupload-new" data-provides="fileupload">
<div class="fileupload-preview thumbnail" style="width: 200px; height: 150px;"><img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image"/></div>
<div>
<span class="btn btn-file"><span class="fileupload-new">Select image</span>
<span class="fileupload-exists">Change</span>
<input type="hidden" name="MAX_FILE_SIZE" value="204800" />
<input type="file" name="image" /></span>
<a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Remove</a>
</div>
</div>
<!--end of file upload -->
</div>
</div>
</div><!-- end of image -->
<div class="control-group"> <!-- start of buttons -->
<label class="control-label"></label>
<div class="controls">
<button type="submit" class="btn btn-primary" >Upload</button>
</div>
</div> <!-- end of buttons -->
</form>
这是我的test.php
<?php
include "dbConnect.php";
//connect to the database
dbConnect();
$path = "uploads/";
$valid_formats = array("jpg","jpeg", "png", "gif", "bmp");
if(isset($_POST['image']))
{echo "1";
$name = $_FILES['image']['name'];
$size = $_FILES['image']['size'];
if(strlen($name))
{
list($txt, $ext) = explode(".", $name);
if(in_array($ext,$valid_formats))
{
if($size<(2048*2048))
{
$actual_image_name = time().substr(str_replace(" ", "_", $txt), 5).".".$ext;
$tmp = $_FILES['image']['tmp_name'];
if(move_uploaded_file($tmp, $path.$actual_image_name))
{
mysql_query("UPDATE tutor SET avatar='$actual_image_name' WHERE userName='Isuru'");
echo "Hari";
}else
echo die(mysql_error());
}else
echo "exceed the file size";
}else
echo "Not a valid format";
}else
echo "no file is selected";
}
?>
答案 0 :(得分:0)
test.php
中的代码正在检查$_POST
变量以查看是否通过表单提交了文件,但您可能希望检查$_FILES
。有关详细信息,请查看PHP documentation on file uploads。
尝试更改:
if(isset($_POST['image']))
到
if(isset($_FILES['image']))