【发布时间】:2017-04-24 17:00:04
【问题描述】:
我正在创建一个网站,在其管理面板上显示上传的新闻。
每篇文章都有一张图片和一个标题(还有描述,但我还没有实现)。
我的问题是,当我尝试发布(并用它上传图片)时,帖子已创建,但图片不存在。
上传者(php):
if (isset($_FILES['image'])) {
//this script
//connects to mysql database
//declares an array that contains table names (array name is db)
require_once("db.php");
//move file to the img folder
move_uploaded_file($_FILES['image']['tmp_name'], "img/" . $_FILES['image']['tmp_name']);
//upload the post to the database
$sql = "INSERT INTO `{$db["posts"]}` (`img`, `text`) VALUES ('img/{$_FILES['image']['tmp_name']}', '{$_POST["text"]}')";
if (!mysql_query($sql)) {
//display error message
}
}
表单(html):
<form action="post.php" method="POST" enctype="multipart/form-data">
<label>Image: </label><input type="file" name="image" />
<br />
<label>Text: </label><input type="text" name="text" />
<input type="submit" />
</form>
发布后我通过ftp查看文件,图片不存在。
【问题讨论】:
-
每次在新代码中使用the
mysql_数据库扩展this happens 时,它都已被弃用,并且已经存在多年,并且在 PHP7 中永远消失了。如果您只是学习 PHP,请花精力学习PDO或mysqli数据库扩展和预处理语句。 Start here -
将此粘贴到 php 文件
ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);的顶部并检查错误。
标签: php file web file-upload upload