Copying file gives 0kb file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • KeredDrahcir
    Contributor
    • Nov 2009
    • 426

    Copying file gives 0kb file

    I'm trying to copy a file from a remote location and the file copies across but has a size of 0kb. Can anyone suggest why this is happening?
    Code:
    <?php
      function copyFile($url,$dirname)
      {
        @$file = fopen ($url, "rb");
        if (!$file)
        {
          echo"<font color=red>Failed to copy $url!</font><br/>";
          return false;
        }
        else
        {
          $filename = basename($url);
          $fc = fopen($dirname."$filename", "wb");
          while (!feof ($file))
          {
            $line = fread ($file, 1028);
            fwrite($fc,$line);
          }
          fclose($fc);
          echo "<font color=blue>File $url saved to PC!</font><br/>";
          return true;
        }
      }
    ?>
    
    <?php
      $filedir=$_SERVER['DOCUMENT_ROOT']."/include/";
      copyFile("http://remote location.co.uk/include/file.php",$filedir);
    ?>
  • dlite922
    Recognized Expert Top Contributor
    • Dec 2007
    • 1586

    #2
    Have you done any debugging?

    out put $line in your while loop, see if you can at least determine that is content to be written.

    I don't see you checking the output of fwrite to see if it's not false.

    Dan

    Comment

    • KeredDrahcir
      Contributor
      • Nov 2009
      • 426

      #3
      Good point. Thanks I'll give that a try.

      Comment

      • KeredDrahcir
        Contributor
        • Nov 2009
        • 426

        #4
        Could this be anything to do with the file I'm trying to copy being a php file?

        Comment

        • KeredDrahcir
          Contributor
          • Nov 2009
          • 426

          #5
          The file might be one a remote site but it's on the same server so I've managed to access it through the server just by going up two layers from the public_html layer to the home layer.

          Code:
          require($_SERVER['DOCUMENT_ROOT'].'/../../domain_name/public_html/folder/file.php');

          Comment

          • Oralloy
            Recognized Expert Contributor
            • Jun 2010
            • 988

            #6
            I'm not a PHP guy, but it seems to me that you're reading from a "URL".

            Also, you note in your request that the file is "remote".

            If that's the case, I would think you'd need to use a HTTP client object to access the remote file. Isn't "fopen" only for the computer's local file system, like in "C"?

            Comment

            Working...