Copy file and rename php
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
someone at terrasim dot com ¶ 1 year 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 ) { hugo_2000 at gmx dot at ¶ 7 years ago
gimmicklessgpt at gmail dot com ¶ 13 years ago
tom at r dot je ¶ 14 years ago
eng-ayman at aymax dot net ¶ 13 years ago
$status;
jim dot mcgowen at cox dot net ¶ 14 years ago
kadnan at yahoo dot com ¶ 18 years ago
Sina Salek ¶ 13 years ago
is_file($source)) {
Bas Vijfwinkel ¶ 7 years ago
allasso residing at signalmesa dot com ¶ 13 years ago
Vinicio Coletti ¶ 6 years ago
jtaylor -at- ashevillenc -dot- com ¶ 15 years ago
2mobile at gmail dot com ¶ 5 years ago
nensa at zeec dot biz ¶ 13 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
|