Php uploads images in batches and puts the image names in the database

Source: Internet
Author: User
Tags glob

A few days ago, I was working on this function. more than eight hundred members were generated by more than eight hundred systems. I uploaded images to more than system members, and then put the image names in the database.

Step 1:

The first step is to upload the image to the corresponding image directory and directly use the existing upload class in the framework:

 Set_ext (array ('zip '); $ path = 'Directory name'; if (! Io: mkdir ($ path) // create directory {throw new Exception ("unable to Create File Upload Directory: $ path ");} $ upload-> set_path ($ path); if (! $ Upload-> is_allow_ext ($ _ FILES ['files '] ['name']) {$ this-> show_message ('zip-format data required', '0 ', NULL, TRUE) ;}$ result = $ upload-> save ($ _ FILES ['files']); $ archive = new Archive_Zip (); $ archive-> set_target ($ path)-> decompress ($ result ['file']); unlink ($ result ['file']); // Delete the used zip; $ this-> show_message ('imported successfully', '1', array ('text' => 'return to import page ', 'href '=>' *** jump link address *** '), TRUE);} catch (Exception $ e) {$ this-> show_me Ssage ('image import failed', '0', NULL, TRUE) ;}}?>

Step 2:

After the image is uploaded, retrieve the image names of all system members in the directory, and traverse the image. Think about it and you don't have to worry about it. php comes with the glob function ();

The glob () function returns a file name or directory that matches the specified mode.

This function returns an array containing matching files/directories. If an error occurs, false is returned.


 

In the loop process, because I only want to get the image name, I replaced the previous directory with null.



Step 3:

After listing all the images, you should insert the image file name into the database.

Write a loop.

First, SELECT ........ Find out the system members and calculate the number of system members,

 Execute ()-> as_array ();?>


Then recycle and insert it into the database:

 $ Files [$ I],); DB: update ('Member table')-> set ($ data)-> where ('System member id', '= ', $ member [$ I] ['System member id'])-> execute ();}?>


OK. That's it.

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.