Jump to content

problems uploading a file


dharm

Recommended Posts

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>

[code]<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>
<? } ?>[/code]
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
Link to comment
Share on other sites

The manual very clearly states:

[quote]<!-- 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>[/quote]

http://us3.php.net/manual/en/features.file-upload.php
Link to comment
Share on other sites

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…

[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>
<? } ?>[/code]

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

Any help will be much appreciated. 
Link to comment
Share on other sites

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".
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.