简体   繁体   中英

How to copy an image from one folder to other in php

I have a image named xyz. Besides this file named xyz has unknown extension viz jpg,jpeg, png, gif etc. I want to copy this file from one folder named advertisers/images to other folder publishers/images in my website cpanl. How to do this with php. Thanks in advance.

You can use copy function:

$srcfile = 'source_path/xyz.jpg';
$dstfile = 'destination_path/xyz.jpg';
copy($srcfile, $dstfile);

Use copy() function

copy('advertisers/images/xyz.png','publishers/
 images/xyz.png');

Change the file extension, whatever it is.

If you don't know the extension, go with the wildcard. It will give you the array of all the files matching with the wildcard.

$files = glob('advertisers/images/xyz.*');
    foreach ($files as $file) {
    copy($file,'publishers/images/'.$file);
}

You should write your code same as below:

<?php
$imagePath = "../Images/somepic.jpg";
$newPath = "../Uploads/";
$ext = '.jpg';
$newName  = $newPath."a".$ext;

$copied = copy($imagePath , $newName);

if ((!$copied)) 
{
    echo "Error : Not Copied";
}
else
{ 
    echo "Copied Successful";
}
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM