Jump to content


Photo

problems uploading a file


  • Please log in to reply
3 replies to this topic

#1 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 August 2006 - 12:54 AM

Hi, I am having trouble with a simple upload file script and would appreciate any help.. Could anyone tell me how I could make files upload to my localhost (for testing)..

here is the script..

<html>
<head>
<title>File Upload Form</title>
</head>
<body><? if (!isset($action)) {?>
This form allows you to upload a file to the server.<br>

<form action="getfile.php" method="post"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="hidden" name="MAX_FILE_SIZE" value="25000" />
<input type="hidden" name="action" value="fghgf" />
<input type="submit" value="Upload File">
</form>

</body>
</html>
<? } ?>
<? if (isset($action)) {?>
<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?php
echo $_FILES['uploadFile'] ['name'];
if ( move_uploaded_file ($_FILES['uploadFile'] ['tmp_name'], 
       "Home/uploads/{$_FILES['uploadFile'] ['name']}")  )
      {  print '<p> The file has been successfully uploaded </p>';
       }
else
      { 
        switch ($_FILES['uploadFile'] ['error'])
         {  case 1:
                   print '<p> The file is bigger than this PHP installation allows</p>';
                   break;
            case 2:
                   print '<p> The file is bigger than this form allows</p>';
                   break;
            case 3:
                   print '<p> Only part of the file was uploaded</p>';
                   break;
            case 4:
                   print '<p> No file was uploaded</p>';
                   break;
         }
       }
?>
</body>
</html>
<? } ?>
here is the output..

Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 27

Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 28

Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 29

Notice: Undefined index: uploadFile in c:\easyphp1-7\www\getfile.php on line 34

#2 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 25 August 2006 - 01:26 AM

The manual very clearly states:

<!-- The data encoding type, enctype, MUST be specified as below -->
<form enctype="multipart/form-data" action="__URL__" method="POST">
    <!-- MAX_FILE_SIZE must precede the file input field -->
    <input type="hidden" name="MAX_FILE_SIZE" value="30000" />
    <!-- Name of input element determines name in $_FILES array -->
    Send this file: <input name="userfile" type="file" />
    <input type="submit" value="Send File" />
</form>


http://us3.php.net/m...file-upload.php

#3 dharm

dharm
  • Members
  • PipPip
  • Member
  • 27 posts

Posted 25 August 2006 - 10:05 AM

Thx!..Got it working.. but if I place each part of the array into separate variables (see blow) and then pass it through a URL it doesn’t work.. can I make this work? Or do I need to pass the array from form to form?

Here is the code…

<html>
<head>
<title>File Upload Form</title>
</head>
<body><? if (!isset($action)) {?>
This form allows you to upload a file to the server.<br>

<form action="getfile.php" enctype="multipart/form-data" method="post"><br>
Type (or select) Filename: <input type="file" name="uploadFile">
<input type="hidden" name="MAX_FILE_SIZE" value="25000" />
<input type="hidden" name="action" value="fghgf" />
<input type="submit" value="Upload File">
</form>

</body>
</html>
<? } ?>
<? if ($action=="fghgf") {
$filename1 = $_FILES['uploadFile']['name'];
$filetmp1 = $_FILES['uploadFile']['tmp_name'];
$filesize1 = $_FILES['uploadFile']['size'];
$fileerror1 = $_FILES['uploadFile']['error'];

header("Location: getfile.php?filename1=$filename1&filetmp1=$filetmp1&filesize1=$filesize1&fileerror1=$fileerror1&action=upload"); 

} 
?>
<?php
if ($action=="upload") {
?>
<html>
<head>
<title>Process Uploaded File</title>
</head>
<body>
<?

if ( move_uploaded_file ($filetmp1, 
       "Home/uploads/{$filename1}")  )
      {  print '<p> The file has been successfully uploaded </p>';
       }
else
      { 
        switch ($fileerror1)
         {  case 1:
                   print '<p> The file is bigger than this PHP installation allows</p>';
                   break;
            case 2:
                   print '<p> The file is bigger than this form allows</p>';
                   break;
            case 3:
                   print '<p> Only part of the file was uploaded</p>';
                   break;
            case 4:
                   print '<p> No file was uploaded</p>';
                   break;
         }
       }
?>
</body>
</html>
<? } ?>

..Output is a blank screen with vars showing correctly in URL

Any help will be much appreciated. 


#4 hitman6003

hitman6003
  • Members
  • PipPipPip
  • Advanced Member
  • 1,807 posts

Posted 25 August 2006 - 11:22 AM

If you do not do something with the file on the page that it is uploaded to, you will not have access to it.  When you upload the file it is kept in the location specified by $_FILES['uploadFile']['tmp_name'], however, when the script is done processing, that file is removed.  Therefor, you must use move_uploaded_file or another function to save the file permanently.

Change your form so it submits directly to action="upload", eliminating the intermediate action="fghgf".




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users