File Upload Checking
Posted: Wed Aug 19, 2009 2:48 am
Hi There,
I have a file upload field on a form which currently works fine.
However, you are currently required to attach a file to the form in order to send succesfully, other wise the following error occurs, and the post stops...
"Warning: fopen() [function.fopen]: Filename cannot be empty in /homepages/37/d293174663/htdocs/course_scripts/add_course.php on line 37"
I understand that 'fopen' requires a file in order to create a stream, but is there a way around this? Either through allowing the file upload to be blank, or forcing the post to cancel and forcing the user to upload a file?
I'm a complete novice and would appreciate some help on this.
Here is my code (nothing special)...
I have a file upload field on a form which currently works fine.
However, you are currently required to attach a file to the form in order to send succesfully, other wise the following error occurs, and the post stops...
"Warning: fopen() [function.fopen]: Filename cannot be empty in /homepages/37/d293174663/htdocs/course_scripts/add_course.php on line 37"
I understand that 'fopen' requires a file in order to create a stream, but is there a way around this? Either through allowing the file upload to be blank, or forcing the post to cancel and forcing the user to upload a file?
I'm a complete novice and would appreciate some help on this.
Here is my code (nothing special)...
Code: Select all
<?php
include("connect.php");
if($_POST['submit']) //If submit is hit
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
}
$courseTitle = $_POST['courseTitle'];
$courseLinks = $_POST['courseLinks'];
$courseHeader = $_POST['courseHeader'];
$courseOverview = $_POST['courseOverview'];
$courseDuration = $_POST['courseDuration'];
$courseObjectives = $_POST['courseObjectives'];
$courseRequirements = $_POST['courseRequirements'];
$courseDescription = $_POST['courseDescription'];
$courseAgenda = $_POST['courseAgenda'];
$courseTestimonials = $_POST['courseTestimonials'];
$courseDurationtext = $_POST['courseDurationtext'];
$courseVideo = $_POST['courseVideo'];
$result = mysql_query("REPLACE INTO course (courseTitle, courseLinks, courseHeader, courseOverview, courseDuration, courseObjectives, courseRequirements, courseDescription, courseAgenda, courseTestimonials, courseDurationtext, courseVideo, name, size, type, content)".
"VALUES ('$courseTitle', '$courseLinks', '$courseHeader', '$courseOverview', '$courseDuration', '$courseObjectives', '$courseRequirements', '$courseDescription', '$courseAgenda', '$courseTestimonials', '$courseDurationtext', '$courseVideo', '$fileName', '$fileSize', '$fileType', '$content')");
echo "<span class='titles'>New Course Added to Library!</span><br>";
}
?>