I have an issue with file uploads. I have a form, which also has a field for file uploads. However, I do not have any processing in the next script to see if there is a file to upload. I need to be able to have the script bypass the upload if it is not there, instead of giving me an error message. Here are the two files:
News_Add.php:
<?php
echo 'Please enter your information in the <b>form below.</b><br>';
echo '<form enctype="multipart/form-data" method="post" action="news_add_action.php">';
echo '<table>';
echo '<tr><td width="50%" align="left">News Title Name</td><td><input type="text" name="newsname"></td>';
//echo '<td><input type="hidden" name="MAX_FILE_SIZE" value="20000000"></td></tr>';
echo '<td width="50%" align="left"><input type="file" name="newspic"></td></tr>';
echo '<td align="left"><input type="submit" value="Add News"><input type="reset" value="Clear Form"></td></tr>';
echo '<tr><td align="left">News Article</td><td><textarea name="newsart" rows="20" cols="40"></textarea></td></tr>';
echo '</table>';
echo '</form>';
?>
News_Add_Action.php
<?php
session_start();
include 'config.php';
$title = $_POST['newsname'];
$article = $_POST['newsart'];
echo 'news_add_action.php';
$query = "INSERT INTO news (nid, title, news) VALUES ('NULL', '$title', '$article')";
mysql_query($query) or die('Error, insert query failed - '.mysql_error());
//Now we need to upload the file
$target_path = "news/";
$target_path = $target_path . basename( $_FILES['newspic']['name']);
if(move_uploaded_file($_FILES['newspic']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['newspic']['name']).
" has been uploaded";
} else{
echo "There was an error uploading the file, please try again!";
echo 'Here is some more debugging info:';
print_r($_FILES);
print "</pre>";
}
mysql_close;
$_SESSION['admin'] = NULL;
include 'index.php';
?>
What is the easiest and correct way to do this?
Thank you;
Ice