smrtdud Posted September 2, 2007 Share Posted September 2, 2007 could some help in writing a php code for copying specific files that start with 'thumb_' only and no other file. like we copy files with specific extension i want to copy files with specific starting. guys plz help. thanks Quote Link to comment https://forums.phpfreaks.com/topic/67614-solved-copy-file-with-a-specific-begining-like-thumb_/ Share on other sites More sharing options...
trq Posted September 2, 2007 Share Posted September 2, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/67614-solved-copy-file-with-a-specific-begining-like-thumb_/#findComment-339663 Share on other sites More sharing options...
smrtdud Posted September 2, 2007 Author Share Posted September 2, 2007 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 Quote Link to comment https://forums.phpfreaks.com/topic/67614-solved-copy-file-with-a-specific-begining-like-thumb_/#findComment-339721 Share on other sites More sharing options...
smrtdud Posted September 5, 2007 Author Share Posted September 5, 2007 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); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/67614-solved-copy-file-with-a-specific-begining-like-thumb_/#findComment-341860 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.