File Upload Checking

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

File Upload Checking

Post by wolfnipplechips »

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)...

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>"; 
}
?>
 
User avatar
tajiknizam
Forum Newbie
Posts: 7
Joined: Tue Aug 18, 2009 6:25 am
Location: Pakistan

Re: File Upload Checking

Post by tajiknizam »

i use a function

Code: Select all

<?php
$file_destination = "images/".$filename; 
if(copy($file,$file_destination)){
echo "success";
}
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

Re: File Upload Checking

Post by wolfnipplechips »

Thanks for the reply.

I'm sending the file to the database as a blob, not a desination.

The most important aspect so far is allowing the form to be sent successfully without any file upload specified.
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

Re: File Upload Checking

Post by wolfnipplechips »

I've added a few checks, but this still isn't satisfactory...

Code: Select all

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)) or die("Error: cannot read file");
    $content = mysql_real_escape_string($content) or die("Error: cannot read file");
    fclose($fp);
 
    if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

Re: File Upload Checking

Post by wolfnipplechips »

Has anybody else got any suggestions.

It appears the form is still entered into the database even if a file isn't uploaded. There's just error messages saying that the file cannot be blank. Is there a way of suppressing these?
wolfnipplechips
Forum Commoner
Posts: 32
Joined: Tue Aug 18, 2009 6:46 am
Location: Berkshire, UK

Re: File Upload Checking

Post by wolfnipplechips »

This is what I currently have..

Can anyone spot what I'm doing wrong?

I'm still getting a 'Warning: fopen() [function.fopen]: Filename cannot be empty'.


Code: Select all

<?php 
include("connect.php"); 
 
 
if($_POST['submit'])   //If submit is hit
{
 
if(!empty($_FILES['userfile']['tmp_name'])){
echo "<br><span class='redtitles'>No PDF was attached!</span><br>"; }
 
else {
 
    $fileName = $_FILES['userfile']['name'];
    $tmpName  = $_FILES['userfile']['tmp_name'];
    $fileSize = $_FILES['userfile']['size'];
    $fileType = $_FILES['userfile']['type'];
    
    $fp = fopen($tmpName, 'r') or die("Couldn't open file");
    $content = fread($fp, filesize($tmpName)) or die("Error: cannot read file");
    $content = mysql_real_escape_string($content) or die("Error: cannot read file");
    fclose($fp);
 
    if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}}
 
$courseTitle = $_POST['courseTitle'];
$courseCategory1 = $_POST["eventCategory1"];
$courseCategory2 = $_POST["eventCategory2"];
$courseCategory3 = $_POST["eventCategory3"];   
$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'];
 
  if($courseCategory2 == "Please select...")
  {
 $courseCategory2 = NULL;
 }
   if($courseCategory3 == "Please select...")
   {
 $courseCategory3 = NULL;
 }
 
   $result = mysql_query("REPLACE INTO course (courseTitle, courseCategory1, courseCategory2, courseCategory3, courseLinks, courseHeader, courseOverview, courseDuration, courseObjectives, courseRequirements, courseDescription, courseAgenda, courseTestimonials, courseDurationtext, courseVideo, name, size, type, content)".
      "VALUES ('$courseTitle', '$courseCategory1', '$courseCategory2', '$courseCategory3', '$courseLinks', '$courseHeader', '$courseOverview', '$courseDuration', '$courseObjectives', '$courseRequirements', '$courseDescription', '$courseAgenda', '$courseTestimonials', '$courseDurationtext', '$courseVideo', '$fileName', '$fileSize', '$fileType', '$content')"); 
 
 
   echo "<br><br><span class='redtitles'>New Course Added to Library!</span><br>"; 
}
?>
r4r4
Forum Newbie
Posts: 11
Joined: Thu Jul 02, 2009 12:01 am

Re: File Upload Checking

Post by r4r4 »

want to see how to upload a file using FTP. Is there any public server available where I can upload a file for free ?
Post Reply