tlawless Posted November 17, 2007 Share Posted November 17, 2007 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)"); } } ?> Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 17, 2007 Share Posted November 17, 2007 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... Quote Link to comment Share on other sites More sharing options...
tlawless Posted November 19, 2007 Author Share Posted November 19, 2007 I added the header call suggested and it helped somewhat. The file is now recognized as a jpg but there are apparently still formatting issues. When I try to open in Photoshop I get "failed to load photo". Quote Link to comment Share on other sites More sharing options...
kratsg Posted November 19, 2007 Share Posted November 19, 2007 Mess around with the image types, it most likely is the problem there... Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 19, 2007 Share Posted November 19, 2007 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. Quote Link to comment Share on other sites More sharing options...
pkSML Posted November 20, 2007 Share Posted November 20, 2007 <?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. Quote Link to comment Share on other sites More sharing options...
tlawless Posted November 20, 2007 Author Share Posted November 20, 2007 pkSML, Thanks for the great, clean, simple code. Unfortunately, I'm not using fopen wrappers due to security concerns. Is the header information sort of a black art? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.