?
Solved

How should this copy() able to skip a file

Posted on 2010-09-17
7
Medium Priority
?
542 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

 
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

Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

752 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