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?

5

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

1 Comment

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'
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

1 Comment

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.
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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.