【发布时间】:2012-03-19 06:10:49
【问题描述】:
我正在尝试使用 php 将文件上传到服务器,但我需要一些帮助。
我有一个 html 表单来提交书名和图书图片。书名将存储在数据库中(见下文),图像将存储在服务器上。
id、书名和日期正在存储在数据库中,但图像未上传。请帮我解决一下。
谢谢。
数据库表“书”
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']。
标签: php mysql html file-upload