28

I have the following code:

$file = 'http://3.bp.blogspot.com/-AGI4aY2SFaE/Tg8yoG3ijTI/AAAAAAAAA5k/nJB-mDhc8Ds/s400/rizal001.jpg';
$newfile = '/img/submitted/yoyo.jpg';

if ( copy($file, $newfile) ) {
    echo "Copy success!";
}else{
echo "Copy failed.";
}

and it always output "Copy failed"

copy(/img/submitted/yoyo.jpg) [function.copy]: failed to open stream: No such file or directory

my directory is set to 777.

any ideas? thanks!

Alex Tartan
  • 6,736
  • 10
  • 34
  • 45
Kris
  • 3,709
  • 15
  • 50
  • 66

3 Answers3

74

While copy() will accept a URL as the source argument, it may be having issues a url for the destination.

Have you tried specifying the full filesystem path to the output file? I'm assuming you're not trying to put the new file onto a remote server.

For example:

$file = 'http://3.bp.blogspot.com/-AGI4aY2SFaE/Tg8yoG3ijTI/AAAAAAAAA5k/nJB-mDhc8Ds/s400/rizal001.jpg';
$newfile = $_SERVER['DOCUMENT_ROOT'] . '/img/submitted/yoyo.jpg';

if ( copy($file, $newfile) ) {
    echo "Copy success!";
}else{
    echo "Copy failed.";
}

The above worked nicely for me.

Mark Biek
  • 146,731
  • 54
  • 156
  • 201
8

I found this function in one of my old project.

private function download_file ($url, $path) {

  $newfilename = $path;
  $file = fopen ($url, "rb");
  if ($file) {
    $newfile = fopen ($newfilename, "wb");

    if ($newfile)
    while(!feof($file)) {
      fwrite($newfile, fread($file, 1024 * 8 ), 1024 * 8 );
    }
  }

  if ($file) {
    fclose($file);
  }
  if ($newfile) {
    fclose($newfile);
  }
 }
Athiwat Chunlakhan
  • 7,589
  • 14
  • 44
  • 72
1

If the file is not publically accessible then you cannot copy a file from a server without having access to it.

You can use ftp_get() to open up a FTP connection and copy file.

$local_file = 'localname.zip'; // the nam
$server_file = 'servername.zip';
$conn = ftp_connect($ftp_server);

$login_result = ftp_login($conn, $ftp_user_name, $ftp_user_pass);

if (ftp_get($conn, $local_file, $server_file, FTP_BINARY)) {
    echo "Successfully copied";
}
ftp_close($conn);

But, If you want to download a file from URL

$fullPath = "filepath.pdf";

if ($fd = fopen ($fullPath, "r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);
    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename=\"".$path_parts["basename"]."\"");
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    while(!feof($fd)) {
        $buffer = fread($fd, 2048);
        echo $buffer;
    }
}
fclose ($fd);
Starx
  • 77,474
  • 47
  • 185
  • 261
  • What does an `ftp` connection have to do with copying a file from an `http` server? In addition and fortunately less and less providers still use the completely outdated `ftp` protocol. So this is not an answer, sorry. – arkascha Nov 21 '16 at 13:10
  • 1
    @arkascha, I have reworded my answer if that clears up things. And I deny your comment of declaring my answer as "not an answer". Because FTP is still widely used in web development world and even if it was outdated, it would still be an answer which suggests an outdated implementation but still would be an answer. – Starx Nov 22 '16 at 06:31
  • The file not being publically accessible was neither the question, nor the issue. You answered a question that was not asked. It is true that unfortunately some service providers still use the old protocol although better alternatives exist, thus force their users to present private credentials and data to everyone listening. But still the question was how to use php's copy function to access a remote file. Which you did not answer at all. – arkascha Nov 22 '16 at 07:02
  • @arkascha The title of the question is `Copy file from remote server or URL` and my answer does answer that both. – Starx Nov 22 '16 at 07:09
  • Indeed. But the title is not the question and it is a wild guess that the ftp protocol can he used here at all. And even if, or if the OP wants to send the file to the client (where did you get that from?), still the content of the question suggests that the OP asks something else. Anyway, we may have different views here. Thats all I commented. – arkascha Nov 22 '16 at 07:15
  • @arkascha, Where in my answer it says `to send the file to the client` (What do you even mean by client?). Questions is asking how to copy and it shows what he is trying to do. Answer from Mark (the accepted answer) points out a solution in what he is trying. My answer gives an alternative solution. That's how answers work in SO, people give multiple solutions and members vote if they agree with the solution and OP picks an answer suitable for him. And you are not stating your opinion/view you are declaring my answer is not an answer and I object to that heavily. – Starx Nov 22 '16 at 09:14
  • Your second code section titled with "If you want to download a file from URL" actually does _exactly_ that: sending the file to the client, an http client, a browser. You send http response headers and the file content to some requesting client, which has nothing to do with what was asked ;-) – arkascha Nov 22 '16 at 09:18
  • @arkascha, Thanks for the clarification and your *client* comment makes sense now. And to further defend, I will still say I provide an alternative solution. And based on the votes I know my solution is not an agreeable solution, hence its the least voted answer of this question. – Starx Nov 22 '16 at 09:23