问题描述:

I am trying to copy lets say two files with name test1234.txtand test2431.txt from a folder to another. I want to copy them and change the filenames to test1.txtand test2.txt. I wrote a kode for that, but I am getting all the time this error: Warning: copy() expects parameter 2 to be a valid path, array given in. Here is the code:

$src = glob('C:/Users/Carl/Downloads/'.date("ymd").'/test*.txt');

$dest = glob('C:/Users/Carl/Downloads/test*.txt');

foreach ($src as $value) {

if(preg_match("([0-9]+)", $value)) {

$arr = array($value);

foreach($arr as $arrValue) {

copy($arrValue, $dest);

}

}

I tried many codes, but I cannot figure out how to eliminate this erroe. Any help is very appreciate.

网友答案:

Your code has more that one problem:

$src = glob('C:/Users/Carl/Downloads/'.date("ymd").'/test*.txt');

This first line is right, I suppose. It will return an array like this:

[
    C:/Users/Carl/Downloads/160220/test1.txt,
    C:/Users/Carl/Downloads/160220/test2.txt,
    C:/Users/Carl/Downloads/160220/test1234.txt
]
$dest = glob('C:/Users/Carl/Downloads/test*.txt');

This line act like previous line. It will return an array like this:

[
    C:/Users/Carl/Downloads/test1.txt,
    C:/Users/Carl/Downloads/test2.txt,
    C:/Users/Carl/Downloads/test1234.txt
]

This means that — if you use this array to set the destination names — this files will be overwritten. Otherwise, if there are not files like test*.txt in Downloads directory, the array is empty and files cannot be copied.

With this loop you perform a foreach loop through all src array elements:

foreach ($src as $value) {

So, following line is completely useless (you retrieve array value by a glob with '160220', so all rows match the [0-9]+ pattern:

    if(preg_match("([0-9]+)", $value)) 
    {

The following lines are incomprensibles: why you assign $value to an array to perform a foreach loop? The following code can performed simply writing copy( $value, ... ).

In other words, when $value is 'C:/Users/Carl/Downloads/160220/test1234.txt' (i.e.), the array is [ 'C:/Users/Carl/Downloads/160220/test1234.txt' ]

        $arr = array($value);
             foreach($arr as $arrValue) 
        {
             copy($arrValue, $dest);

The last line fails anyway, because $dest is an array, and you can't set destination value as array. You have to set it to a valid path string.

If you want copy files to a new destination without overwrite existing files you have to try in this way:

$src        = glob( 'C:/Users/Carl/Downloads/'.date("ymd").'/test*.txt' );
$destPrefix = 'C:/Users/Carl/Downloads/test';

$i = 1;
foreach( $src as $value )
{
    while( file_exists( "{$destPrefix}{$i}.txt" ) ) $i++;
    copy( $value, "{$destPrefix}{$i}.txt" );
}

By this way, in the while loop, you search if file Downloads/test1.txt exists; if they exists, increment $i by 1 and search if file Downloads/test2.txt exists, etc.. When the filename doesn't exists, you can copy your source to destination.

Otherwise, if you want overwrite existing files, you can act as you original code:

$src  = glob( 'C:/Users/Carl/Downloads/'.date("ymd").'/test*.txt' );
$dest = glob('C:/Users/Carl/Downloads/test*.txt');
foreach( $src as $key => $value )
{
    copy( $value, $dest[$key] );
}

but, if total file number matched in $dest are less than $src files, your copy command will fail.

Edit:

  1. Please note that with copy your original files will not be deleted;
  2. If you want copy files preserving their names you can use this command: copy( $value, 'C:/Users/Carl/Downloads/'.basename($value));
网友答案:

the result of glob is an array so you should use a key to specify a unique value:

$src = glob('C:/Users/Carl/Downloads/'.date("ymd").'/test*.txt');
$dest = glob('C:/Users/Carl/Downloads/test*.txt');
    foreach ($src as $value) {
        if(preg_match("([0-9]+)", $value)) {
             $arr = array($value);
             foreach($arr as $key => $arrValue) {
                copy($arrValue, $dest[$key]);  // <= here
             }
        }
    }

if you have no file in the destination do like this:

$src = glob('C:/Users/Carl/Downloads/'.date("ymd").'/test*.txt');
$base_dest = 'C:/Users/Carl/Downloads/';
    foreach ($src as $value) {
        if(preg_match("([0-9]+)", $value)) {
             $arr = array($value);
             foreach($arr as $arrValue) {
                $info = pathinfo($arrValue);
                $dest = $base_dest . $info['basename'];
                copy($arrValue, $dest);  // <= here
             }
        }
    }
相关阅读:
Top