What we bring to you today isHowever, the standard AJAX class (XmlHttpRequest) cannot implement the file transfer function. Therefore, the content discussed here is how to build an asynchronous file upload function based on php ajax technology. In this function, you need to use the built-in box and (IFRAME) to transfer files. This function enables users to use this page and enter the file description when uploading files.
This example is based on the typical case of AJAX.
System Environment
· Newer versions of browsers. For example, Opera, Firefox, or Internet Explorer.
· PHP 4.3.0 or later
· PHP version 5
· Enable the 'short _ open_tag 'option in PHP (otherwise, a parsing error will occur ).
Analysis of PHP-based AJAX technical functions
Upload files using the built-in IFRAME (framework. It consists of three parts.
· There is a simple <form... form in the middle of the page. The form only contains the <input type = "file"...> control. The target link of the form is a hidden IFRAME (implemented by the CSS style "display: none;"), and the OnChange event of the only control in the form is used to trigger javascript Functions. This function is used to check the extension submitted by the user and then submit the form.
· On the server side, a processing process (using FILEFRAME to sit and comment) is written in PHP ). This process is used to check the files uploaded from the client and save them on the server, and return them to the user in the form of Javascript code. The Javascript script returned to the user changed the page that the user is viewing through "parent.w.w.doc ument", set the file name, and enable the button for the user to submit the form. The enable button operation is implemented through the getElementById function.
· There is also a form on the home page that contains the description submitted by the user and the hidden file name. You can enter the description of the file while uploading the file. After the file is uploaded, click the button to view the file information returned to the user after the upload. (The file name returned and the description entered by the user constitute the file information ).
You may say that this operation does not conform to common sense: the file has been submitted before the user confirms. What if the user does not submit it. You can process the files abandoned by users by yourself.
In this example, the file is stored in the directory of a file system. You need to configure this directory when the script starts running. The specific variables that contain this directory information are $ upload_dir and $ web_upload_dir. Check whether the directory is writable.
Here we use the following PHP functions:
· Move_uploaded_file-transfers files uploaded to the server
· Fopen-open a file
· Fwrite-write content into a file
· Fclose-close the file
· Str_replace-replace string
· Filesize-returned File Size
· Filemtime-return Processing Time
When using PHP-based AJAX technology, you can use the manual to find out how to use these functions. Note that you should replace the HTM (<,>, &) tag with (& lt;, & gt; and & amp ;).