Home  >  Article  >  Backend Development  >  PHP newbies on the road (8)_PHP tutorial

PHP newbies on the road (8)_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:01:21968browse

7. File upload

You can use PHP to implement the file upload function. Note that the client browser should be Netscape3 or above or IE3 or above. At the same time, because this program is related to your PHP configuration file (php3.ini for PHP3, php.in for PHP4) settings. Before executing this program, please check whether your PHP configuration file has the following settings:

Remove the comment character of the ;upload_tmp_dir line, that is, the semicolon ";" in front of it, so that the line is in php.ini document works. upload_tmp_dir is used to define the temporary path where uploaded files are stored. Here you can also define an absolute path for it, for example: upload_tmp_dir = d:upload Of course, your d:upload directory must have read and write permissions at this time.

If you have defined the upload path in your .php3 program, the path of the uploaded file will be based on the path defined in the .php3 program. In the following example, the receiver.php3 file specifies the directory used to store uploaded files: d:upload.

upload_max_filesize is used to limit the maximum size of uploaded files processed by PHP. It is calculated in bytes. The default value is 2097152= 2*1024*1024 bytes (2 megabytes). You can modify this gap. Define the maximum upload file size.

Don’t forget to restart the Apache, IIS or PWS service after modification.
 
At the same time, in PHP, there are several points worth noting when uploading files:
1. In the form form, set the method attribute to post and the enctype attribute to multipart/form-data;

2. You can add a hidden type input box to the form, with a hidden value field named MAX_FILE_SIZE. You can limit the size of the uploaded file by setting its VALUE. Of course, this value cannot exceed the upload_max_filesize in the PHP configuration file (PHP3 is php3.ini, PHP4 is php.ini). Note that this input box must be placed in front of all file type input boxes, otherwise it will be invalid;

3. After the PHP program is run, the uploaded file is placed in the temporary directory. If the uploaded file has not been renamed or moved, the file will be automatically deleted from the temporary folder at the end of the request, so we'd better upload the new uploaded file immediately to a permanent directory or change its file name.


First we need a form page to upload files (upload.htm):


Upload Your File</ TITLE> <br></HEAD> <br><BODY> <br><FORM ACTION="receiver.php3" <br>ENCTYPE="multipart/form-data" METHOD=POST> <br>< ;INPUT TYPE="HIDDEN" <br>NAME="MAX_FILE_SIZE" VALUE="2000000"> <br><INPUT TYPE="FILE" <br>NAME="uploadfile" SIZE="24" MAXLENGTH="80 "> <br><BR><BR> <br><INPUT TYPE="SUBMIT" VALUE="Upload File!" <br>NAME="sendit"> <br><INPUT TYPE= "SUBMIT" VALUE="Cancel" <br>NAME="cancelit"><BR> <br></FORM> <br></BODY> <br></HTML> <br><br>PHP file that handles uploaded files (receiver.php3) <br><? <br>function do_upload () <br>{ <br>global $uploadfile, $uploadfile_size; <br>global $local_file, $error_msg; <br>if ( $uploadfile == "none" ) <br>{ <br>$error_msg = "Sorry, you did not select any file to upload!"; <br>return; <br>} <br>if ( $uploadfile_size > 2000000 ) <br>{ <br>$error_msg = "Sorry, the file you want to upload is too big!"; <br>return; <br>} <br>$the_time = time (); <br><br>// Specify the directory you use to store uploaded files here. You need to have write permissions for the following directories<br>// At the same time, we can also specify another directory for uploaded files, such as: $upload_dir = "/local/uploads"; <br><br>$upload_dir = "d:/upload"; <br>$local_file = "$upload_dir/$the_time"; <br>if ( file_exists ( '$local_file' ) ) <br>{ <br>$seq = 1; <br>while ( file_exists ( "$upload_dir/$the_time$seq" ) ) { $seq++; } <br>$local_file = "$upload_dir/$the_time$seq" ; <br>}; <br>rename ( $uploadfile, $local_file ); <br>display_page (); <br>} <br>function display_page () <br>{ <br>// This is your page Content<br>} <br>?> <br><HTML> <br><HEAD> <br><TITLE>php3 Receiving Script

if ( $error_msg ) { echo "$error_msg

"; }
if ( $sendit )
{
do_upload ();
echo "File uploaded successfully! ";
}
elseif ( $cancelit )
{
header ( "Location: $some_other_script" );
echo "File upload failed!";
exit;
}
else
{
some_other_func ();
}
?>




www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/316837.htmlTechArticle7. File upload You can use PHP to upload files. Note that the client browser should be Netscape3 or above or IE3 and above versions. At the same time, because this program is related to your PHP configuration...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn