PHP newbie (8) _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
PHP beginners (8 ). 7. You can use PHP to upload files. Note that the browser on the client should be Netscape3 or IE3 or later. At the same time, because this program and your PHP configuration 7. file Upload

You can use PHP to upload files. Note that the browser on the client should be Netscape3 or later or IE3. At the same time, because this program is related to your PHP configuration file (PHP3 is php3.ini and PHP4 is php. in) settings. Before executing this program, check whether the following settings are made in your PHP configuration file:

Remove the; upload_tmp_dir line annotator, that is, the semicolon ";" in front, so that this line plays a role in the php. ini document. Upload_tmp_dir is used to define the temporary PATH for storing the uploaded file. here you can also define an absolute path for it, for example, upload_tmp_dir = d: upload. of course, your d: the upload directory must have read and write permissions.

If you have defined the upload path in your. php3 program, the path of the file to be uploaded is based on the path defined in. php3 program. In the following example, the directory used to store the uploaded files is 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 MB ), you can modify the default value to define the maximum size of uploaded files.

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

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

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


First, you need to upload a single file table webpage (upload.htm ):


Upload Your File






Process the uploaded php file (er. php3)
Function do_upload ()
{
Global $ uploadfile, $ uploadfile_size;
Global $ local_file, $ error_msg;
If ($ uploadfile = "none ")
{
$ Error_msg = "Sorry, you have not selected any files to upload! ";
Return;
}
If ($ uploadfile_size> 2000000)
{
$ Error_msg = "Sorry, the file to be uploaded is too large! ";
Return;
}
$ The_time = time ();

// Specify the directory where you want to store the uploaded files. you must have the write permission on the following directories.
// You can also specify another directory for the uploaded file, for example, $ 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
}
?>


Php3 processing ing 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 ();
}
?>

V

Http://www.bkjia.com/PHPjc/316837.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/316837.htmlTechArticle7. file upload you can use PHP file upload function, note that the client browser should be Netscape3 or later version IE3. At the same time, because this program and your PHP configuration...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.