Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #15914

Re: Fast PHP way to find a file given the leftmost characters of the file name

From Jerry Stuckle <jstucklex@attglobal.net>
Newsgroups comp.lang.php
Subject Re: Fast PHP way to find a file given the leftmost characters of the file name
Date 2015-12-05 15:53 -0500
Organization A noiseless patient Spider
Message-ID <n3vinb$39g$1@dont-email.me> (permalink)
References <n3v7c9$ko9$1@dont-email.me> <n3vbbr$4eh$1@speranza.aioe.org> <87egf0k7bd.fsf@ahungry.com>

Show all headers | View raw


On 12/5/2015 2:45 PM, Matthew Carter wrote:
> Markus Heinz <markus.heinz@uni-dortmund.de> writes:
> 
>> Hello.
>>
>> On 2015-12-05 at 18:39 James Harris wrote:
>>> Basic query: What is the best way in PHP to find a file given just the
>>> leftmost characters of its name? I am looking for something that will
>>> scale so that it can be expected to execute quickly even if there are
>>> thousands of files in a given directory.
>> [...]
>>> Any comments? Even if you think the idea is a bad one I would appreciate
>>> the feedback.
>>
>> The glob function might be helpful to accomplish your goal:
>> <http://de2.php.net/manual/en/function.glob.php>
>>
>> Another alternative might be to store the full filenames in a database
>> table and then do a SQL query like the following:
>>
>> SELECT filename FROM files WHERE filename LIKE 'prefix%'
>>
>> In this query "prefix" is the prefix which is being searched for and
>> the query will return all complete filenames matching this prefix.
>>
>> Which solution scales better should be examined in a setup like the
>> target environment and is influenced by parameters such as number of
>> files, filesystem type, available RAM, CPU speed etc.
>>
>>> James
>>
>> Regards
>>
>> Markus
>>
> 
> FWIW, I just tested in a directory with 50,000 files on a system with
> 1.5G RAM (and non-SD disk) and was able to get 190 matches when
> specifying the first 2 letters in 0.103 seconds using
> glob($letters.'*'), as well as similar results when specifying all the
> way to a single unique name.
> 
> So, I would stick with glob vs attempting to over-engineer it (if you
> are working with real files and not just database content), as the only
> reason to micro-optimize would be if you had extremely high traffic (in
> which case I think you could afford the $20 or less a month to just get
> an SD Linode, where the cost of disk I/O is almost non-existent).
> 

Matthew, I tend to agree with you.  Even if there are multiple levels of
directories, it should still be pretty fast if done right.  I think the
OP is falling into the premature optimization trap.

Do it with clear and easy to understand code and see if there is a
problem.  Chances are there will not be.  IFF there, solve the problem.

Using a database sounds fine at first.  But it can be difficult to
ensure the file system and the database are always in sync.  This is
especially true if the OP allows (or uses) ftp.

-- 
==================
Remove the "x" from my email address
Jerry Stuckle
jstucklex@attglobal.net
==================

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fast PHP way to find a file given the leftmost characters of the file name James Harris <james.harris.1@gmail.com> - 2015-12-05 17:39 +0000
  Re: Fast PHP way to find a file given the leftmost characters of the file name Arno Welzel <usenet@arnowelzel.de> - 2015-12-05 19:37 +0100
  Re: Fast PHP way to find a file given the leftmost characters of the file name Markus Heinz <markus.heinz@uni-dortmund.de> - 2015-12-05 19:45 +0100
    Re: Fast PHP way to find a file given the leftmost characters of the file name Matthew Carter <m@ahungry.com> - 2015-12-05 14:45 -0500
      Re: Fast PHP way to find a file given the leftmost characters of the file name Jerry Stuckle <jstucklex@attglobal.net> - 2015-12-05 15:53 -0500
      Re: Fast PHP way to find a file given the leftmost characters of the file name James Harris <james.harris.1@gmail.com> - 2015-12-06 00:50 +0000
        Re: Fast PHP way to find a file given the leftmost characters of the file name Matthew Carter <m@ahungry.com> - 2015-12-07 00:17 -0500
          Re: Fast PHP way to find a file given the leftmost characters of the file name James Harris <james.harris.1@gmail.com> - 2015-12-08 11:45 +0000

csiph-web