我正在創建一個網站,顯示在其管理面板上上傳的新聞。
每篇文章都有一個圖像和一個標題(和描述,但我還沒有實現它)。
我的問題是,當我嘗試發佈(並上傳圖片)後創建,但圖像不存在。無法複製上傳的文件(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);'檢查錯誤。 –