Answer the question
In order to leave comments, you need to log in
glob() function in PHP?
Just recently, a project required a function to search for files by glob() mask. But for now, in thought, is it worth using it, and how much it loads the server.
The task is as follows:
There is an object, let's say a car, it has 20-30 photos, which are stored in 5 folders with different sizes. Accordingly, if there are 20-30,000 photographic objects in the database, there can be approximately 150-200,000 files. The mask search will be performed something like this: "/path/photos/[obj_id]_*.jpg" So, something really confuses me in using this function ... And I can't find information about loads anywhere.
Answer the question
In order to leave comments, you need to log in
Create 200k files:
#!/bin/bash
dir="/test"
ctr=1
range=30000
while [ ${ctr} -lt 200001 ]; do
id=${RANDOM}
let "id %= ${range}"
`touch "${dir}/${id}_${ctr}.jpg"`
let ctr=ctr+1
done
~> ./test_glob
~> ls -1 /test | wc -l
200000
<?php
$dir = '/test/';
$id = rand( 1, 30000 );
$pattern = $dir . $id . '_*.jpg';
$time_start = microtime(true);
$photos = glob( $pattern );
$time_end = microtime(true);
$time = $time_end - $time_start;
echo 'execution time is ', $time, ' seconds', PHP_EOL;
print_r($photos);
~> php ./test_glob.php
execution time is 0.15708804130554 seconds
Array
(
[0] => /test/23513_10050.jpg
[1] => /test/23513_10631.jpg
[2] => /test/23513_121888.jpg
[3] => /test/23513_150044.jpg
[4] => /test/23513_167985.jpg
[5] => /test/23513_185798.jpg
[6] => /test/23513_188480.jpg
[7] => /test/23513_193143.jpg
[8] => /test/23513_68603.jpg
)
IMHO, storing 200,000 files in a folder is not the best idea. I would make a folder structure like 12/34/56/78/90/*.jpg, where 1234567890 is obj_id. Although, of course, it strongly depends on the file system and free memory on the server
It will be expensive to perform it, but you can test it yourself (a small test script). It is better to index the current files into the database, and add new ones there immediately after adding ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question