file upload - $_FILES and $_POST empty

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
sabatier
Forum Newbie
Posts: 6
Joined: Thu Aug 23, 2007 5:40 am

file upload - $_FILES and $_POST empty

Post by sabatier »

hi everyone,

Here is my form to upload a file to my server:

Code: Select all

<form action="upload.php" method="post" enctype="multipart/form-data" name="frmUpload">
<input name="MAX_FILE_SIZE" id="MAX_FILE_SIZE" type="hidden" value="999999999">
  <input name="userfile" type="file">
  <input type="submit" value="Send">
</form>
And here is upload.php:

Code: Select all

if ($_FILES['userfile']['error'] > 0)
  {
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])
    {
      case 1:  echo 'File exceeded upload_max_filesize';  break;
      case 2:  echo 'File exceeded max_file_size';  break;
      case 3:  echo 'File only partially uploaded';  break;
      case 4:  echo 'No file uploaded';  break;
    }
    exit;
  }

  // Does the file have the right MIME type?
  if ($_FILES['userfile']['type'] != 'text/plain')
  {
    echo 'Problem: file is not plain text';
    exit;
  }

  // put the file where we'd like it
  $upfile = '/uploads/'.$_FILES['userfile']['name'];

  if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
  {
     if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
     {
        echo 'Problem: Could not move file to destination directory';
        exit;
     }
  } 
  else 
  {
    echo 'Problem: Possible file upload attack. Filename: ';
    echo $_FILES['userfile']['name'];
    exit;
  }


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

  // reformat the file contents
  $fp = fopen($upfile, 'r');
  $contents = fread ($fp, filesize ($upfile));
  fclose ($fp);
 
  $contents = strip_tags($contents);
  $fp = fopen($upfile, 'w');
  fwrite($fp, $contents);
  fclose($fp);

  // show what was uploaded
  echo 'Preview of uploaded file contents:<br><hr>';
  echo $contents;
  echo '<br><hr>';
file_uploads is on in php.ini.
upload_tmp_dir is C:/PHP.

No matter what I do, $_FILES and $_POST are empty.
print_r($_FILES) returns an empty array.

I'm tearing my hair out over this. Can someone please tell me what I'm doing wrong?!

Regards,

Ruth
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

When you run

Code: Select all

print_r($_FILES);
you should specify the name of the object i.e.

Code: Select all

print_r($_FILES['userfile']);
sunnyverma1984
Forum Newbie
Posts: 3
Joined: Mon Sep 10, 2007 10:04 am
Contact:

Re: file upload - $_FILES and $_POST empty

Post by sunnyverma1984 »

sabatier wrote:hi everyone,

Here is my form to upload a file to my server:

Code: Select all

<form action="upload.php" method="post" enctype="multipart/form-data" name="frmUpload">
<input name="MAX_FILE_SIZE" id="MAX_FILE_SIZE" type="hidden" value="999999999">
  <input name="userfile" type="file">
  <input type="submit" value="Send">
</form>
And here is upload.php:

Code: Select all

if ($_FILES['userfile']['error'] > 0)
  {
    echo 'Problem: ';
    switch ($_FILES['userfile']['error'])
    {
      case 1:  echo 'File exceeded upload_max_filesize';  break;
      case 2:  echo 'File exceeded max_file_size';  break;
      case 3:  echo 'File only partially uploaded';  break;
      case 4:  echo 'No file uploaded';  break;
    }
    exit;
  }

  // Does the file have the right MIME type?
  if ($_FILES['userfile']['type'] != 'text/plain')
  {
    echo 'Problem: file is not plain text';
    exit;
  }

  // put the file where we'd like it
  $upfile = '/uploads/'.$_FILES['userfile']['name'];

  if (is_uploaded_file($_FILES['userfile']['tmp_name'])) 
  {
     if (!move_uploaded_file($_FILES['userfile']['tmp_name'], $upfile))
     {
        echo 'Problem: Could not move file to destination directory';
        exit;
     }
  } 
  else 
  {
    echo 'Problem: Possible file upload attack. Filename: ';
    echo $_FILES['userfile']['name'];
    exit;
  }


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

  // reformat the file contents
  $fp = fopen($upfile, 'r');
  $contents = fread ($fp, filesize ($upfile));
  fclose ($fp);
 
  $contents = strip_tags($contents);
  $fp = fopen($upfile, 'w');
  fwrite($fp, $contents);
  fclose($fp);

  // show what was uploaded
  echo 'Preview of uploaded file contents:<br><hr>';
  echo $contents;
  echo '<br><hr>';
file_uploads is on in php.ini.
upload_tmp_dir is C:/PHP.

No matter what I do, $_FILES and $_POST are empty.
print_r($_FILES) returns an empty array.

I'm tearing my hair out over this. Can someone please tell me what I'm doing wrong?!

Regards,

Ruth
its working on my wampserver. its mean there is no error in your code. check php.ini
Post Reply