Jump to content

Help with Remote Files


tlawless

Recommended Posts

I've written a func fsockcopy which, if not obvious, uses sockets to open and copy a remote file to a local server. I want to use it to copy image (binary) files but it seems to be messing up the data.

 

Any help here would be appreciated. Thanks in advance.

 

 

<?

 

include "./fcopy_sock.php";

 

echo '<pre>';

 

echo fsockcopy ("http://www.currentinventory.net//equipment-images/12127/12127 001.jpg","./");

//echo fsockcopy ("http://www.unionwide.com/Trucks/Dump_Truck_Financing.html","./");

 

 

echo '</pre>';

?>

 

File: fcopy_sock.php

<?php

 

// -------------------------------------------------------------------

// function fsockcopy(string $source, string $destdir)

//    copies file from remote location ($source) to local directory($destdir)

//    keeps original filename

// returns TRUE on success and FALSE on failure

// -------------------------------------------------------------------

function fsockcopy ($source, $destdir) {

// Open and load RSS file

$urlParts = parse_url($source);

$host = $urlParts['host'];

$uri  = $urlParts['path'];

 

if (strcmp($urlParts['query'], '') != 0) {

$uri .= '?' . $urlParts['query'];

}

 

if(strcmp($urlParts['fragment'],'') !=0){

$fragment = $urlParts['fragment'];

$fragment = substr($fragment,4,strlen($fragment)-3);

$uri = $uri . $fragment;

}

 

if ($f = fsockopen($host, 80, $errno, $errstr)) {

$rss_content = '';

fputs($f, "GET $uri HTTP/1.0\r\nHost: $host\r\n\r\n");

 

    $filename = basename($source); // get the filename

    $fc = fopen($destdir."$filename", "wb");

   

    echo "fsockcopy: source($source)\n";

    echo "fsockcopy: destfile($destdir$filename)\n";

    echo "fsockcopy: fc($fc)...\n";

//    $mq = get_magic_quotes_runtime();

//    echo "get_magic_quotes_runtime($mq)\n";

       

while (!feof($f)) {

// $line = fgets($f, 1028);

      $line = fread($f, 1028);

 

      fwrite($fc,$line,strlen($line));

      $len = strlen($line);

      echo "strlen(line) = strlen($len)\n";

 

}

    fclose($fc);

fclose ($f);

 

return TRUE;

} else {

return False;

//die("Network error: $errstr ($errno)");

}

 

}

 

?>

 

Link to comment
https://forums.phpfreaks.com/topic/77762-help-with-remote-files/
Share on other sites

Here's a quick stab. You don't write an image file into a plain-text file (such as .html or .php). You need to change the type of files by editing it's headers, I remember doing this before... Uhh, but I forgot o_o So if anyone wants to continue my rambling for me.. xD

 

header('Content-type: image/jpeg');

 

Something along those lines? Maybe I'm thinking of something else with UTF-8 encoding, as if when you open a file through sockets, if you don't define the encoding type (in this case an image), it would default to plain text...

If you're using PHP5 and fopen wrappers enabled, try this:

 

<?php
$url = "http://calvarybucyrus.org/CBC-50.jpg";
$filename = "./savedfile.jpg";
$a = file_put_contents($filename, file_get_contents($url));
if ($a > 0) {echo "{$a} bytes written to {$filename} from {$url}";}
?>

 

You could turn it into a function easily. It simply fetches a remote image and saves it locally.

<?php
echo webcopy("http://calvarybucyrus.org/CBC-50.jpg", "./");

function webcopy($source, $destdir) {
$filename = $destdir . basename($source);
$a = file_put_contents($filename, file_get_contents($source));
if ($a > 0) {return "{$a} bytes written to {$filename} from {$source}";}
else {return "We had a problem with this query.";}
}
?>

 

This will preserve the original filename and is more similar to your code.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.