Copy image from one server to another php

I'm trying to copy an image from a remote server with the following code:

$src = "//www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/";
file_put_contents[$dest, file_get_contents[$src]];

Unfortunately I keep getting the following error:

Warning: file_put_contents[/server/location/upload/] [function.file-put-contents]: failed to open stream: Is a directory in /server/location/myscript.php on line 220

Do you have any ideas how to get around this?

asked Feb 5, 2012 at 20:12

$src = "//www.imagelocation.com/image.jpg";
$dest = "/server/location/upload/" . basename[$src];
file_put_contents[$dest, file_get_contents[$src]];

You need to specify the filename. I added basename[$src] which will write to the same filename that the original was. Be careful if you're copying from other directories, basename[] only returns the filename so if you copy /image.jpg and /a/image.jpg you'll write over the original.

answered Feb 5, 2012 at 20:15

nachitonachito

6,9052 gold badges24 silver badges44 bronze badges

2

This is because $dest is a directory, not a file. You could either manually specify the filename or use basename[]

answered Feb 5, 2012 at 20:13

axiomeraxiomer

2,0901 gold badge17 silver badges26 bronze badges

1

To copy an image from one folder to another folder you have to read the image file that you want to copy. Then create another file with the same name as the original file in the directory to where you want to copy.

Copy[] function:

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.
Read more at kodlogs.

Code:


Enter fullscreen mode Exit fullscreen mode

Before 20 days I had one task, in which I need to copy / transfer image files from one server to another dynamically.

To fulfill my requirement I used php “libcurl” library, which created by Daniel Stenberg, and allows you to connect and communicate to many different types of servers with many different types of protocols.

Follow the under given steps to copy / transfer files from one server to another using curl :

Step : 1 : Copy and use the under given code in your php script

$ch = curl_init ["Source file’s complete url”];
curl_setopt[$ch, CURLOPT_HEADER, 0];
curl_setopt[$ch, CURLOPT_RETURNTRANSFER, 1];
curl_setopt[$ch, CURLOPT_BINARYTRANSFER,1];
$rawdata = curl_exec[$ch];
// Check if any error occured 
if[curl_errno[$ch]] 
{ 
  $fp = fopen[“Destination file’s complete url”, 'w'];
  fwrite[$fp, $rawdata];
  fclose[$fp];
}
curl_close [$ch];
ob_flush[];
flush[];

Step : 2 : Replace

"Source file’s complete url”

properly.

Step : 3 : And replace

“Destination file’s complete url”

according to your requirement.

That’s it.

So now you are able to copy / transfer files from one server to another.

I hope this will help you….!!

How do I copy an image from one server to another?

Let's check out all such methods in this article..
Method 1. Saving and Loading the Image from TAR files. ... .
Method 2. Copy Docker Images Via SSH. ... .
Method 3. Copying Docker Images using Docker Machines. ... .
Method 4. Copying Images Using DOCKER_HOST variable. ... .
Method 5. Use Docker-push-ssh to copy images. ... .
Method 6..

How do I copy an image from one directory to another 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.

Chủ Đề