problems with file upload

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
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

problems with file upload

Post by vasilis »

i am running php 4.06 on Apache Server in a Win ME system. I

am trying to debug a file upload script but it doesnt work. The

form i use is a classical file upload form with code:

Code: Select all

<html>
<head>
  <title>Administration - upload new files</title>
</head>
<body>
<h1>Upload file</h1>
<form enctype="multipart/form-data" action="upload.php" 

method=post>
  <input type="hidden" name="MAX_FILE_SIZE" value="1000">
  Upload this file: <input name="userfile" type="file">
  <input type="submit" value="Send File">
<input type="hidden" value="somevalue">
</form>
</body>
</html>
I have 3 questions:

1) When I try to upload a jpg file by submitting the form, I get the

following variable values: $userfile ="none"., $userfile_size=0 .

The $userfile_name and $userfile_type variables have correct

values. It seems that no file is uploaded to the temp file upload dir

of the apache server. I have checked the php.ini directives (and

the file size) and I have tried different path formats in the

upload_tmp_dir directive (e.g. "C:\WINDOWS\TEMP"/tmp, "C:

\WINDOWS\TEMP", "C:\\WINDOWS\\TEMP", "C:/WINDOWS/

TEMP" or the default php upload tmp directory) but none of these

work.
Do you think there is a problem with Apache running on Windows

or is it something else?
2) Also, when I try to pass some other variables with this multipart

form, their values dont pass across. (e.g. if i use a url

propagation, action="upload.php?var1=$value1", or pass a

variable through a hidden input, I get an undefined $var1 variable

in the upload.php script, although it should normally pass)

3) the upload script "upload.php" (which is the action attr of the

form) has a call to a function1 which has a call to function2. When

I try to debug the upload procedure in PHPEd (with debugging

On of course), it debugs ok during the first lines of the script, but

when it reaches the lines with the call to function1, something

returns false, and the debugging stops, i.e. I get the final output

without having stepped through the lines of the functions that are

called. Therefore, I cant figure out where the bug is. Even if I add

a breakpoint, the phpEd doesnt stop there but it continues to the

end of the script. Is there a way to make the phpEd debug

through these lines as well?

I know these are a lot of questions but I really have tried hard. I

appreciate any help.
User avatar
mchaggis
Forum Contributor
Posts: 150
Joined: Mon Mar 24, 2003 10:31 am
Location: UK

Post by mchaggis »

try changing the following line:

Code: Select all

<input type="hidden" name="MAX_FILE_SIZE" value="1000">
you are only allowing files smaller than 1k to be uploaded, try adding a few 0's
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Post by vasilis »

sorry, in my previous post actually I have used a value of "100000" in the line

Code: Select all

<input type="hidden" name="MAX_FILE_SIZE" value="1000">

and not "1000"
i.e., my code allows for big files to upload and I have checked that they are smaller than the max_file_size, but the problem still exists. Does anybody have any clue on the question concerning the phpEd?
User avatar
Etherguy
Forum Commoner
Posts: 70
Joined: Fri Nov 01, 2002 9:09 pm
Location: Long Island, New York

PHP Code

Post by Etherguy »

Could you supply some of the PHP code? It is kinda hard to troubleshoot with just the html form portion.

I will give you an example that I fine works pretty well, but without any further details I can not help ya.

Code: Select all

if(isset($uploadfile))
   &#123;

   print("<br>All set to upload a file." );
   print("<br>uploadfile: " . $uploadfile );
   print("<br>uploadfile_name: " . $uploadfile_name );
   print("<br>uploadfile_size: " . $uploadfile_size );
   print("<br>uploadfile_type: " . $uploadfile_type );
   &#125;
else
   &#123;
   print("<br>Please select the local file to upload." );
   print("<form enctype="multipart/form-data""
      . " action="upload.php" method="post">"
      . "<input type="hidden" name="MAX_FILE_SIZE""
      . " value="2000000">"
      . "<input type="file" name="uploadfile" size="60">"
      . "<br><input type="submit" value="Upload" onClick=alert("Uploading File")></form>" );
   &#125;
?>

<?php
if(isset($uploadfile) and is_uploaded_file($uploadfile))
   &#123;
   if(move_uploaded_file($uploadfile,
      $path . "/home/uploads" . $uploadfile_name))
      &#123;
      print("<br>Uploaded file saved." );
      &#125;
   else
      &#123;
      print("<br>Uploaded file save failed." );
      &#125;
   &#125;
Hope this helps.

Regards.
vasilis
Forum Commoner
Posts: 40
Joined: Tue Apr 22, 2003 7:37 am

Re: PHP Code

Post by vasilis »

Etherguy wrote:Could you supply some of the PHP code? It is kinda hard to troubleshoot with just the html form portion.

I will give you an example that I fine works pretty well, but without any further details I can not help ya.

Code: Select all

if(isset($uploadfile))
   &#123;

   print("<br>All set to upload a file." );
   print("<br>uploadfile: " . $uploadfile );
   print("<br>uploadfile_name: " . $uploadfile_name );
   print("<br>uploadfile_size: " . $uploadfile_size );
   print("<br>uploadfile_type: " . $uploadfile_type );
   &#125;
