How to create duplicate file in php?

Hi guys wanted to also add on how to copy using a dynamic copying and pasting.

let say we don't know the actual folder the user will create but we know in that folder we need files to be copied to, to activate some function like delete, update, views etc.

you can use something like this... I used this code in one of the complex project which I am currently busy on. i just build it myself because all answers i got on the internet was giving me an error.

    $dirPath2 = "users/$uniqueID"; #creating main folder and where $uniqueID will be called by a database when a user login.
    $result = mkdir[$dirPath2, 0755];
            $dirPath2 = "users/$uniqueID/profile"; #sub folder
            $result = mkdir[$dirPath2, 0755];
                $dirPath3 = "users/$uniqueID/images"; #sub folder 
                $result = mkdir[$dirPath3, 0755];
                    $dirPath4 = "users/$uniqueID/uploads";#sub folder
                    $result = mkdir[$dirPath4, 0755];
                    @copy['blank/dashboard.php', 'users/'.$uniqueID.'/dashboard.php'];#from blank folder to dynamic user created folder
                    @copy['blank/views.php', 'users/'.$uniqueID.'/views.php']; #from blank folder to dynamic user created folder
                    @copy['blank/upload.php', 'users/'.$uniqueID.'/upload.php']; #from blank folder to dynamic user created folder
                    @copy['blank/delete.php', 'users/'.$uniqueID.'/delete.php']; #from blank folder to dynamic user created folder

I think facebook or twitter uses something like this to build every new user dashboard dynamic....

[PHP 4, PHP 5, PHP 7, PHP 8]

copyCopies file

Description

copy[string $from, string $to, ?resource $context = null]: bool

If you wish to move a file, use the rename[] function.

Parameters

from

Path to the source file.

to

The destination path. If to is a URL, the copy operation may fail if the wrapper does not support overwriting of existing files.

Warning

If the destination file already exists, it will be overwritten.

context

A valid context resource created with stream_context_create[].

Return Values

Returns true on success or false on failure.

Examples

Example #1 copy[] example

See Also

  • move_uploaded_file[] - Moves an uploaded file to a new location
  • rename[] - Renames a file or directory
  • The section of the manual about handling file uploads

simonr_at_orangutan_dot_co_dot_uk

18 years ago

Having spent hours tacking down a copy[] error: Permission denied , [and duly worrying about chmod on winXP] , its worth pointing out that the 'destination' needs to contain the actual file name ! --- NOT just the path to the folder you wish to copy into.......
DOH !
hope this saves somebody hours of fruitless debugging

cooper at asu dot ntu-kpi dot kiev dot ua

16 years ago

It take me a long time to find out what the problem is when i've got an error on copy[]. It DOESN'T create any directories. It only copies to existing path. So create directories before. Hope i'll help,

steve a h

14 years ago

Don't forget; you can use copy on remote files, rather than doing messy fopen stuff.  e.g.

promaty at gmail dot com

11 years ago

Here is a simple script that I use for removing and copying non-empty directories. Very useful when you are not sure what is the type of a file.

I am using these for managing folders and zip archives for my website plugins.



Cheers!

absorbentshoulderman at gmail dot com

9 years ago

A nice simple trick if you need to make sure the folder exists first:



That simple.

ASchmidt at Anamera dot net

8 years ago

Below a code snippet for downloading a file from a web server to a local file.

It demonstrates useful customizations of the request [such as setting a User-Agent and Referrer, often required by web sites], and how to download only files if the copy on the web site is newer than the local copy.

It further demonstrates the processing of response headers [if set by server] to determine the timestamp and file name. The file type is checked because some servers return a 200 OK return code with a textual "not found" page, instead of a proper 404 return code.

kadnan at yahoo dot com

18 years ago

you can also try xcopy command by using Shell to move/copy files/folders from one place to another
here is the code:



by executing this command, it will move folder along with all contents to destination.

-adnan

