PHP + JavaScript allows you to upload images without refreshing, phpjavascript

Source: Internet
Author: User

PHP + JavaScript allows you to upload images without refreshing, phpjavascript

Html file code

<! -- Ajax File Upload starts -->
<Script type = "text/javascript" src = "/imageupload/jquery-1.10.2.min.js"> </script>
<Script type = "text/javascript" src = "/imageupload/layer. js"> </script>
<Script type = "text/javascript" src = "/imageupload/ajaxupload. js"> </script>
<! -- Ajax File Upload ends -->

 

<! -- Start the Upload File button list -->
<Input id = "requesturl" type = "hidden" value = "{: U ('admin/upload/uploadfile')}"/>
<Input id = "ajaxuploadfile" type = "file" onchange = "filechange ()"/>
<Input id = "filepathurl" type = "hidden" value = ""/>
<Input type = "button" value = "first" pathurl = "./Uploads/admin/trailer/" class = "uploadclass"/>
<Input type = "button" value = "second" pathurl = "./Uploads/admin/fdfdfd/" class = "uploadclass"/>
<Input type = "button" value = "third" pathurl = "./Uploads/admin/cdcdfd/" class = "uploadclass"/>
<! -- The Upload File button list ends -->

PHP file code

/**
* File Upload Method
*/
Public function uploadfile (){
// Upload a single file
$ Upload = new \ Think \ Upload (); // instantiate the upload class
$ Upload-> maxSize = 100000000; // sets the attachment upload size.
$ Upload-> exts = array ('jpg ', 'gif', 'png', 'jpeg '); // you can specify the attachment upload type.
$ Url = $ _ POST ['filepathurl'];
If (! File_exists ($ url )){
Mkdir (USD url, 0777, true );
}
$ Upload-> rootPath = $ url; // you can specify the root directory for uploading attachments.
// Upload a single object
$ Info = $ upload-> uploadOne ($ _ FILES ['postfilename']);
If (! $ Info ){
Echo json_encode (array ('bool '=> false, 'error' => $ upload-> getError ()));
} Else {
$ Path = $ info ['savepath']. $ info ['savename'];
Echo json_encode (array ('bool '=> true, 'path' => $ path ));
}
}

 

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.