Thinkphp (php) + Ajax Upload Images

Source: Internet
Author: User
: This article mainly introduces thinkphp (php) + Ajax to upload images. For more information about PHP tutorials, see. External plug-ins used:

  • Jquery. form. js (form submission): http://malsup.com/jquery/form/#download

  • Jquery. json. min. js (parsing json data): http://www.oschina.net/p/jquery-json

Form code:

"show_photo_upload_img" src="" alt=""/>

Js code:

Php in the background:

FunctionfileUpload () {$ config = C ('File _ UPLOAD_CONFIG '); // The attached information $ request_data = I ('post. '); // show_bug ($ request_data); $ member_id = $ request_data ['Member _ id']; if (empty ($ the_file_usage )) {$ the_file_usage = $ file_usage ['download'];} // show_bug_with_exit ($ file_name); $ file_info ['Member _ id'] = $ member_id; $ file_info ['created _ time'] = time (); // Upload a file // instantiate the Upload class and input the configuration array above $ uploader = new Upload ($ config, 'loca L '); // $ uploader-> saveName = $ file_uuid; $ info = $ uploader-> upload ($ _ FILES); // show_bug_with_exit ($ info ); // Determine whether the upload is successful. if ($ info) {// obtain the information about the uploaded file after the upload is successful ($ infoas & $ file) {// concatenate the upload directory $ file ['rootpath'] =. ltrim ($ config ['rootpath'], ". "); // concatenate the relative path of the file. $ file ['filepath'] = $ file ['rootpath']. $ file ['savepath']. $ file ['savename'];} // You can output the result here, the key name of the relative path is $ info ['upload'] ['filepath'] $ filepath = $ file ['Fil Epath ']; // show_bug_with_exit ($ filepath); $ file_info ['File _ path'] = $ filepath; $ save_file_in_DB = $ this-> savefileinfo1_db ($ file_info ); // delete the object if (! $ Save_file_in_DB) {unlink ($ filepath); $ return_data ['error'] = 'File Upload failed. please try again '; echo json_encode ($ return_data );} $ return_data ['error'] = 'File uploaded successfully'; $ return_data ['IMG _ path'] = $ filepath; $ return_data ['IMG _ id'] = $ save_file_in_DB; jsonReturn ($ return_data);} else {// output error message $ error_msg = $ uploader-> getError (); $ return_data ['error'] = $ error_msg; jsonReturn ($ return_data) ;}} functionsaveFileInfoIntoDB ($ fil E_info) {$ file = M ('file'); $ rs_u_file = $ File-> add ($ file_info); $ file_id = $ file-> getLastInsID (); if (! $ Rs_u_file) {returnfalse;} return $ file_id ;}

Configuration File:

'File _ UPLOAD_CONFIG '=> array ('mimes' => '', // The MiMe type of the FILE to be uploaded 'maxsize' => 6*1024*1024, // the size limit of the uploaded file (0-No limit) 'exts' => array ('jpg ', 'GIF', 'PNG', 'jpeg '), // Set the attachment Upload type 'autosub' => true, // The automatic subdirectory stores the file 'subname' => array ('date', 'Y-m-D '), // subdirectory creation method, [0]-function name, [1]-parameter. multiple parameters use the array 'rootpath' => '. /Uploads/', // Save the root path 'savepath' => '', // Save the path 'savename' => array ('uniqid ',''),),

Function. php file

/*** Return json data to the client * @ access protected * @ param mixed $ data the data to be returned * @ return void */functionjsonReturn ($ data) {$ json_str = json_encode ($ data); // return the header containing status information ('content-Type: application/JSON; charset = utf-8 ') in json data format to the client '); // process the 'null' contained in json and replace it with an empty string $ search = 'null'; $ replace = '""'; $ returndata = str_replace ($ search, $ replace, $ json_str); // testadddata=testtable (null, $ returndata); exit ($ returndata );}

The above introduces thinkphp (php) + Ajax upload pictures, including the content, hope to be helpful to friends who are interested in PHP tutorials.

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.