473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

file_exists and thousand of files

I've a site working as follows

Table "articles"
[id]
[name]
[...more fields]

When somebody want to see the article's details, I generate (thorugh
php) a page where all the detalis are listed, as well as the article's
views (images)

If the articles's id is $id, then, I search for its images this way:

for ($i=1;$i<11;$i++){
$filename= $path.$id.'_'.$i.'.jpg';

if (file_exists($filename)) {
Show the image here
}

}///for
This let me upload from 1 to 10 views, and show them as long as they
are in the sever

Pretty easy as you can see, but, the problem is that there could be (in
fact, there will be) more that 10000 images in the same directory.
I'm wondering whether there will be some kind of speed-problem with
file_exists function using it to search among such a number of files...

Had anyone experiences/problems with this ?

regards - jm

Jun 13 '06 #1
2 1250
julian_m wrote:
I've a site working as follows

Table "articles"
[id]
[name]
[...more fields]

When somebody want to see the article's details, I generate (thorugh
php) a page where all the detalis are listed, as well as the article's
views (images)

If the articles's id is $id, then, I search for its images this way:

for ($i=1;$i<11;$i++){
$filename= $path.$id.'_'.$i.'.jpg';

if (file_exists($filename)) {
Show the image here
}

}///for
This let me upload from 1 to 10 views, and show them as long as they
are in the sever

Pretty easy as you can see, but, the problem is that there could be (in
fact, there will be) more that 10000 images in the same directory.
I'm wondering whether there will be some kind of speed-problem with
file_exists function using it to search among such a number of files...

Had anyone experiences/problems with this ?

regards - jm


Hi,

Good question.
If PHP is scanning through the directory 10 times in a row, you are clearly
wasting precious CPU.
Some OS's can be helpfull by having their filesystem indexed, making your
approach doable.

I think you could do a general faster job by trying to match a substring to
the filename in a certain directory, and do it once, not 10 times.
That is sometimes refered to as 'globbing'.
I know this from Perl, but I just saw it exists in PHP under the name....
glob. What a surpise. ;-)

So go to www.php.net and look for the function glob.

Good luck

Regards,
Erwin Moller
Jun 13 '06 #2
Erwin Moller wrote:
I think you could do a general faster job by trying to match a substring to
the filename in a certain directory, and do it once, not 10 times.
That is sometimes refered to as 'globbing'.
I know this from Perl, but I just saw it exists in PHP under the name....
glob. What a surpise. ;-)


Somehow I doubt you can do a better job than the OS. Typically a binary
search is used for directory look-up. Finding one file amidst a
thousand wouldn't take more than 10 string comparisons. So for ten
look-ups the OS would do no more than 100 comparisons. Unless you write
a b-search routine in PHP yourself, you'll certainly do worse. A search
with in_array() or array_search() would require on average 500
comparisons per look-up. With a hash-table it's better, but then the
construction of it would require generating a 1000 hash keys. And if
you're on Windows you have case-sensitivity to consider...

Jun 13 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Andrew Crowe | last post by:
Hi guys, I've created this little function to check whether a user has uploaded a file with the same name as an existing file, and if so rename it to file-1.jpg, file-2.jpg etc. ...
10
by: Google Mike | last post by:
{NOTE: I have PHP 4.2.2 for RH9 Linux.} Anyone have a better file_exists() out there? Even if you use shell out tricks with Linux using the `command` trick, I'd be interested to see what you...
1
by: Hinrich Specht | last post by:
Hello, I have a problem using file_exists. I want to use file_exists to dertermine if a product-image is available or not to show either the product-image or a standard-image. This is the code:...
3
by: annie | last post by:
Hi I've got two linux boxes running redhat9 and either PHP Version 4.2.2 or PHP Version 4.3.7. The file structure is set up the same on both machines and both files exist. The problem is not...
5
by: lkrubner | last post by:
I've written some template code and one thing I'm trying to protect against is references to images that don't exist. Because users have the ability to muck around with the templates after...
6
by: +86 | last post by:
i encountered this problem: "include('inc.php')" will work problely but "include('./inc.php') doesn't work .. both file_exists('inc.php') or file_exists('./inc.php') didn't return the right...
2
by: roger.moon2 | last post by:
So I wrote a script to run via crontab but when it tries to check whether a file exists like below: if (!file_exists("settings.ini")) return false; it checks whether the file exists in $HOME...
9
by: damezumari | last post by:
$file = 'C:\Documents and Settings\Michael\Escritorio\Elvis\test.txt'; file_exists($file) and file_exists('"'.$file.'"') both return false although the file does exist on the user's machine....
3
by: rickcasey | last post by:
I'm using PHP 4.4.4-8 on Debian Linux on a low traffic site, and need to detect if a file exists, and if so, unlink it. I am mystified as to why the file_exists() function does not work in one...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.