Home  >  Q&A  >  body text

PHP implements file upload function

I want to upload a file to a given folder.

<?php
$folder = "upload/";
if (is_uploaded_file($HTTP_POST_FILES['filename']['tmp_name']))  {   
    if (move_uploaded_file($HTTP_POST_FILES['filename']['tmp_name'], $folder.$HTTP_POST_FILES['filename']['name'])) {
         echo "File uploaded";
    } else {
         echo "File not moved to destination folder. Check permissions";
    };
} else {s
     echo "File is not uploaded";
}; 
?>

The error is:

Note: Undefined variable: HTTP_POST_FILES in C:\wamp\www\sdg\import\ips.php on line 3

P粉868586032P粉868586032207 days ago525

reply all(2)I'll reply

  • P粉464082061

    P粉4640820612024-03-26 13:22:37

    PHP 4.1 introduces Super global. They replace the old long named arrays that contained the data extracted from the request. $_FILES[] Replaced $HTTP_POST_FILES[], $_GET[ ] Replaced $HTTP_GET_VARS[] Wait

    For subsequent PHP 4 versions, old arrays and new arrays can be used side by side. PHP 5 disables the generation of old arrays by default and introduces the php.ini< /code> directive register_long_arrays< /code> which can be used to re-enable old arrays create.

    As of PHP 5.4, the old long named arrays are completely removed and register_long_arrays disappear with them.

    Conclusion: You are learning from a very old or very bad tutorial. Find a better one.

    reply
    0
  • P粉788571316

    P粉7885713162024-03-26 09:22:47

    The following is one way to upload files, there are many other ways.

    As @nordenheim said, $HTTP_POST_FILES is deprecated as of PHP 4.1.0, so its use is not recommended.

    PHP code (upload.php)

     5000000) {
            $msg = "Sorry, your file is too large.";
        } elseif (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            $msg = "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been uploaded.";
        }
    }
    
    ?>

    HTML code to activate the function

    Select file to upload:

    Hope this helps.

    reply
    0
  • Cancelreply