Php copy file and rename
This should be pretty easy. I wan't to copy & rename images that already exist on the server while still retaining the original image. Here's the original image location:
This is what I want:
How can I achieve this? You can see I'm not just simply renaming an existing file / image. I want to copy it and rename it to the same directory.
asked Jul 11, 2012 at 22:58
4 Just use the copy method: http://php.net/manual/en/function.copy.php Ex:
answered Jul 11, 2012 at 23:02
SybioSybio 8,4653 gold badges43 silver badges51 bronze badges 0 PHP has a function, copy built-in that can do this. Here's an example:
The function returns a boolean indicating whether the copy was successful. It's as simple as that! answered Jul 11, 2012 at 23:03
AndrewAndrew 2,04420 silver badges32 bronze badges Like rename(), copy() also takes two parameters - the filename you wish to copy from, and the filename you wish to copy to. The difference between rename() and copy() is that calling rename() results in the file being in only one place, the destination, whereas copy() leaves the file in the source location as well as placing a new copy of the file into the destination.
The result of that script is that there will be a file $filename and also a $filename.old, e.g. c:\\windows\\myfile.txt and c:\\windows\\myfile.txt.old. Author's Note: this function will not copy empty (zero-length) files - to do that, you need to use the function touch(). Want to learn PHP 7? Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today! If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it! Next chapter: Deleting files with unlink() >> Previous chapter: Moving files with rename() Jump to: Home: Table of Contents Copyright ©2015 Paul Hudson. Follow me: @twostraws. (PHP 4, PHP 5, PHP 7, PHP 8) copy — Copies file Descriptioncopy(string If you wish to move a file, use the rename() function. Parametersfrom Path to the source file. to The destination path. If Warning If the destination file already exists, it will be overwritten. context A valid context resource created with stream_context_create(). Return Values Returns ExamplesExample #1 copy() example
copy($file, $newfile)) { See Also
simonr_at_orangutan_dot_co_dot_uk ¶ 18 years ago
cooper at asu dot ntu-kpi dot kiev dot ua ¶ 16 years ago
steve a h ¶ 14 years ago
promaty at gmail dot com ¶ 11 years ago
absorbentshoulderman at gmail dot com ¶ 9 years ago
='C:\File\Whatever\Path\Joe.txt'; ASchmidt at Anamera dot net ¶ 8 years ago
$http_status ) { tom at r dot je ¶ 14 years ago
gimmicklessgpt at gmail dot com ¶ 13 years ago
eng-ayman at aymax dot net ¶ 13 years ago
$status;
jim dot mcgowen at cox dot net ¶ 14 years ago
hugo_2000 at gmx dot at ¶ 7 years ago
2mobile at gmail dot com ¶ 5 years ago
Sina Salek ¶ 13 years ago
is_file($source)) {
kadnan at yahoo dot com ¶ 18 years ago
allasso residing at signalmesa dot com ¶ 13 years ago
Vinicio Coletti ¶ 6 years ago
someone at terrasim dot com ¶ 11 months ago
jtaylor -at- ashevillenc -dot- com ¶ 15 years ago
nensa at zeec dot biz ¶ 13 years ago
Bas Vijfwinkel ¶ 7 years ago
cory at veck dot ca ¶ 11 years ago
('DS', DIRECTORY_SEPARATOR); // I always use this short form in my code.function copy_r( $path, $dest ) mishawagon at gmail dot com ¶ 3 years ago
mspreij ¶ 15 years ago
How do you rename the file if already exists in PHP?If the new name specified by the user already exists, the rename() function overwrites it. The old name of the file and the new name specified by the user are sent as parameters to the rename() function and it returns True on success and a False on failure.
How do you rename copy and delete a file in PHP?PHP copy(), rename() and unlink() file handling. copy() – used to copy a file.. rename() – used to rename a file.. unlink() – used to delete a file.. How do I copy and paste a file in PHP?The copy() function in PHP is used to copy a file from source to target or destination directory. It makes a copy of the source file to the destination file and if the destination file already exists, it gets overwritten. The copy() function returns true on success and false on failure.
How do I move a file from one directory to another in PHP?If you need to copy file from one folder to another using php code then you can use “copy()” function of php. php provide copy function to move your file from one place to another.
|