Solved

How should this copy() able to skip a file

Posted on 2010-09-17
7
536 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Link failure 16 70
PHP SMTP authentication 6 28
How do I fix this error with quotation marks? 4 26
php output utf-8 problem 6 24
Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
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…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

726 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