5

I'm having difficulty in copying an image from one folder to another, now i have seen many articles and questions regarding this, none of them makes sense or work, i have also used copy function but its giving me an error. " failed to open stream: No such file or directory" i think the copy function is only for files. The image i wanna copy is present in the root directory. Can anybody help me please. What i am doing wrong here or is there any other way???

<?php
$pic="somepic.jpg";
copy($pic,'test/Uploads');
?>
Coder
  • 199
  • 2
  • 4
  • 11
  • 1
    how about adding absolute paths? like `copy($_SERVER['DOCUMENT_ROOT']."/".$pic , $_SERVER['DOCUMENT_ROOT'].'/test/Uploads');` ? – roullie Jan 25 '16 at 07:46
  • 1
    You should set destination filename. Just `test/Uploads` is not enough. – u_mulder Jan 25 '16 at 07:48
  • @roullie Now its saying:The second argument to copy() function cannot be a directory – Coder Jan 25 '16 at 07:52
  • File name for what? i want to copy it in a folder. is that not possible? – Coder Jan 25 '16 at 07:53
  • take a look at http://stackoverflow.com/questions/2050859/copy-entire-contents-of-a-directory-to-another-using-php – Professor Abronsius Jan 25 '16 at 07:56
  • 1
    What is impossible is to guess the right filename to use. The destination path needs to include the filename. Only the target directory is not enough. – Charlotte Dunois Jan 25 '16 at 08:08
  • @CharlotteDunois, i have specified the file name but now the images are copied to the root directory, why is that? – Coder Jan 25 '16 at 08:31

3 Answers3

8

You should write your code same as below :

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

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

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

You should have file name in destination like:

copy($pic,'test/Uploads/'.$pic);
Rahul
  • 334
  • 1
  • 8
  • now the images are copied but they are copying to the root folder? why is that? – Coder Jan 25 '16 at 08:31
  • can you share your directory structure? – Rahul Jan 25 '16 at 08:58
  • yeah sure.. C:\xampp\htdocs here are the images i wanna copy, and also my php file. C:\xampp\htdocs\test\uploads here i want th epics to be – Coder Jan 25 '16 at 09:28
  • you are saying C:\xampp\htdocs here are your images which is not possible. Then what is the root directory or application name. According to me C:\xampp\htdocs/test here should be the images. Your application name is test. right? – Rahul Jan 25 '16 at 09:30
1

For your code, it must be like this:

  $pic="somepic.jpg";
  copy($pic,'test/Uploads/'.$pic);

Or use function, like this:

$pic="somepic.jpg";
copy_files($pic,'test/Uploads');

function copy_files($file_path, $dest_path){
    if (strpos($file_path, '/') !== false) {
        $pathinfo = pathinfo($file_path);
        $dest_path = str_replace($pathinfo['dirname'], $dest_path, $file_path);
    }else{
        $dest_path = $dest_path.'/'.$file_path;
    }
    return copy($pic, $dest_path);
}