我已经创建了一个表单,允许客户端创建博客。我希望表单提交到数据库和上传图像到服务器时提交。到目前为止,所有的信息都到达了数据库,但图像没有上传到服务器。
我猜我想问的是,表单是否可以同时执行两个功能,这是如何实现的?谢谢。
我的表单代码如下。
<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>
发布于 2014-08-09 05:56:00
您的<form>
标记中缺少enctype="multipart/form-data"
属性。没有它,文件上传将无法工作。
PHP :另外,您的<input type="file" ...>
字段被分配了名称blog_image
,但是在您的PHP代码中,您尝试获取一个由名称myfile
标识的上传文件。
用错误消息填充<input type="file" ...>
的value
属性也不是最好的主意。事实上,你根本不应该弄乱这个属性。最好在<input>
标签之前或之后显示任何错误消息。
发布于 2014-08-09 06:01:16
打开错误报告,看看PHP是否抛出任何错误或警告。我建议使用以下方法打开错误报告:
error_reporting(-1);
ini_set('display_errors', 'On');
另外,检查您的文件上传路径是否正确。
请确保enctype="multipart/ form - data“属性存在于您的also中,该属性指定在提交到服务器时表单中的数据应如何编码。
https://stackoverflow.com/questions/25212792
复制相似问题