PHP newbies on the road (8)_PHP tutorial
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):
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 big!";
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 specify another directory for uploaded files, 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 ()
{
// This is your page Content
}
?>
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

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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.

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.

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 do negative margins not take effect in some cases? During programming, negative margins in CSS (negative...

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

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

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.

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