PHP: randomly reading files

Source: Internet
Author: User
A directory contains 10000 txt files. How can I achieve this if I want to randomly read 100 of them? A directory contains 10000 txt files. How can I achieve this if I want to randomly read 100 of them?

Reply content:

A directory contains 10000 txt files. How can I achieve this if I want to randomly read 100 of them?

Traverse all the files and generate an array of file names. array_rand is randomly extracted from the files. Then you can open the files one by one.

Inefficient. You can try to save the file name list

  1. Files without indexes can only be traversed to obtain the index at random.GlobThe official function tests the next 10000 text files, which is about time-consuming.0.25 sIt's a little long, but it's okay. After the index0-10000You just need to randomly retrieve 100 numbers. In addition, you can save the indexes for the next time. You can re-generate the indexes each time you add files.
  2. If you can rename a file,.Rename and random fetch1-10000If the number is 100, it is okay.

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.