carl
carl

Reputation: 396

How to move/copy a file that has a wildcard in the name in PHP?

I am trying to copy a file that I download it. The file name is test1234.txt, but I want to access it using a wildcard like this: test*.txt and after that to move it to another folder (because I don't know how the file name looks like, but I know that the beginning is test and the rest is changing every time I download a new one). I tried some codes:

$myFile = 'C:/Users/Carl/Downloads/'. date("y-m-d") . '/test*.txt';
$myNewFile = 'C:/Users/Carl/Downloads/'. date("y-m-d").'/text.xml';
    if(preg_match("([0-9]+)", $myFile)) {
        echo 'ok';
        copy($myFile, $myNewFile);
    }

I am getting an error because of * in $myFile. Any help is very appreciated.

Upvotes: 0

Views: 680

Answers (2)

PetitCitron
PetitCitron

Reputation: 41

For complete response, if you want to only move *.txt in NewFolder.

$myFiles  = 'C:/Users/Carl/Downloads/*.txt';
$myFolderDest = 'C:/Users/Carl/NewFolder/'; 

foreach (glob($myFiles) as $file) {
    copy($file, $myFolderDest . basename($file));
}

Upvotes: 0

carl
carl

Reputation: 396

$myFile= 'C:/Users/Carl/Downloads/'. date("y-m-d") . '/test*.txt';
$myNyFile = 'C:/Users/Carl/Downloads/'.date("y-m-d").'/test.txt'; 

   foreach (glob($myFile) as $fileName) {
      copy($fileName, $myNyFile);
   }

Upvotes: 2

Related Questions