Solved

How should this copy() able to skip a file

Posted on 2010-09-17
7
519 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
Comment Utility

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
Comment Utility

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
Comment Utility

@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
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 1

Author Comment

by:trrsrr
Comment Utility
I'm checking it now ...

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

Author Closing Comment

by:trrsrr
Comment Utility
@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
Comment Utility
@VanHackman:
And just to add ... your modified function rocks  ...  :)
0
 
LVL 11

Expert Comment

by:VanHackman
Comment Utility
@trrsrr:

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

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

771 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

12 Experts available now in Live!

Get 1:1 Help Now