Jump to content

[SOLVED] copy file with a specific begining like 'thumb_'


Recommended Posts

This will copy all files beginning with thumb_ to a directory called thumbs.

 

<?php

  foreach (glob("thumb_*") as $filename) {
    if (copy($filename,'thumbs/'.$filename)) {
      echo "$filename copied to thumbs/$filename<br />";
    } else {
      echo "Unamble to copy $filename to thumbs/$filename<br />";
    }
  }

?>

 

Hope it helps.

thanks for the reply

 

this is the code i am using but its not reading the content of the folder, it does  not read the file in a folder the code that u gave works fine for a single dir, but i want to implement it for subdirs also.

 

<html><body>

<form action="copy.php" method="GET">
Source: <input type="text" name="source" />
Dest: <input type="text" name="dest" />
<input type="submit" />
</form>
</body></html>
<?php

function full_copy( $source, $target )
    {
        if ( is_dir( $source ) )
        {
           @mkdir( $target );
           echo "<b>Creating dir:</b>".$target." <br/>";
            $d = dir( $source );
           
            while ( FALSE !== ( $entry = $d->read() ) )
            {
                if ( $entry == '.' || $entry == '..' )
                {
                    continue;
                }
               
                $Entry = $source . '/' . $entry;           
                if ( is_dir( $Entry ) )
                {
                  full_copy( $Entry, $target . '/' . $entry );
                  echo "<b>Copying file: </b> ".$target . '/' . $entry ."<br />" ;
                  continue;
                }

foreach (glob("thumb_*") as $filename) {
    if (copy($Entry.'/'.$filename,$target . '/' . $entry .'/'.$filename)) {
      echo "$filename copied to thumbs/$filename<br />";
    } else {
      echo "Unamble to copy $filename to thumbs/$filename<br />";
    }
  }
                  }
           
            $d->close();
        }else
        {
  foreach (glob("thumb_*") as $filename) {
    if (copy($source.'/'.$filename,$target . '/'.$filename)) {
      echo "$filename copied to thumbs/$filename<br />";
    } else {
      echo "Unamble to copy $filename to thumbs/$filename<br />";
    }
  }


        }

}

//*****************************

if (isset($_GET["source"]) && isset($_GET["dest"]))
{
$sr= $_GET["source"];
$ds= $_GET["dest"];
} else
{
$sr= "";
$ds= "";

}
if (!$sr == "" && !$ds == "") {
echo full_copy($sr, $ds);
}


?>

 

could u plz tell me the eror in the above code or correct it.

thanks

i finally found the solution and i am using this code below

 

<html><body>

<form action="copy.php" method="GET">
Source: <input type="text" name="source" />
Dest: <input type="text" name="dest" />
<input type="submit" />
</form>
</body></html>
<?php

set_time_limit(3700);
echo ini_get('max_execution_time');

function full_copy( $source, $target )
    {
        if ( is_dir( $source ) )
        {
           @mkdir( $target );
           echo "<b>Creating dir:</b>".$target." <br/>";
            $d = dir( $source );
            while ( FALSE !== ( $entry = $d->read() ) )
            {
                if ( $entry == '.' || $entry == '..' )
                {
                    continue;
                }
               
                $Entry = $source . '/' . $entry;           
                if ( is_dir( $Entry ) )
                {
                  full_copy( $Entry, $target . '/' . $entry );
                  echo "<b>Copying file: </b> ".$target . '/' . $entry ."<br />" ;
                  continue;
                }
}
  $dir = @opendir( dirname($Entry) );
echo "<font color=red><h2>currrent dir is ".dirname($Entry)." <br></h2></font>";
   foreach (glob(dirname($Entry).'/thumb_*.*') as $filename) {
                copy( $filename, $target.'/'.basename($filename)  );
                echo "<b>Copying file :$filename to-- </b>". $target.'/'.basename($filename). "<br />" ;
            } 



closedir( $dir );                            
            $d->close();
        }else
        {
$dir = @opendir( dirname($Entry) );
echo "<font color=red><h2>currrent dir is ".dirname($Entry)." <br></h2></font>";
   foreach (glob(dirname($Entry).'/thumb_*.*') as $filename) {
                copy( $filename, $target.'/'.basename($filename)  );
                echo "<b>Copying file :$filename to-- </b>". $target.'/'.basename($filename). "<br />" ;
            } 



closedir( $dir );                            
        }

}

//*****************************

ini_set("session.gc_maxlifetime", 60*60); 
$currentTimeoutInSecs = ini_get("session.gc_maxlifetime");
echo $currentTimeoutInSecs;
if (isset($_GET["source"]) && isset($_GET["dest"]))
{
$sr= $_GET["source"];
$ds= $_GET["dest"];
} else
{
$sr= "";
$ds= "";

}
if (!$sr == "" && !$ds == "") {
echo full_copy($sr, $ds);
}


?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.