else
   &#123;
   print("<br>Please select the local file to upload." );
   print("<form enctype="multipart/form-data""
      . " action="upload.php" method="post">"
      . "<input type="hidden" name="MAX_FILE_SIZE""
      . " value="2000000">"
      . "<input type="file" name="uploadfile" size="60">"
      . "<br><input type="submit" value="Upload" onClick=alert("Uploading File")></form>" );
   &#125;
?>

<?php
if(isset($uploadfile) and is_uploaded_file($uploadfile))
   &#123;
   if(move_uploaded_file($uploadfile,
      $path . "/home/uploads" . $uploadfile_name))
      &#123;
      print("<br>Uploaded file saved." );
      &#125;
   else
      &#123;
      print("<br>Uploaded file save failed." );
      &#125;
   &#125;
Hope this helps.

Regards.

here is some code that you had asked (sorry for the delay)
my form submit code is :

Code: Select all

<form ENCTYPE="multipart/form-data" method=post action="admin_insert_type.php">
  <INPUT TYPE=hidden name=MAX_FILE_SIZE value="200000">
      Insert product type name:
    <input type=text name=product_type_name size=40 maxlength=40 value="">
  <?php
  
      print "<br>select product brand";
      //get the product brands from database and make a drop-down select box with these
      $brand_array=get_brands(-1);  //here, I use a function with which all brands stored in a database 

are returned 
      print "<br><SELECT name="brand_lst"><OPTION value="null" selected>Product brands</

OPTION>";
      // display the product brands that are stored in the $group_array array
      display_brands($brand_array,$brand_lst); //and these brands populate the select box as its 

options
      print "</SELECT>";

      print "<br>enter comments of the new product type:";
      print "<br><TEXTAREA NAME="descptn" ROWS="5" COLS="45"></TEXTAREA>";
      print "<br>select a image file";  //the user selects a jpg file to upload to the server so its path is 

stored in the database
      print "<br><INPUT NAME="userfile" TYPE="file">";
  
  ?>
  <br>
      
      <input type=submit value="insert new product type"; ?>"></form>
-----------------------------------------

the script that runs when the form is submitted is admin_insert_type.php:

Code: Select all

<?php
// include function files for this application with require_once() statements
require_once ("some_file.php");


//define two arrays which denote the files that can be uploaded
$allowed_types = array("image/bmp","image/gif","image/pjpeg","image/jpeg", "image/jpg");
$registered_types = array(
    "image/bmp"                 => ".bmp, .ico",
    "image/gif"                 => ".gif",
    "image/pjpeg"                => ".jpg, .jpeg",
    "image/jpeg"                => ".jpg, .jpeg"    
); 

//check w a user defined function that all form inputs are filled
if (filled_out($HTTP_POST_VARS))            &#123;

echo "<h1>Uploading file...</h1>";

  // $userfile is where file went on webserver
  // $userfile_name is original file name
  // $userfile_size is size in bytes
  // $userfile_type is mime type e.g. image/gif

  echo "Variables are:<br>";
  echo $userfile." ".$userfile_name." ".$userfile_size." ".$userfile_type."<br>";


  if ($userfile=="none")
  &#123;
    echo "Problem: no file uploaded";
    exit;
  &#125;

  if ($userfile_size==0)
  &#123;
    echo "Problem: uploaded file is zero length";
    exit;
  &#125;

  if (!in_array($userfile_type,$allowed_types)) &#123;    //$allowed_types 
            $error .= "\n<li>the file type you selected is not allowed to be uploaded. You can upload only files of the following types:\n<ul>";
            while ($type = current($allowed_types)) &#123;
                $error .= "\n<li>" . $registered_types&#1111;$type] . " (" . $type . ")</li>";
                next($allowed_types);
            &#125;
            $error .= "\n</ul>";
        &#125;

  $upfile = "//some path//product_images".$userfile_name;

  if ( !copy($userfile, $upfile))
  &#123;
    echo "Problem: Could not move file into directory";
    exit;
  &#125;

  echo "File uploaded successfully<br><br>";


//below follow some code for inserting some other field values (input from the form) in the database
//use a user-defined function (I have checked it, it works ok, dont bother about that)           
if(insert_type($product_type_name, $brand_lst, $descptn, $userfile_name))  &#123;
              $brand_name=get_brand_name($brand_lst);
              echo "a new product type was added to the database";
            &#125;
            else  &#123;
              echo "no new product type was inserted";
             &#125;
&#125;
//the "else" part of the initial "if" statement
else  &#123;
            echo "You didnt fill the form. Please try again";
                  print "<p><a href="admin_insert_type_form.php">Back to the input form</a></p>";
          &#125;
?>
When I run the whole code in phpEd, the debugger runs ok till before the submission of the form. Once I click submit, the debugger stops debugging and the whole of the rest script runs till the end, without
being able to debug it. I also get no values from any of the file variables. All these are undefined.

Therefore, I cant figure out whats going on.
Appreciate any suggestions...
Post Reply