在我的代码中,我有一个文件输入,与textareas一起使用,我的HTML表单如下所示:
<form action="includes/listing1.inc.php" method="post" enctype="multipart/form-data">
<input type="file" name="image_file">
<textarea name="title" cols="30" rows="2" placeholder="Title"></textarea>
<textarea name="description" cols="50" rows="5" placeholder="Description"></textarea>
<textarea name="price" cols="5" rows="1" placeholder="Price"></textarea>
<select multiple="multiple" name="categories">
<option value="bla1">bla1</option>
<option value="bla2">bla2</option>
<option value="bla3">bla3</option>
<option value="bla4">bla4</option>
<option value="bla5">bla5</option>
</select>
<input type="hidden" name="vendor" value="<?php $_SESSION['UserUid']; ?>">
<input type="submit" name="listpost-submit" value="Post listing">
注意,我有文件输入和enctype="multipart/form-data"
,我有以下PHP代码:
<?php
if (isset($_POST['listpost-submit'])) {
require 'dbh.inc.php';
$filename = $_FILES['image_file']['name'];
$target = 'site_images/';
$filetarget = $target.$filename;
$tempfilename = $_FILES['image_file']['name'];
$title = $_POST['title'];
$description = $_POST['description'];
$price = $_POST['price'];
$cat = $_POST['categories'];
$vendor = $_POST['vendor'];
$result = move_uploaded_file($tempfilename, $filetarget);
if ($result == true) {
echo '<div>Your file has been uploaded!</div>';
$sql = "INSERT INTO listings
(`image`,`title`,`description`,`price`,`category`,`vendor`,`imgpath`)
VALUES ('$filename', '$title', '$description', '$price', '$cat', '$vendor',
'$filetarget')";
header("Location: ../index.php?listing=posted");
exit();
}
elseif (empty($title) || empty($description) || empty($price) || empty($cat)
|| $vendor) {
echo '<div>Something is missing!</div>';
exit();
}
else {
echo '<div>There was a problem uploading your file!</div>';
exit();
}
}
mysqli_close($conn);
数据库结构:
idListings(int) imgListings(varchar(200)) titleListings descriptionListings priceListings categoryListings vendorListings imgpathListings(varchar(250))
目前,如果我运行代码,它将告诉我无法上传文件,我尝试将文件更改为中间没有空格的文件,但这也不起作用,我的问题是如何修复这样所有文件都可以存储在数据库中。请告诉我信息是否不足!
答案 0 :(得分:0)
找到了。您需要用户索引tmp_name,它是/ tmp文件夹上的原始文件名。
更改自:
$tempfilename = $_FILES['image_file']['name'];
收件人:
$tempfilename = $_FILES['image_file']['tmp_name'];
应该可以。