我创建了一个允许客户端创建博客的表单。我希望表单提交到数据库和上传图像到服务器时提交。到目前为止,所有的信息得到数据库,但图像没有上传到服务器。
我想我要问的是,表单是否有可能同时执行两个功能,这是如何完成的?谢谢。
我的表单代码如下。
<form name="my-form" action="<?php echo $editFormAction; ?>" method="POST" id="my-form" class="my-form">
<fieldset>
<section>
<label class="label">Blog Title <strong class="red-font">50 characters MAX with spaces</strong></label>
<label class="input">
<i class="icon-append fa fa-tag"></i>
<input type="text" name="title" id="title">
</label>
</section>
<section>
<label class="label">descrition <strong class="red-font">150 characters MAX with spaces</strong></label>
<label class="input">
<i class="icon-append fa fa-edit"></i>
<input type="4" name="desc" id="desc">
</label>
</section>
<section>
<label class="label">Keywords <strong class="red-font">6-8 groups of key words</strong></label>
<label class="input">
<i class="icon-append fa fa-key"></i>
<input type="4" name="keywords" id="keywords">
</label>
</section>
<section>
<label class="label">Select A Category</label>
<label class="select" id="category">
<select name="category">
<?php
do {
?>
<option value="<?php echo $row_rsCategory['category']?>"<?php if (!(strcmp($row_rsCategory['category'], $row_rsCategory['category']))) {echo "selected="selected"";} ?>><?php echo $row_rsCategory['category']?></option>
<?php
} while ($row_rsCategory = mysql_fetch_assoc($rsCategory));
$rows = mysql_num_rows($rsCategory);
if($rows > 0) {
mysql_data_seek($rsCategory, 0);
$row_rsCategory = mysql_fetch_assoc($rsCategory);
}
?>
</select>
<i></i>
</label>
</section>
<section>
<label class="label">Add Blog Image </label>
<input type="file" name="blog_image" value="<?php
//Properties of Image Upload
$name = $_FILES["myfile"] ["name"];
$type = $_FILES["myfile"] ["type"];
$size = $_FILES["myfile"] ["size"];
$temp = $_FILES["myfile"] ["tmp_name"];
$error = $_FILES["myfile"] ["error"];
if ($error > 0)
die ("Something went wrong. Please upload your image again");
else
{
move_uploaded_file($temp,"../../images/uploads/".$name);
}
?>">
</label>
</section>
<section>
<label class="label">Type Blog</label>
<label class="textarea">
<!--<i class="icon-append fa fa-edit"></i>-->
<textarea rows="30" name="blog_content" id="blog_content"></textarea>
</label>
</section>
<section>
<label class="label"><strong class="red-font">Publish to Web?</strong></label>
<label class="select" id="publish">
<select name="publish">
<option value="No" <?php if (!(strcmp("No", $row_rsBlogs['publish']))) {echo "selected="selected"";} ?>>No</option>
<option value="Yes" <?php if (!(strcmp("Yes", $row_rsBlogs['publish']))) {echo "selected="selected"";} ?>>Yes</option>
</select>
<i></i>
</label>
</section>
<footer>
<button type="submit" class="button">Add Blog </button>
</footer>
<input name="id" type="hidden" value="">
<input name="author" type="hidden" value="<?php echo $row_rsAdmin['name']; ?>">
</fieldset>
<input type="hidden" name="MM_insert" value="my-form">
</form>
您在<form>
标记中缺少enctype="multipart/form-data"
属性。如果没有它,文件上传将无法工作。
编辑:另外,您的<input type="file" ...>
字段已被分配名称blog_image
,但在您的PHP代码中,您尝试抓取由名称myfile
标识的上传文件。
用错误消息填充<input type="file" ...>
的value
属性也不是最好的主意。事实上,您根本不应该乱动这个属性。最好在<input>
标记之前或之后显示任何错误消息。
如果还没有打开错误报告,看看PHP是否会抛出任何错误或警告。我建议使用以下命令来打开错误报告:
error_reporting(-1);
ini_set('display_errors', 'On');
同时,检查你的文件上传路径是否正确,
请确保enctype="multipart/form-data"属性存在于您的also中,该属性指定提交给服务器时表单中的数据应如何编码