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)"); } } ?> Link to comment https://forums.phpfreaks.com/topic/77762-help-with-remote-files/ 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... Link to comment https://forums.phpfreaks.com/topic/77762-help-with-remote-files/#findComment-393649 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". Link to comment https://forums.phpfreaks.com/topic/77762-help-with-remote-files/#findComment-394520 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... Link to comment https://forums.phpfreaks.com/topic/77762-help-with-remote-files/#findComment-394767 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. Link to comment https://forums.phpfreaks.com/topic/77762-help-with-remote-files/#findComment-394788 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. Link to comment https://forums.phpfreaks.com/topic/77762-help-with-remote-files/#findComment-394804 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? Link to comment https://forums.phpfreaks.com/topic/77762-help-with-remote-files/#findComment-394891 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.