0

I merely have the file name, without extension (.txt, .eps, etc.) The directory has several subfolders. So, the file could be anywhere.

How can I seek the filename, without the extension, and copy it to different directory?

James B
  • 8,183
  • 4
  • 33
  • 40
Kel
  • 309
  • 1
  • 8
  • 25
  • Wouldn't you need to know the extension? What would happen if you wanted to copy `text.txt` but the program found and copied `text.doc`? – Anthony Forloney Apr 16 '10 at 15:10
  • possible duplicate of http://stackoverflow.com/questions/1860393/recursive-file-search-php – Gordon Apr 16 '10 at 15:12
  • that's actually a good point. what if the file name is unique? – Kel Apr 16 '10 at 15:13
  • *(reference)* http://de.php.net/manual/en/class.recursivedirectoryiterator.php and http://de.php.net/manual/en/function.pathinfo.php and http://de.php.net/manual/en/function.copy.php – Gordon Apr 16 '10 at 15:13
  • I tried it out, it can't find my network drive. what should I do? – Kel Apr 16 '10 at 15:32

4 Answers4

2

http://www.pgregg.com/projects/php/preg_find/preg_find.php.txt seems to be exactly what you need, to find the file. then just use the normal php copy() command http://php.net/manual/en/function.copy.php to copy it.

chris
  • 9,745
  • 1
  • 27
  • 27
1

https://stackoverflow.com/search?q=php+recursive+file

Community
  • 1
  • 1
Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • It can't find network drives: Fatal error: Uncaught exception 'UnexpectedValueException' with message 'RecursiveDirectoryIterator::__construct(X:\apps)... failed to open dir: No such file or directory' – Kel Apr 16 '10 at 15:31
0

have a look at this http://php.net/manual/en/function.copy.php

as for seeking filenames, could use a database to log where the files are? and use that log to find your files

studioromeo
  • 1,563
  • 12
  • 18
  • i need to find the file first in the directory. For example: I have the file name: iamfile01. I need to find it first... I know it is on hard drive X: but no idea in which directory it could be in. – Kel Apr 16 '10 at 15:11
0

I found that scandir() is the fastest method for such operations:

function findRecursive($folder, $file) {
    foreach (scandir($folder) as $filename) {
        $path = $folder . '/' . $filename;
        # $filename starts with desired string
        if (strpos($filename, $file) === 0) {
            return $path;
        }
        # search sub-directories
        if (is_dir($path)) {
            $result = findRecursive($path);
            if ($result !== NULL) {
                return $result;
            }
        }
    }
}

For copying the file, you can use copy():

copy(findRecursive($folder, $partOfFilename), $targetFile);
soulmerge
  • 73,842
  • 19
  • 118
  • 155