allasso residing at signalmesa dot com

13 years ago

As far as recursive copy, something like this seems to work fine for me:



Of course you need to get all your permissions clear.  You can do the necessary stuff to use variables.

You could also do this to create the destination directory:



This will create a new directory called "dir_dest" if it does not already exist. This is a bit risky though if your situation is ambiguous, and you want to continue to make backups etc, 'cause if you do it twice you end up with:

dir_destination/dir_source

to avoid that one could do something like:



Maybe someone can tell me when or why it would be better to use all that PHP code I see here.

Vinicio Coletti

6 years ago

Copying large files under Windows 8.1, from one NTFS filesystem to another NTFS filesystem, results in only the first 4 GiB copied and the rest of the file is ignored.

So, if you think to have files larger than 4 GiB, instead of doing:
   copy[$source,$destination];
it is much better to do something like:
   exec["xcopy $source $destination"];

I will check to see if this issue is valid also under Linux.
It depends on PHP not being compiled in 64 bit mode?

someone at terrasim dot com

11 months ago

On Windows, php-7.4.19-Win32-vc15-x64 - copy[] corrupted a 6GB zip file. Our only recourse was to write:

function file_win_copy[ $src, $dst ] {
shell_exec[ 'COPY "'.$src.'" "'.$dst.'"'];
return file_exists[$dest];
}

jtaylor -at- ashevillenc -dot- com

15 years ago

It seems as though you can only use move_uploaded_file[] once on a temporary file that has been uploaded through a form. Most likely the action this function takes destroys the temporary file after it has been moved, assuming permanent placement on a file system.

Attempting to use the function again in the same PHP script will return false and not move the file.

I ran into this when an image that was uploaded did not need resizing [smaller than a size threshold] and after moving the temporary upload to the "originals" directory, an attempt was made to again move the temporary file to another folder.

This behavior is understandable, but be careful - in this instance, I simply used copy[] on the file that was already uploaded.

nensa at zeec dot biz

13 years ago

When I recently had to copy a few millions of small files [< 1kb] from one NAS to another and for some reasons had to do that file by file I compared the following function with copy[$src, $dest] and shell_exec["cp -r $src $dest"].

Surprisingly stream_copy seamed to be slightly faster [at least in this specific context].

Bas Vijfwinkel

7 years ago

Paths and filenames with Japanese characters are not handled correctly if you are running Apache/PHP on a Windows machine.
With the following code you can convert e.g. the network path to the appropriate encoding so that Windows understands where to look:



There are however a number of characters that cannot be converted correctly like '②' and '﨑' because there are no SJIS equivalents.

cory at veck dot ca

11 years ago

My own 'cp -R' written in PHP.

Hopefully it will work for your situation. I'm using it in a web based file manager for my CMS.

mishawagon at gmail dot com

3 years ago

Copy failed for me until I added dirname[__FILE__] to the file paths.
copy[dirname[__FILE__].$tempdata,dirname[__FILE__].$filepath]

mspreij

15 years ago

This function creates a new filename to use for a copy of the given filename, its behaviour was mostly sto^Wborrowed from how the OS X Finder [*1] does it.
Note it *doesn't* actually copy the file, it just returns the new name. I needed it to work regardless of data source [filesystem, ftp, etc].

It also tries to match the current name as neatly as possible:
foo.txt -> foo copy.txt -> foo copy 1.txt -> foo copy 2.txt [etc]
foo.bar.baz.jpg -> foo.bar.baz copy.jpg
foobar -> foobar copy -> foobar copy 1 [etc]
".txt" -> .txt copy, and "txt." -> txt. copy
file.longextension -> file.longextension copy

It keeps trying until it finds a name that is not yet taken in $list, or until it looped 500 times [change as needed].

If the renamed file becomes longer than max filename length, it starts chopping away at the end of the part before where it adds " copy": reallylong...filename.txt -> reallylong...filena copy.txt

Chủ Đề