Solved

How should this copy() able to skip a file

Posted on 2010-09-17
7
522 Views
Last Modified: 2012-05-10
Hi experts,

I got this function smartCopy from php.net which works like charm but I really need to add a feature there that I dont know how to and I really need help with this.

So, here is how I applied to my script ... It is working great, copying all files in 'temp' folder to the folder beneath
smartCopy('./temp/', './', $options=array('folderPermission'=>0755,'filePermission'=>0755));

I wonder if you would modify the function for me so I can skip a file (or perhaps more) to not to be copied .. for example 'config.php' ... so perhaps the function can be modified and used like this:
smartCopy('./temp/', './', $options=array('folderPermission'=>0755,'filePermission'=>0755), $skipFile=array('config.php'));
So while copying all files it will skip 'config.php' ...

Thanks...

And below is the function codes :
function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755))

{ 

        $result=false;



        if (is_file($source)) {

            if ($dest[strlen($dest)-1]=='/') {

                if (!file_exists($dest)) {

                    cmfcDirectory::makeAll($dest,$options['folderPermission'],true);

                }

                $__dest=$dest."/".basename($source);

            } else {

                $__dest=$dest;

            }

            $result=copy($source, $__dest);

            chmod($__dest,$options['filePermission']);

           

        } elseif (is_dir($source)) {

            if ($dest[strlen($dest)-1]=='/') {

                if ($source[strlen($source)-1]=='/') {

                    //Copy only contents

                } else {

                    //Change parent itself and its contents

                    $dest=$dest.basename($source);

                    @mkdir($dest);

                    chmod($dest,$options['filePermission']);

                }

            } else {

                if ($source[strlen($source)-1]=='/') {

                    //Copy parent directory with new name and all its content

                    @mkdir($dest,$options['folderPermission']);

                    chmod($dest,$options['filePermission']);

                } else {

                    //Copy parent directory with new name and all its content

                    @mkdir($dest,$options['folderPermission']);

                    chmod($dest,$options['filePermission']);

                }

            }



            $dirHandle=opendir($source);

            while($file=readdir($dirHandle))

            {

                if($file!="." && $file!="..")

                {

                     if(!is_dir($source."/".$file)) {

                        $__dest=$dest."/".$file;

                    } else {

                        $__dest=$dest."/".$file;

                    }

                    //echo "$source/$file ||| $__dest<br />";

                    $result=smartCopy($source."/".$file, $__dest, $options);

                }

            }

            closedir($dirHandle);

        } else {

            $result=false;

        }

        return $result;



}

Open in new window

0
Comment
Question by:trrsrr
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:Chad Smith
ID: 33706635

function smartCopy($source, $dest, $options=array('folderPermission'=>0755,'filePermission'=>0755), $skipFile=array())
{ 
        $result=false;

        if (is_file($source) && !in_array($source,$skipFile)) {
            if ($dest[strlen($dest)-1]=='/') {
                if (!file_exists($dest)) {
                    cmfcDirectory::makeAll($dest,$options['folderPermission'],true);
                }
                $__dest=$dest."/".basename($source);
            } else {
                $__dest=$dest;
            }
            $result=copy($source, $__dest);
            chmod($__dest,$options['filePermission']);
           
        } elseif (is_dir($source)) {
            if ($dest[strlen($dest)-1]=='/') {
                if ($source[strlen($source)-1]=='/') {
                    //Copy only contents
                } else {
                    //Change parent itself and its contents
                    $dest=$dest.basename($source);
                    @mkdir($dest);
                    chmod($dest,$options['filePermission']);
                }
            } else {
                if ($source[strlen($source)-1]=='/') {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                } else {
                    //Copy parent directory with new name and all its content
                    @mkdir($dest,$options['folderPermission']);
                    chmod($dest,$options['filePermission']);
                }
            }

            $dirHandle=opendir($source);
            while($file=readdir($dirHandle))
            {
                if($file!="." && $file!="..")
                {
                     if(!is_dir($source."/".$file)) {
                        $__dest=$dest."/".$file;
                    } else {
                        $__dest=$dest."/".$file;
                    }
                    //echo "$source/$file ||| $__dest<br />";
                    $result=smartCopy($source."/".$file, $__dest, $options, $skipFile);
                }
            }
            closedir($dirHandle);
        } else {
            $result=false;
        }
        return $result;

}

Open in new window

0
 
LVL 11

Accepted Solution

by:
VanHackman earned 500 total points
ID: 33706636

here is it...

This line does all the magic:

if (is_file($source) && (!in_array(pathinfo($source, PATHINFO_BASENAME), $options['Skip_File'])) )

I attached the full functional example, just execute the CopyDemo.php and see it do the Work, copying the content from Folder1 to Folder2, and skip the files specified.

and the function must be used like this:


$source	 = 'Folder1';
$dest	 = 'Folder2';
$options = array('folderPermission'=>0755,'filePermission'=>0755,'Skip_File' => array('DontCopyMe.txt', 'DontCopyMe1.txt', 'DontCopyMe2.txt', 'DontCopyMe3.txt'));

smartCopy($source, $dest, $options)

Open in new window

smartcopy.zip
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 33706642

@knight00:

Your Solution is almost Ok, but it have a error in this part:

if (is_file($source) && !in_array($source,$skipFile))


You should use the pathinfo() function, in order to get just the file name from $source and compare it to the list of files to skip, in other way your script will only work when you are copying files in the same folder where the script is being executed or when full paths are specified in $skipFile.

So, take care, that is a mistake.
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 1

Author Comment

by:trrsrr
ID: 33706722
I'm checking it now ...

Thanks and I'll be back soon with results ...
0
 
LVL 1

Author Closing Comment

by:trrsrr
ID: 33706748
@VanHackman:
You're right ... actually I tried solution from @knight00 too but it just copy everything just like the original function.
So, thanks a lot for the help... really appreciated.

Regards...
0
 
LVL 1

Author Comment

by:trrsrr
ID: 33706753
@VanHackman:
And just to add ... your modified function rocks  ...  :)
0
 
LVL 11

Expert Comment

by:VanHackman
ID: 33706847
@trrsrr:

Thank you!, I'm glad to hear it help you.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
nagios 4 php error after installation 6 79
WordPress TK Title 8 25
What is the best PDF generator to use? 1 30
How to count in a table in php 22 18
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now