Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在PHP中移动/复制名称中包含通配符的文件?_Php_File_Path - Fatal编程技术网

如何在PHP中移动/复制名称中包含通配符的文件?

如何在PHP中移动/复制名称中包含通配符的文件?,php,file,path,Php,File,Path,我试图复制一个我下载的文件。文件名是test1234.txt,但我想使用如下通配符访问它:test*.txt,然后将其移动到另一个文件夹(因为我不知道文件名是什么样子,但我知道开头是test,其余的每次下载新文件时都在更改)。我尝试了一些代码: $myFile = 'C:/Users/Carl/Downloads/'. date("y-m-d") . '/test*.txt'; $myNewFile = 'C:/Users/Carl/Downloads/'. date("y-m-d").'/te

我试图复制一个我下载的文件。文件名是
test1234.txt
,但我想使用如下通配符访问它:
test*.txt
,然后将其移动到另一个文件夹(因为我不知道文件名是什么样子,但我知道开头是
test
,其余的每次下载新文件时都在更改)。我尝试了一些代码:

$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);
    }

由于
$myFile
中的
*
,我收到一个错误。非常感谢您的帮助。

如果您只想在NewFolder中移动*.txt,请获取完整的响应

$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);
   }
$myFiles='C:/Users/Carl/Downloads/*.txt';
$myFolderDest='C:/Users/Carl/NewFolder/';
foreach(glob($myFiles)作为$file){
复制($file,$myFolderDest.basename($file));
}

获取完整响应,如果您只想在NewFolder中移动*.txt

$myFiles='C:/Users/Carl/Downloads/*.txt';
$myFolderDest='C:/Users/Carl/NewFolder/';
foreach(glob($myFiles)作为$file){
复制($file,$myFolderDest.basename($file));
}

您在这方面做过尝试吗?是的。该文件存在,但我无法获取它(因为数字是部分的)。请显示您实际尝试使用
glob()
解析通配符的代码。$myFile='C:/Users/Carl/Downloads/'。日期(“y-m-d”)。'/测试*.txt'$myNyFile='C:/Users/Carl/Downloads/'.date(“y-m-d”)。/test.txt';foreach(glob($myFile)作为$fileName){copy($myFile,$myNyFile);}
@mario
感谢您的评论/帮助。我现在知道我哪里做错了。如果你再回信,我会接受最好的。你在这方面试过了吗?是的。该文件存在,但我无法获取它(因为数字是部分的)。请显示您实际尝试使用
glob()
解析通配符的代码。$myFile='C:/Users/Carl/Downloads/'。日期(“y-m-d”)。'/测试*.txt'$myNyFile='C:/Users/Carl/Downloads/'.date(“y-m-d”)。/test.txt';foreach(glob($myFile)作为$fileName){copy($myFile,$myNyFile);}
@mario
感谢您的评论/帮助。我现在知道我哪里做错了。如果你再发一次作为答复,我会接受它为最好的。感谢
@Ghost
@mario
感谢
@Ghost
@mario