Form试图处理没有't存在
Form tries to process image that doesn't exists
我正在建立一个博客。我的客户希望在必要时上传图像,但不是一直上传。我写了代码来做这件事,但我想我错过了一些东西。任何愿意帮助我调试的人都将不胜感激。
if (!in_array($file_ext,$allowed_ext)) {
$errors[] = strtoupper($file_ext).'''s is not an approved file extension. Please try again. Approved extensions are '.implode(',', $allowed_ext).'.';
}
上面的代码片段来自下面的页面,是不断被触发的错误。
if (empty($_POST) === false) {
$allowed_ext = array('jpg','jpeg','png');
if (isset($_FILES['image']) && !empty($_FILES['image'])) {
$file_name = $_FILES['image']['name'];
$file_ext = strtolower(end(explode('.', $file_name)));
$file_size = $_FILES['image']['size'];
$file_tmp = $_FILES['image']['tmp_name'];
if (!in_array($file_ext,$allowed_ext)) {
$errors[] = strtoupper($file_ext).'''s is not an approved file extension. Please try again. Approved extensions are '.implode(',', $allowed_ext).'.';
} else if ($file_size > 655360) {
$errors[] = 'File Size to large. Please try a different image';
}
}
if (empty($errors) === true) {
if (isset($_FILES['image']) === true && empty($_FILES['image']) === false) {
$file_name = md5($file_name).'.'.$file_ext;
move_uploaded_file($file_tmp, '../assets/images/'.$file_name);
$image_location = 'assets/images/'.$file_name;
}
$data = array(
'title' => $_POST['title'],
'category_id' => $_POST['category_id'],
'author_id' => $session_user_id,
'author_name' => $user_data['admin_name'],
'summary' => $_POST['summary'],
'contents' => nl2br($_POST['content']),
'image' => $image_location,
'date' => date("F j, Y"),
'time' => date("h:i a e")
);
echo 'Something Cool';
insertPost($db, $data);
header('Location: new.php?success');
}
}
echo '<div id="page-wrapper">';
echo '<div class="row">';
?>
<div class="col-lg-12">
<h1>Write a new post <small></small></h1>
<ol class="breadcrumb">
<li><a href="index.php" class="fa fa-dashboard"> Dashboard</a></li>
<li>Write A New Post</li>
</ol>
</div>
</div>
<?php
if (isset($_GET['success']) === true && empty($_GET['success']) === true) {
$success = '
<div class="alert alert-success">
<h4>Blog Post Successively Submitted</h4>
<p>Your blog post was successfully submitted to the database. Go check it out on the <a href="../index.php">homepage</a>!
</div>
';
echo $success;
} else {
if (empty($errors) === false) {
echo '<div class="alert alert-danger"><srong>After trying to process your request, the following errors occured:</strong>';
echo output_errors($errors);
echo '</div>';
}
?>
<form action="" method="post" enctype="multipart/form-data">
<div class="col-lg-6 col-sm-12">
<div class="form-group">
<label for="title">Title</label><br>
<input name="title" type="text" class="form-control" placeholder="Title of Post" required/>
</div>
<div class="form-group">
<label for="category_id">Category</label><br>
<select name="category_id" class="form-control">
<option value=""> --Select a Category-- </option>
<?php
$query = $db->prepare("SELECT category_id, category_name FROM catagories");
$query->execute();
while ($result = $query->fetch(PDO::FETCH_ASSOC)) {
echo '<option value="'.$result['category_id'].'">'.$result['category_name'].'</option>';
}
?>
</select>
</div>
<div class="form-group">
<label for="summary">Summary</label><br>
<textarea name="summary" class="form-control" rows="3" placeholder="Summary of your post. Keep it short and simple as this is what will be displayed on the homepage"></textarea>
</div>
<div class="form-group">
<label for="content">Content</label><br>
<textarea id="wysihtml5-textarea" class="textarea-wysiwyg form-control" rows="10" name="content" placeholder="Article Contents Go Here"></textarea>
</div>
<div class="form-group">
<label for="image">Image</label><br>
<input type="file" name="image" class="form-control" />
</div>
<div class="form-group">
<button class="btn btn-primary" type="submit">Submit Post</button> <button class="btn btn-danger" type="reset">Reset Form</button>
</div>
<?php } ?>
</form>
</div>
<div class="col-lg-6 col-sm-12">
<h3>Posting Guidelines and Restrictions</h3>:
<strong>Restrictions</strong>
<ul>
<li>Images extensions are restricted to JPG's, JPEG's, & PNG's</li>
<li>Image file sizes are limited to 5mb</li>
</ul>
<strong>Guidelines</strong>
<ul>
<li>All the fields are required. Submiting the form with any of the fields left empty will result in errors.</li>
<li>The summary should be kept to less then a single paragraph. It is what is displayed on the homepage.</li>
<li>I'm not really sure what else to put here. Email me if you have any questions.</li>
</ul>
</div><!-- /#page-wrapper -->
<?php
include 'includes/footer.php'; ?>
如果条件错误。您错误地验证了图像是否已上传。
更改
if (isset($_FILES['image']) && !empty($_FILES['image']))
至
if (isset($_FILES['image']) && $_FILES['image']['error'] == 0 && $_FILES['image']['size'] > 0)
同样,如果条件以类似的方式改变,你正在移动/保存图像。
相关文章:
- 正在将事件处理程序添加到不存在的类
- 是否存在Javascript Liferay Service库的文档?如何处理错误情况
- 如果文件不存在,fs.watch是否有错误处理程序
- 事件处理程序连接在哪里,以及它为什么存在
- InnerHTML创建问题,没有't加载顺序正确,可能存在处理问题
- Form试图处理没有't存在
- 是否存在添加两个相同事件处理程序的默认行为
- 如何让你的用户脚本处理那些没有'还不存在
- 处理 JavaScript 中不存在的变量
- Dropzone,如果存在错误,如何不处理队列
- 使用 on() 将事件处理程序添加到尚不存在的元素
- 使用 javascript 运行 Apache 服务器中存在的批处理文件
- 用Javascript处理TestComlete中的对象不存在错误
- 预防'内容嗅探'处理用户上传的图像时存在类型漏洞
- className的存在阻止调用onClick处理程序
- 检测Angular JavaScript承诺链中是否存在next处理程序
- 是否有一种方法来验证YouTube视频ID,同时处理CORS和不存在的API
- 批处理文件>Javascript的在有WinSCP祝辞检查文件是否存在
- 消息事件处理程序是否存在于窗口对象中
- 当页面加载时,我是否必须为不存在的类处理不同的单击事件