Asynchronous file uploads are frequently encountered and must be addressed in modern AJAX-implemented Web applications. However, the standard Ajax class (XMLHttpRequest) does not implement the ability to transfer files. So what we're talking about here is how to build asynchronous file uploads on top of the Ajax technology. In this feature you need to use the built-in boxes and (IFRAME) to transfer files. The effect of this function is that when the page uploads the file, the user can also use the page and fill in the file description.
This example is an analysis of the classic case of Ajax.
System environment
· Newer versions of the browser. such as Opera,firefox or Internet Explorer.
· PHP 4.3.0 or later
· PHP version 5
· The ' short_open_tag ' option in PHP is turned on (otherwise parsing errors will occur).
Functional analysis
Upload files via the built-in IFRAME (frame). comprises three parts.
· There is a simple control in the middle of the page. The target link to this form is a hidden iframe (the style "Display:none" through CSS). Implementation) and the onchange event of the only control inside the form is used to trigger the JavaScript function. The purpose of this function is to check the user-submitted extension and then submit the form.
· A processing process (annotated with Fileframe) was written in PHP on the server side. This process is used to store files uploaded from the client and stored on the server, and is returned to the user in the form of JavaScript code. The JavaScript script returned to the user changed the page that the user is currently viewing with "parent.window.document", sets the name of the file, and enabled the button to have the user submit the form. The action to enable the button is implemented through the getElementById function.
· There is also a form on the main page that contains user-submitted descriptions and hidden file names. The user can fill in the file description while uploading the file. When the file upload is finished, the user clicks on the button to see the file information that is returned to the user after uploading. (The file information is formed by the returned file name and the user-entered description).
You might say this is not common sense: The file has been submitted before the user confirms it. What would happen if the user hadn't submitted it? You can handle the files that are discarded by the user in the extension itself.
This example stores the file in a file system directory. You need to configure this directory when the script starts running, specifically the variables that contain this directory information are $upload_dir and $web_upload_dir. Here is a permission check to see if the directory is writable.
Here are some of the following PHP functions:
· Move_uploaded_file-Transfer files uploaded to the server once
· fopen-Open File
· Fwrite-Write content to file
· Fclose-Close File
· Str_replace-Replace string
· FileSize-Return file size
· Filemtime-Return processing time
You can find these functions in the manual if you use them. Please note that the HTM (<, &) tags are replaced with (<, > and &).
Source
$upload _dir = "/var/www/anyexample/aeu"; Path to the file store
$web _upload_dir = "/aeu"; Path to the file under the Web directory
$TF = $upload _dir. '/'. MD5 (rand ()). ". Test ";
$f = @fopen ($TF, "w");
if ($f = = False)
Die ("Fatal error! {$upload _dir} is not writable. Set ' chmod 777 {$upload _dir} '
Or something like this ");
Fclose ($f);
Unlink ($TF);
Working with uploaded files
if (Isset ($_post[' fileframe '))
{
$result = ' ERROR ';
$result _msg = ' No FILE field found ';
if (isset ($_files[' file '))//Accept file from browser
{
if ($_files[' file ' [' error '] = = UPLOAD_ERR_OK)//No Error
{
$filename = $_files[' file ' [' Name ']; Filename
Move_uploaded_file ($_files[' file ' [' Tmp_name '], $upload _dir. '/'. $filename);
Process the main process-transfer files to $upload _dir
$result = ' OK ';
}
ElseIf ($_files[' file '] [' error '] = = upload_err_ini_size)
$result _msg = ' The uploaded file exceeds the upload_max_filesize directive in php.ini ';
Else
$result _msg = ' Unknown error ';
}
Echo ';
Echo '
http://www.bkjia.com/PHPjc/767402.html www.bkjia.com true http://www.bkjia.com/PHPjc/767402.html techarticle asynchronous file uploads are frequently encountered and must be addressed in modern AJAX-implemented Web applications. However, the standard Ajax class (XMLHttpRequest) does not implement the ability to transfer files. The reason for ...