我想上傳文件到服務器上使用PHP,但我需要一些幫助。文件上傳php mysql
我有一個HTML表單提交書名和書籍圖像。書名將被存儲在數據庫中(見下文),圖像將被存儲在服務器上。
標識,書名和日期正在存儲在數據庫中,但是圖像未上傳。請幫我整理一下。
謝謝。
數據庫表「書」
id int(11), book_name varchar(255), date_added date
add_book.php
<?php
$book_name = $_POST['book'];
// insert fields to database
$sql_query = mysql_query("INSERT INTO books (book_name, date_added) VALUES ('$book_name', now()");
// get id for that row
$id = mysql_insert_id();
// rename the book to that id followed by the format .jpg
$new_book_name = "$id.jpg";
// define upload path
$upload_path = "../book_images/";
// move the uploaded file to the upload path with the new name
move_uploaded_file($_FILES['upload']['tmp_name'], $upload_path . $new_book_name);
?>
<form action="add_book.php" method="post" enctype="multipart/form-data" name="bookform" id="bookform">
Book name: <input name="book" type="text" id="book" value=""/> <br />
Book image: <input type="file" name="upload" id="upload" />
<input name="submit" type="submit" value="Add book" />
</form>
羅斯的東西可能會出現上傳錯誤。不要盲目嘗試複製文件,請首先檢查$ _FILES ['upload'] ['error']。 – Corbin 2012-03-18 21:33:48