Я работаю с решением, предоставленным Cool Hand Luke UK, которое загружает файл и добавляет различные детали в MySQL (, вот пост ).Пока что работает отлично, но я хочу немного изменить ситуацию.
Моя форма настроена так, чтобы пользователи могли добавлять либо файл, либо URL, но не оба сразу (код ниже, но некоторыеработать, чтобы показать / скрыть поля URL и файла, но я уверен, что вы поняли).Я хотел бы использовать тот же сценарий, но сталкиваться с проблемами, когда поле файла не заполнено.
Я надеялся, что кто-то может помочь настроить php, чтобы я мог использовать обе функции, используя один и тот же php?
Любая помощь была бы фантастической.Заранее спасибо.
-
PHP (insert_news.php):
<?php error_reporting(E_ALL^E_NOTICE);
define('INCLUDE_CHECK',true); include "connect.php";
$target = "../uploads/"; $target = $target . basename( $_FILES['photo']['name']);
//This gets all the other information from the form
$name=$_POST['name'];
$author=$_POST['author'];
$newsID=$_POST['newsID'];
$description=$_POST['description'];
$type=$_POST['type'];
$url=$_POST['url'];
$pic=(mysql_real_escape_string($_FILES['photo']['name']));
//Writes the information to the database
mysql_query("INSERT INTO adidas_news (name, description, documentName, author, newsID, url, type) VALUES ('$name', '$description', '$pic', '$author', '$newsID', '$url', '$type')");
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) {
//Tells you if its all ok
header("Location: ../news.php"); } else {
//Gives and error if its not
echo "Sorry, there was a problem uploading your file."; }
?>
Форма:
<form method="post" id="addURLForm" action="insert_news.php">
<img src="images/bg_addform_top.gif" width="446" height="11" />
<table class="addForm" cellspacing="0">
<tr>
<th>Name:<span class="greenText">*</span></th>
<td><input name="name" type="text" class="textBox required" maxlength="80" /></td>
</tr>
<tr>
<th>Type:<span class="greenText">*</span></th>
<td>
<select name="type" class="textBox">
<option selected="selected">-- Select --</option>
<option value="file">File/Document</option>
<option value="url">URL/Link</option>
</select>
</td>
</tr>
<tr>
<th>Full URL:<span class="greenText">*</span></th>
<td><input name="url" type="text" class="textBox url" maxlength="120" /></td>
</tr>
<tr>
<th>File:<span class="greenText">*</span></th>
<td><input type="file" class="" name="photo"></td>
</tr>
<tr>
<th>Description:<span class="greenText">*</span></th>
<td><textarea name="description" class="textBox required"></textarea></td>
</tr>
<tr>
<th>
<input name="newsID" type="hidden" value="<?php
for ($i=0; $i<5; $i++) {
$d=rand(1,30)%2;
echo $d ? chr(rand(65,90)) : chr(rand(48,57));
}
?>" />
<input name="author" type="hidden" value="<?php
$result = mysql_query("SELECT * FROM adidas_members WHERE usr='".$_SESSION['usr']."'");
while($row = mysql_fetch_array($result))
{
echo "" . $row['fullName'] . "";
}
?>" />
</th>
<td><input type="image" class="submitButton" src="images/button_submit.gif" /></td>
</tr>
</table>
</form>