Summary of simple and quick PHP file upload methods. PHP has been developing for a long time and many users are familiar with PHP. here I will share my personal understanding and discuss it with you. the support for PHP file uploading is becoming more and more perfect, after a long history of PHP development in PHP4.0.3, many users know PHP very well. here I will share my personal understanding and discuss it with you. the support for PHP file uploading is becoming more and more perfect, before PHP 4.0.3, we can only use the copy () and is_uploaded_file () methods to upload files. In fact, PHP is much easier to upload files than ASP, this is a small proud place for me to choose PHP.
After PHP 4.0.3, PHP has a new function move_uploaded_file (). Uploading a file is a lot easier. you only need this function. (I am not saying that uploading a file system is enough to have only one function. do not misunderstand what I mean, I mean move_uploaded_file is a core function for uploading files ). Now, let's take a look at the three functions (from the PHP Manual ). Copy (string source, string desk); copy the file from source to dest. If the call succeeds, TRUE is returned. if the call fails, FALSE is returned.
PHP file upload example:
- if (!copy($file, $file.’.bak’)) {
- print (”failed to copy $file…<br>n”);
- }
- is_uploaded_file(string filename)
If the file given by filename is uploaded through http post, TRUE is returned.
- move_uploaded_file(string filename, string destination)
This function checks and ensures that the file specified by filename is a valid PHP file to be uploaded (that is, the file is uploaded through the http post Upload mechanism of PHP ). If the file is valid, move it to the file specified by destination. The interpretation of the three functions can draw a conclusion that move_uploaded_file () is a combination of the copy () and is_uploaded_file () functions, after talking about these three functions in such a half day, they may be a little unfamiliar. here are two examples: first, create a front page and name it index.html.
- <form enctype=”multipart/form-data” action=”_URL_” method=”post”>
- Send this file: <input name=”filename” type=”file”>
- <input type=”submit” value=”Send File”>
-
form>
1. The following describes how to upload files using the copy () and is_uploaded_file () methods (save as: copy.php, upload the file, and change _ URL _ in index.html to copy. php)
-
Php
- $Path= "./Uploadfiles/"; // file Upload path
- If (is_uploaded_file ($ filename ))
- // Note that $ filename is passed to is_uploaded_file. do not pass $ _ FILES ["filename"] ["name"].
- {
- $File1= $ _ FILES ["filename"] ["name"];
- // Note that the $ _ POST [] method is supported by PHP4.1.0 and later. The $ HTTP_POST_FILES [] method is used for versions earlier than PHP.
- $File2= $ Path. time (). $ file1;
- $Flag=1;
- }
- If ($ flag) $Result=Copy($ File1, $ file2 );
- If ($ result) echo "Upload successful !";
- ?>
2. The following describes how to upload files using the move_uploaded_file () method (save as: move.php, upload the file, and change _ URL _ in index.html to move. php)
-
Php
- $Path= "./Uploadfiles/"; // Upload path
- If ($ _ FILES ["filename"] ["name"])
- {
- $File1= $ _ FILES ["filename"] ["name"];
- $File2= $ Path. time (). $ file1;
- $Flag=1;
- }
- If ($ flag) $Result=Move_uploaded_file($ _ FILES ["filename"] ["tmp_name"], $ file2 );
- // Note that the first parameter passed to move_uploaded_file is the temporary file uploaded to the server.
- If ($ result) echo "Upload successful !";
- ?>
This is just two small programs. In fact, the code required to complete file upload is not the case. There are also file size restrictions, file extension selection, and some other security solutions. I wrote this article, and everyone is interested in writing a complete file upload system. Haha !!!
4.0.3...