Copy folder problem...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bobcooper
Forum Newbie
Posts: 2
Joined: Wed Oct 01, 2008 11:14 am

Copy folder problem...

Post by bobcooper »

Hi there,

I am having a problem copying a folder and its sub folders. When I try the script on my computer it works fine, but when I upload it to my website it only creates a new folder, it does not copy the files. I was thinking it might be something to do with permissions so added chmod settings but that does not work either.

Here is the code:

Code: Select all

<?php
 
 
$dir = "../";
$count = 0;
 
if(is_dir($dir)) {
    if($handle = opendir($dir)) {
        while(($file = readdir($handle)) !== false) {
            if($file != '.' && $file != '..' && $file != 'upload'){ 
                $count++;
            }
        }
        closedir($handle);
    }
}
$issueNo = $count + 1;
$destination = $_SERVER['DOCUMENT_ROOT']."/newsletter/issue_".$issueNo;
$source = $_SERVER['DOCUMENT_ROOT']."/newsletter/issue_1";
 
function full_copy( $source, $target ) {
    if ( is_dir( $source ) ) {
        @mkdir( $target );
        chmod($target, 0777);
        $d = dir( $source );
        while ( FALSE !== ( $entry = $d->read() ) ) {
            if ( $entry == '.' || $entry == '..' ) {
                continue;
            }
            $Entry = $source . '/' . $entry; 
            if ( is_dir( $Entry ) ) {
                full_copy( $Entry, $target . '/' . $entry );
                continue;
            }
            copy( $Entry, $target . '/' . $entry );
        }
 
        $d->close();
    }else {
        copy( $source, $target );
    }
}
 
full_copy($source, $destination);
 
?>

Your help would be greatly appreciated,

Thanks,

Bob
http://www.bobcooper.org.uk
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Copy folder problem...

Post by Ziq »

Is it a spam?
bobcooper
Forum Newbie
Posts: 2
Joined: Wed Oct 01, 2008 11:14 am

Re: Copy folder problem...

Post by bobcooper »

Hi there,

No it's not a spam, it is a CMS where the client clicks a button to create a duplicate of a newsletter that he can then edit for his next email.

Any thoughts?

Bob
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: Copy folder problem...

Post by Ziq »

Try to set error_repoting method to ALL.

Code: Select all

<?
error_reporting(E_ALL);
?>
copy() function must generate a "warning" error if file doesn't have a necessary permission.

P.S. For CMS better use a database.
Post Reply