Good all the time of day, gentlemen! Need advice php guru.

There is a tmp directory on the site, inside there are files that have known names (from where), but extensions are not written. The algorithm is this, we know the first part of the name - you need to find if there are files starting with this name, and if there is any, let's say it starts at f931992748863024fda1b647080e8ffa (I suspect that in the past it was session_id, but not the essence)

Tell me how to find all the files that start with the specified name? (there may be several, maybe 1, or maybe not). file_exists() , unfortunately, requires an exact match. The second part of the files is usually _1.txt , _5.docx ... in this format, i.e. underscore, digit, period, and some extension. Would love to get an answer on php

ps to form an array about the contents of the directory, and then sort through it - I think it is not an option, because specifically in my catalog more than 10 thousand files. It will take a lot of time.

  • Sounds like downloading files :) - atnartur
  • one
    It seems, but nevertheless it is a search when the first part of the name is known. atnartur, help with the code? 8-) - sergey
  • After: forming an array with 10k lines and going through it not only for a long time, but also eat up a lot of memory. - zenith
  • ls **/somefile* - neoascetic
  • full example would be even more eloquent 8-) - sergey


2 answers 2

As an option:

 $dir = opendir($path); $string_to_search='f931992748863024fda1b647080e8ffa'; while(($file = readdir($dir)) !== false) { $a[] = strstr($file,$string_to_search); } print_r($a); closedir($dir); 
  • thanks, what you need. - sergey

Because This is the only answer, and it works, I upgraded a little.

 <? // где - откроем $dir = opendir("tmp"); //начинается на $string_to_search='f931992748863024fda1b647080e8ffa'; // ищем $mass_sas[] = ""; while(($file = readdir($dir)) !== false) { $mass_sa = strstr($file,$string_to_search); if($mass_sa != "") { $mass_sas[] = $mass_sa; } } // где - закроем closedir($dir); // если охота посмотреть что вышло echo "<pre>"; print_r($mass_sas); echo "</pre>"; ?>