views:

113

answers:

5

So yea, im working on a windows system and while this works locally, know it will break on other peoples servers. Whats a cross platform way to do the same as this

function fetch($get,$put){
    file_put_contents($put,file_get_contents($get));
}
+3  A: 

I don't see why that would fail unless the other computer is on PHP4. What you would need to do to make that backwards compatible is add functionality to provide replacements for file_get_contents & file_put_contents:

if(version_compare(phpversion(),'5','<')) {
    function file_get_contents($file) {
        // mimick functionality here
    }    
    function file_put_contents($file,$data) {
        // mimick functionality here
    }
}
if it is a v4 vs v5 issue, http://pear.php.net/package/PHP_Compat has file_put_contents() (file_get_contents is in v4.3+)
Alister Bulman
A: 

Shawn's answer is absolute correct, the only thing is that you need to make sure your $put varialable is a valid path on either the Windows Server on the Unix server.

X-Istence
A: 

well when i read your question I understood you wanted to bring a file from a remote server to your server locally, this can be done with the FTP extension from php

http://www.php.net/manual/en/function.ftp-fget.php

if this is not what you intent I believe what shawn says is correct

else tell me in the comments and i'll help you more

PERR0_HUNTER
A: 

If the fopen wrappers are not enabled, the curl extension could be: http://php.net/curl

Erik
A: 

Here would be the solution using simple file operations:

<?php
$file = "http://www.domain.com/thisisthefileiwant.zip";
$hostfile = fopen($file, 'r');
$fh = fopen("thisisthenameofthefileiwantafterdownloading.zip", 'w');

while (!feof($hostfile)) {
    $output = fread($hostfile, 8192);
    fwrite($fh, $output);
}

fclose($hostfile);
fclose($fh);
?>

Ensure your directory has write permissions enabled. (CHMOD)

Therefore, a replacement for your fetch($get, $put) would be:

function fetch($get, $put) {    
    $hostfile = fopen($get, 'r');
    $fh = fopen($put, 'w');

    while (!feof($hostfile)) {
        $output = fread($hostfile, 8192);
        fwrite($fh, $output);
    }

    fclose($hostfile);
    fclose($fh);
}

Hope it helped! =)


Cheers, KrX

Sherman