Solved

How should this copy() able to skip a file

Posted on 2010-09-17
7
526 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…

770 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