1
我想用php上傳圖像;但似乎不起作用,我不明白爲什麼。當我嘗試只插入沒有圖像的文本時,它工作正常(我的意思是如果我從php部分刪除圖像上傳的所有代碼);所以我猜這個部分是錯誤的,但是我找不到它。 這是我目前使用的上傳代碼。任何幫助,這是表示讚賞。無法用php上傳圖像
if (isset($_POST['add'])) {
$text = $_POST['text'];
$title = $_POST['title'];
$category = $_POST['category'];
$fileName = $_FILES['userfile']['userfile'];
$tmpName = $_FILES['userfile']['tmp_name'];
// make a new image name
$ext = substr(strrchr($fileName, "."), 1);
// generate the random file name
$randName = md5(rand() * time());
// image name with extension
$myFile = $randName . '.' . $ext;
// save image path
$path = "/img/" . $myFile;
$result = move_uploaded_file($tmpName, $path);
if (!$result) {
echo "Error uploading image file <br />";
var_dump($_FILES);
exit;
} else {
$db = new mysqli("localhost", "user", "mypass", "mydb");
if (mysqli_connect_errno()) {
printf("Connect failed: %s<br/>", mysqli_connect_error());
}
mysqli_set_charset($db, "UTF8");
$query = "INSERT INTO posts (post_text, image_name, post_image, post_title, category) VALUES (?, ?, ?, ?, ?)";
$conn = $db->prepare($query);
if ($conn == TRUE) {
$conn->bind_param("ssssi",$text, $myFile, $path, $title, $category);
if (!$conn->execute()) {
echo 'error insert';
} else {
header("Location: index.php");
exit;
}
} else {
die("Error preparing Statement");
}
}
} else {
echo 'error';
}
這裏是上傳的形式
<form method="post" action="postblog.php" enctype="multipart/form-data">
Title: <input type="text" name="title" id="title">
Category: <select name="category" id="category">
Desc :<br />
<textarea id="text" name="text" rows="15" cols="80" style="width: 80%"></textarea>
Image: <input type="file" name="userfile" />
<input type="submit" name="add" id="add" value="Добави">
</form>
的錯誤是從這裏:
如果(!$結果){回聲 「錯誤上傳圖像文件
」; }
哪裏上傳的代碼? – 2015-02-23 13:56:12
我已經添加了。我的錯。 – 2015-02-23 13:57:41
你有什麼錯誤嗎? – Bono 2015-02-23 13:58:13