PHP file upload problem

bnrj_sjy

New Member
Messages
18
Reaction score
0
Points
0
I am new to php and is trying to construct a file upload script that will only allow pdf file

i am using the following codes for my form.php

PHP:
<form action="upload.php" method="post"
enctype="multipart/form-data">
  <h4>Single File Upload<br />
  </h4>
  <label for="file">Filename:</label>
<input type="file" name="file" id="file" />
<br /><br />

<input type="submit" name="submit" value="Submit" />
</form>
and the following for the upload.php
PHP:
<?php
if (($_FILES["file"]["type"] == "application/pdf")
&& ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>

when i run it on localhost it shows the file uploaded successfully

but when i run it on server it gives the message Invalid File

please help me regarding it
 

drf1229

New Member
Messages
71
Reaction score
1
Points
0
Are you sure you uploaded the same file both times? I've never encountered this error, however since I'm the only one who uses file uploads on my website, I took off all of the file limits. Try taking off the first limit:
Code:
if (($_FILES["file"]["type"] == "application/pdf")
&& ($_FILES["file"]["size"] < 200000)){
}

and...

else{
print "Invalid file";
}
  {
because that's the error message you are getting. If you continue to get an error message generated by your program, delete file limit that follows. If you still want to keep your file limits, double check you have the right file types and/or sizes. Good luck!
 

Gouri

Community Paragon
Community Support
Messages
4,565
Reaction score
245
Points
63
I tried that code. It is ok and working fine.

check what is the maximum upload size in the server.

One more thing if POST size exceeds server limit then $_POST and $_FILES arrays becomes empty.
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
Add debugging code to see what the server thinks certain values are ......

PHP:
<?php
if (($_FILES["file"]["type"] == "application/pdf")
&& ($_FILES["file"]["size"] < 200000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";
 
    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
 
    echo "Invalid file";
 
  ##************   DEBUGGING CODE:  ************##
 
  echo  "<br />File type is : " . $_FILES["file"]["type"] . "<br />File size is: " . $_FILES["file"]["size"]  ;
 
 
  }
?>

Also, you can test the max upload size by testing:

PHP:
$max = ini_get( 'upload_max_filesize' ) ;

echo "Max is $max" ;

I show it to be 32M on Chopin.
 
Last edited:
Top