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

PHP newbies on the road (8)_PHP tutorial

Jul 21, 2016 pm 04:01 PM
php upload use Function Can accomplish client document Notice Browser of

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 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain the match expression (PHP 8 ) and how it differs from switch. Explain the match expression (PHP 8 ) and how it differs from switch. Apr 06, 2025 am 12:03 AM

In PHP8, match expressions are a new control structure that returns different results based on the value of the expression. 1) It is similar to a switch statement, but returns a value instead of an execution statement block. 2) The match expression is strictly compared (===), which improves security. 3) It avoids possible break omissions in switch statements and enhances the simplicity and readability of the code.

Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Describe the purpose and usage of the ... (splat) operator in PHP function arguments and array unpacking. Apr 06, 2025 am 12:07 AM

The... (splat) operator in PHP is used to unpack function parameters and arrays, improving code simplicity and efficiency. 1) Function parameter unpacking: Pass the array element as a parameter to the function. 2) Array unpacking: Unpack an array into another array or as a function parameter.

How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? How to correctly display the locally installed 'Jingnan Mai Round Body' on the web page? Apr 05, 2025 pm 10:33 PM

Using locally installed font files in web pages Recently, I downloaded a free font from the internet and successfully installed it into my system. Now...

Why does negative margins not take effect in some cases? How to solve this problem? Why does negative margins not take effect in some cases? How to solve this problem? Apr 05, 2025 pm 10:18 PM

Why do negative margins not take effect in some cases? During programming, negative margins in CSS (negative...

How to obtain real-time application and viewer data on the 58.com work page? How to obtain real-time application and viewer data on the 58.com work page? Apr 05, 2025 am 08:06 AM

How to obtain dynamic data of 58.com work page while crawling? When crawling a work page of 58.com using crawler tools, you may encounter this...

How to customize the resize symbol through CSS and make it uniform with the background color? How to customize the resize symbol through CSS and make it uniform with the background color? Apr 05, 2025 pm 02:30 PM

The method of customizing resize symbols in CSS is unified with background colors. In daily development, we often encounter situations where we need to customize user interface details, such as adjusting...

What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? What is Cross-Site Request Forgery (CSRF) and how do you implement CSRF protection in PHP? Apr 07, 2025 am 12:02 AM

In PHP, you can effectively prevent CSRF attacks by using unpredictable tokens. Specific methods include: 1. Generate and embed CSRF tokens in the form; 2. Verify the validity of the token when processing the request.

How to use CSS and Flexbox to implement responsive layout of images and text at different screen sizes? How to use CSS and Flexbox to implement responsive layout of images and text at different screen sizes? Apr 05, 2025 pm 06:06 PM

Implementing responsive layouts using CSS When we want to implement layout changes under different screen sizes in web design, CSS...

See all articles