Phpiframe implementation of brushless new file upload (without ajax) _ PHP Tutorial

Source: Internet
Author: User
Phpiframe implements the upload of non-refreshing files (without ajax ). This article introduces how to upload files. it is refreshing to use phpiframe to upload files. if you need phpiframe, refer to the following. First of all, ajax does not introduce the file upload method in this article. it is refreshing to use php iframe to upload files. if you need it, refer to the following.

First, ajax was unable to upload files, which misled me for a while. if I couldn't sleep tonight, I followed the instructions to upload a new file without refreshing.

In fact, the principle is very simple

The code is as follows:



And general

A tag has one more target attribute than a tag, which is used to specify where the tab is opened and submit data.

If this attribute is not set, the url in the action will be redirected on this page as usual.

If it is set to the name value of iframe, that is, "upload", it will be opened in the iframe, because CSS is set to hidden, so there will be no movement. If you remove display: none, the returned information of the server is displayed.

In addition, paste your own classes.

The code is as follows:

Class upload
{
Public $ _ file;

Public function _ construct ($ name = null)
{
If (is_null ($ name) |! Isset ($ _ FILES [$ name])
$ Name = key ($ _ FILES );

If (! Isset ($ _ FILES [$ name])
Throw new Exception ("no file is uploaded ");

$ This-> _ file =$ _ FILES [$ name];

If (! Is_uploaded_file ($ this-> _ file ['tmp _ name'])
Throw new Exception ("Exception ");
If ($ this-> _ file ['error']! = 0)
Throw new Exception ("error code:". $ this-> _ file ['error']);
}
Public function moveTo ($ new_dir)
{
$ Real_dir = $ this-> checkDir ($ new_dir );
Return move_uploaded_file ($ this-> _ file ['tmp _ name'], $ real_dir. '/'. $ this-> _ file ['name']);
}
Private function checkDir ($ dir)
{
$ Real_dir = realpath ($ dir );
If ($ real_dir = false)
Throw new Exception ("The specified directory {$ dir} does not exist ");
If (! Is_writable ($ real_dir ))
Throw new Exception ("The specified directory {$ dir} cannot be written ");
Return $ real_dir;
}

}

Call example:

The code is as follows:

$ InputName = 'uploadfile ';
// That isThe name value in. this parameter is optional.
$ Upload = new upload ($ inputName );
$ New_dir = "/www"; // path to which the object is moved
$ Upload-> moveTo ($ new_dir );


Iframe to implement upload is also refreshing. if you need it, refer to. First, ajax does not...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.