我有一个带有文件输入字段的表单:
<tr align="left">
<td>Image :</td>
<td align="left">
<input type="file" name="ImageFile" size="18">
</td>
</tr>
然后我在提交时处理这个图像文件:
$image_tmpname = $_FILES['ImageFile']['name'];
$imgdir = "blogImages/";
$imgname = $imgdir.$image_tmpname;
$blogs = new Blogs();
move_uploaded_file($_FILES['ImageFile']['tmp_name'], $imgname);
$insert = $blogs->insertBlog($heading, $article, $date, $imgname);
我要保存图像的目录被调用blogImages
,并且与上面的目录位于同一目录中。
您可能会注意到在上面我调用insertBlog
的类Blogs'. Insert blog takes all the info and inputs the data to a mysql table. The code for
insertBlog 中调用的函数是:
function insertBlog($heading, $article, $date, $imgname){
$query = "INSERT INTO Blogs (BlogTitle, MainArticle, PostDate, Image) VALUES ('$heading', '$article', '$date', '$imgname')";
$oDatabase = new database;
$connection = $oDatabase->Connect();
if (!mysql_select_db($oDatabase->Name(), $connection))
$oDatabase->ShowError("Blogs.insertBlog");
if (!(@ mysql_query ($query, $connection)))
$oDatabase->ShowError("Blogs.insertBlog");
return (mysql_insert_id());
}
当用户填写表格时,它会将除图像信息之外的所有其他信息正确存储在 MySQL 表中。此外,它不会将实际图像存储在blogImages
文件夹中。那么如何让这个脚本将图像上传到blogImages
文件夹并将其路径存储在 mysql 表中。目前它没有将图像存储在数据库中,只将值blogImages/
放在我的 MySQL 表中的图像路径字段中。