Home > Backend Development > PHP Tutorial > Please take care of me if you are new to PHP. Please take care of me if you are new to PHP (8)

Please take care of me if you are new to PHP. Please take care of me if you are new to PHP (8)

WBOY
Release: 2016-07-29 08:34:45
Original
964 people have browsed it

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 line ";upload_tmp_dir", that is, the semicolon ";" in front of it, so that this line will be in the php.ini document kick in. 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 define the maximum size by modifying the default value. The size of the uploaded file.
Don’t forget to restart the Apache, IIS or PWS service after modification.
 At the same time, there are several points worth noting about file uploading in PHP:
1. In the form form, set the method attribute to post and the enctype attribute to multipart/form-data;
2. In the form form, you can Add a hidden type input box 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 for uploading files (upload.htm):


Upload Your File


ENCTYPE="multipart/form-data" METHOD=POST>
NAME="MAX_FILE_SIZE" VALUE="2000000">
NAME="uploadfile" SIZE="24" MAXLENGTH="80">



NAME ="sendit">
NAME="cancelit">




PHP file that handles uploaded files (receiver.php3)
function do_upload ()
{
global $uploadfile, $uploadfile_size;
global $local_file, $error_msg;
if ( $uploadfile == "none" )
{
$error_msg = "Sorry, you did not select any file to upload!";
return;
}
if ( $uploadfile_size > 2000000 )
{
$error_msg = "Sorry, the file you want to upload is too large ! ";
return;
}
$the_time = time ();
// Specify the directory you use to store uploaded files here. You need to have write permissions for the following directories
// At the same time, we can also upload files Specify another directory, such as: $upload_dir = "/local/uploads";
$upload_dir = "d:/upload";
$local_file = "$upload_dir/$the_time";
if ( file_exists ( '$local_file' ) )
{
$seq = 1;
while ( file_exists ( "$upload_dir/$the_time$seq" ) ) { $seq++; }
$local_file = "$upload_dir/$the_time$seq";
};
rename ( $uploadfile, $local_file );
display_page ();
}
function display_page ()
{
// Here is your page content
}
?>





if ( $error_msg ) { echo "$error_msg

"; }
if ( $sendit )
{
do_upload ();
echo "File uploaded successfully!
>

v

The above has introduced the PHP newbies, please take care of me (8), including the content of the newbies, please take care of me. I hope it will be helpful to friends who are interested in PHP tutorials.


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template