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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1261
29
C# Tutorial
1234
24
The Compatibility of IIS and PHP: A Deep Dive The Compatibility of IIS and PHP: A Deep Dive Apr 22, 2025 am 12:01 AM

IIS and PHP are compatible and are implemented through FastCGI. 1.IIS forwards the .php file request to the FastCGI module through the configuration file. 2. The FastCGI module starts the PHP process to process requests to improve performance and stability. 3. In actual applications, you need to pay attention to configuration details, error debugging and performance optimization.

How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial How to register an account on Ouyi Exchange Ouyi Exchange Registration Tutorial Apr 24, 2025 pm 02:06 PM

The steps to register an Ouyi account are as follows: 1. Prepare a valid email or mobile phone number and stabilize the network. 2. Visit Ouyi’s official website. 3. Enter the registration page. 4. Select email or mobile phone number to register and fill in the information. 5. Obtain and fill in the verification code. 6. Agree to the user agreement. 7. Complete registration and log in, carry out KYC and set up security measures.

Binance download link Binance download path Binance download link Binance download path Apr 24, 2025 pm 02:12 PM

To safely download the Binance APP, you need to go through the official channels: 1. Visit the Binance official website, 2. Find and click the APP download portal, 3. Choose to scan the QR code, app store, or directly download the APK file to download to ensure that the link and developer information are authentic, and enable two-factor verification to protect the security of the account.

Download the official website of Ouyi Exchange app for Apple mobile phone Download the official website of Ouyi Exchange app for Apple mobile phone Apr 28, 2025 pm 06:57 PM

The Ouyi Exchange app supports downloading of Apple mobile phones, visit the official website, click the "Apple Mobile" option, obtain and install it in the App Store, register or log in to conduct cryptocurrency trading.

What to do if the USDT transfer address is incorrect? Guide for beginners What to do if the USDT transfer address is incorrect? Guide for beginners Apr 21, 2025 pm 12:12 PM

After the USDT transfer address is incorrect, first confirm that the transfer has occurred, and then take measures according to the error type. 1. Confirm the transfer: view the transaction history, obtain and query the transaction hash value on the blockchain browser. 2. Take measures: If the address does not exist, wait for the funds to be returned or contact customer service; if it is an invalid address, contact customer service and seek professional help; if it is transferred to someone else, try to contact the payee or seek legal help.

Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Sesame Open Door Official Website Entrance Sesame Open Door Official Latest Entrance 2025 Apr 28, 2025 pm 07:51 PM

Sesame Open Door is a platform that focuses on cryptocurrency trading. Users can obtain portals through official websites or social media to ensure that the authenticity of SSL certificates and website content is verified during access.

How to register an account on Sesame Open Exchange? Tutorial on Registration of Sesame Open Exchange How to register an account on Sesame Open Exchange? Tutorial on Registration of Sesame Open Exchange Apr 24, 2025 pm 02:00 PM

Registering a Sesame Door Account requires 7 steps: 1. Prepare a valid email or mobile phone number and a stable network; 2. Visit the official website; 3. Enter the registration page; 4. Select and fill in the registration method; 5. Obtain and fill in the verification code; 6. Agree to the user agreement; 7. Complete registration and log in, it is recommended to carry out KYC and set security measures.

What is on-chain transaction? What are the global transactions? What is on-chain transaction? What are the global transactions? Apr 22, 2025 am 10:06 AM

EU MiCA compliance certification, covering 50 fiat currency channels, cold storage ratio 95%, and zero security incident records. The US SEC licensed platform has convenient direct purchase of fiat currency, a ratio of 98% cold storage, institutional-level liquidity, supports large-scale OTC and custom orders, and multi-level clearing protection.

See all articles