7. File Upload
You can use PHP to implement the file upload function, note that the client's browser should be more than Netscape3 or more than IE3 version. Also, because this program is related to your PHP configuration file (PHP3 is PHP3.INI,PHP4 for php.in) settings. Before executing the program, please check that your PHP configuration file has the following settings:
Upload_tmp_dir the comment character of the line, that is, the preceding semicolon ";", to make the line work in the php.ini document. Upload_tmp_dir is used to define the temporary path of the upload file, where you can also define an absolute path, for example: Upload_tmp_dir = D:upload Of course, at this time your D:upload directory must have read and write permissions.
If you have defined the path of the upload in your. PhP3 program, the path of the uploaded file is based on the path defined in the. php3 program. In the following example, the Receiver.php3 file specifies the directory to hold the uploaded file: d:upload.
Upload_max_filesize is the maximum size of the upload file that is used to limit php processing, in bytes, the default value is 2097152 = 2*1024*1024 bytes (2 megabytes), and you can define the maximum upload file size by modifying the default value.
Don't forget to restart the Apache,iis or PWS service after the change.
At the same time in PHP, the file upload a few points are worth noting:
1. In the form form, set the method property to the Post,enctype property to Multipart/form-data;
2. In the form form, you can add an input box of type hidden, where the name is Max_file_size's hidden range, and you can limit the size of the uploaded file by setting its value. Of course, this value can not exceed the PHP configuration file (PHP3 is php3.ini,php4 to php.ini) in the upload_max_filesize, note that this input box must be placed in the input box of all file types, otherwise it is invalid OH ;
3. After the PHP program has finished running, the upload file is placed in the temp directory. If the uploaded file is not renamed or moved, the file will be automatically deleted from the Temp folder at the end of the request, so we'd better move the new upload file upload to a permanent directory or change its file name immediately.
First we need a form page (upload.htm) to upload the file:
<title>Upload Your File</title>
PHP file to process uploaded files (receiver.php3)
function Do_upload ()
{
Global $uploadfile, $uploadfile _size;
Global $local _file, $error _msg;
if ($uploadfile = = "None")
{
$error _msg = "Sorry, you have not selected any file upload!" ";
Return
}
if ($uploadfile _size > 2000000)
{
$error _msg = "Sorry, the file you are uploading is too big!" ";
Return
}
$the _time = time ();
Here you specify the directory where you want to store the uploaded files, you need to have write access to the following directories
At the same time, we can also give the upload file to 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 the content of your page
}
?>
<title>PhP3 receiving Script</title>
if ($error _msg) {echo]
$error _msg
"; }
if ($sendit)
{
Do_upload ();
echo "File Upload successful! ";
}
ElseIf ($cancelit)
{
Header ("Location: $some _other_script");
echo "File upload failed!";
Exit
}
Else
{
Some_other_func ();
}
?>
V
The above introduces the novice on the road please take care of PHP novice on the road (eight), including the Novice on the road please take care of the content, I hope that the PHP tutorial interested friends helpful.