我正在创建一个网站,显示在其管理面板上上传的新闻。
每篇文章都有一个图像和一个标题(和描述,但我还没有实现它)。
我的问题是,当我尝试发布(并上传图片)后创建,但图像不存在。无法复制上传的文件(php)
上传(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检查文件发布后,图像不存在。
每次您使用[mysql_'](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) 在新代码中的数据库扩展 * * [发生这种情况](https://media.giphy.com/media/kg9t6wEQKV7u8/giphy.gif)** 它已被弃用,并已使用多年,并在PHP7中永远消失。 如果您只是学习PHP,花费精力学习'PDO'或'mysqli'数据库扩展和准备语句。 [从这里开始](http://php.net/manual/en/book.pdo.php) – RiggsFolly
您的脚本处于[SQL注入攻击]风险中(http://stackoverflow.com/questions/60174/how -can-i-prevent-sql -injection-in-php) 即使 [如果你是逃避输入,它不安全!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets -around-mysql-real-escape-string) 使用[prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly
将此贴在php文件的顶部 'ini_set('display_errors',1); ini_set('display_startup_errors',1); error_reporting(E_ALL);'检查错误。 –