PHP Ajax allows you to upload without any new attachments and ajax attachments.
A basic and indispensable function for a website is file upload. Using PHP preloading to upload files has a unique advantage. Today, let's take a look at the new implementation of File Upload without refreshing.
--------------------------------------------------------------------------------
Normal Form
Front-end page
<form action="./fileupload.php" method="POST"><p>Username<input type="text" name="username" /></p><p>Password<input type="password" name="password" /></p><p>E-mail<input type="text" name="email" /></p><input type="submit" value="Register" /></form>
Background Processing
Print_r ($ _ POST); $ username =$ _ POSY ['username']; $ password =$ _ POST ['Password']; $ email = $ _ POST ['email ']; // compare the content on the server with the database to implement the relevant business logic.
···
Form with file
There is a big difference between forms with files and normal forms. First, we need to declare an attribute on the form, as shown below:
<Form enctype = 'multipart/form-data'>
To tell the server that the uploaded form contains file information.
Refresh Method
When talking about the refresh mode, that is, after clicking submit, the page jumps to the business processing interface. This is also the form data submission that we implement in common mode.
Front-End Interface
<form action="./fileupload.php" enctype="multipart/form-data" method="post"><p>Username<input type="text" name="username" /></p><p>Password<input type="password" name="password" /></p><p>E-mail<input type="text" name="email" /></p><p>Photo<input type="file" name="photo" /></p><input type="submit" value="Register" />
Background page
In addition to common form data, the most important thing for background processing is to process file information. PHP has a built-in function that can easily transfer uploaded files from the temporary data area to our target folder to implement the upload business logic.
Move_uploaded_file ('temporary file path', 'destination file path ');
Refreshing new methods
The only difference between refreshing and refreshing is that page redirection is blocked. We usually implement this in two ways.
• Add return false at the end of the Form submission event.
<Script> var form = document. getElementsByTagName ('form') [0]; form. onsubmit = function () {// to do something · // block page Jump return false;} </script>
• Use h5-related methods.
<Script> var form = document. getElementsByTagName ('form') [0]; form. onsubmit = function (event) {// to do something · // block page Jump event. preventDefault () ;}</script>
Other operations must be consistent with those with jumps.
Upload large files
Although PHP is convenient and fast to upload files. However, the size of the uploaded file is not unlimited. By default, there are two factors that limit the size of the uploaded files.
• Extreme Values of post
• Extreme Values of upload
You can manually change the configuration information of php. ini to control the size of uploaded files.
POST extreme values
Post_max_size = 200 M
Upload Extreme Value
Upload_max_filesize = 200 M
Upload details
An ajax object has an attribute called upload, and upload also exists as an object. It has an onprogress method. We can monitor this method to view the percentage of files uploaded.
Front-end page
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
Background Processing
<? Php // print_r ($ _ POST); // echo "---------------------". "<BR>"; // print_r ($ _ FILES); // The storage location and name of the attachment. Remember to have the upload folder $ path = ".. /upload/"; // obtain the original file name $ origin_name = $ _ FILES ['photo'] ['name']; // concatenate the file into the name $ server_name = $ path on the server. $ origin_name; if ($ _ FILES ['photo'] ['error']> 0) {die ("error! ". $ _ FILES ['photo '] ['error']);} if (move_uploaded_file ($ _ FILES ['photo'] ['tmp _ name'], $ server_name )) {echo "<BR> ". "Upload Success! ";} Else {echo" <BR> "." Upload Failed! ". $ _ FILES ['photo'] ['error'];}?>
Ensure the existence of the upload folder and the path problem.
Summary
File Upload is a basic function and is not complicated to implement. However, this function is so important that we can easily use File Upload to modify user profile pictures and other file information.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.