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
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...

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

<?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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.