0
我試圖修復此表單,以使文件上傳一個選項,而不是要求,因爲我不會總是有圖像可用。到目前爲止,這是我從研究中得出的結果,但我仍然收到一條消息,要求提供文件。PHP表單與非必需的文件上傳
else if (isset($_POST['add_news_btn'])) {
include 'connect.php';
$newsdate = (isset($_POST['newsdate']) ? $_POST['newsdate'] : null);
$newstitle = (isset($_POST['newstitle']) ? $_POST['newstitle'] : null);
$newscatagory = (isset($_POST['newscategory']) ? $_POST['newscategory'] : null);
$newstext = (isset($_POST['newstext']) ? $_POST['newstext'] : null);
// upload file
if (!empty($_FILES['newsuploader'])) {
if(move_uploaded_file($_FILES["newsuploader"]["tmp_name"], "../media/images/" .$_FILES["newsuploader"]["name"]))
echo "Saved";
$imageURL = "media/images/" .$_FILES["newsuploader"]["name"];
/* else
$imageURL='';
*/
if (isset($_POST['display']) && $_POST['display'] == '1')
{
$stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory, imageURL, display_image) VALUES(?, ?, ?, ?, ?, 1)");
}
else {
$stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory, imageURL, display_image) VALUES(?, ?, ?, ?, ?, 0)");
}
} else if (empty($_FILES['newsuploader'])){
$imageURL = "media/images/news-logo.png";
$stmt = $conn->prepare("INSERT INTO news (date, title, content, newscatagory, imageURL, display_image) VALUES(?, ?, ?, ?, ?, 0)");
}
$stmt->bind_param('sssss', $newsdate, $newstitle, $newstext, $newscatagory, $imageURL);
$stmt->execute();
$stmt->close();
echo "done";
}
}
對應形式
<form name="news-page" action="" method="POST" enctype="multipart/form-data">
<h1>News</h1>
<span id="newstitle">
<p id="newstitle">News Title</p>
<input id="title" type="text" name="newstitle" value="News Title"/>
</span>
<span id="newsdate">
<p>News Date</p>
<input id="news_date" type="text" name="newsdate" value="News Date"/>
</span>
<span id="category">
<p>News Category</p>
<input id="newscategory" type="text" name="newscategory" value="News Category"/>
</span>
<p id="news_info">News Information</p>
<textarea id="newsinfo" name="newstext">Bacon ipsum dolor amet turducken boudin sirloin ..</textarea>
<div id="newsimage">
<img src/>
<p>Insert News Image</p>
<label class="myLabel" id="news-image-upload">
<input type="file" required name="newsuploader" id="fileToUpload" />
<span>Select Image</span>
</label>
<input type="checkbox" name="display" value="1">Display Image
<button type="submit" name="add_news_btn">Add News</button>
</div>
</form>
由於它的工作原理 - 由於某種原因,我想我需要改變在PHP的東西。 – Chri