user3125609
user3125609

Reputation: 31

Is there an alternative to file_get_contents?

Is there an alternative to file_get_contents? This is the code I'm having issues with:

if ( !$img = file_get_contents($imgurl) ) { $error[] = "Couldn't find the file named $card.$format at $defaultauto"; }
else {
    if ( !file_put_contents($filename,$img) ) { $error[] = "Failed to upload $filename"; }  
    else { $success[] = "All missing cards have been uploaded"; }
}

I tried using cURL but couldn't quite figure out how to accomplish what this is accomplishing. Any help is appreciated!

Upvotes: 2

Views: 9679

Answers (1)

TURTLE
TURTLE

Reputation: 3847

There are many alternatives to file_get_contents I've posted a couple of alternatives below.

fopen

function fOpenRequest($url) {
   $file = fopen($url, 'r');
   $data = stream_get_contents($file);
   fclose($file);
   return $data;
}
$fopen = fOpenRequest('https://www.example.com');// This returns the data using fopen.

curl

function curlRequest($url) {
   $c = curl_init();
   curl_setopt($c, CURLOPT_URL, $url);
   curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
   $data = curl_exec($c);
   curl_close($c);
   return $data;
}
$curl = curlRequest('https://www.example.com');// This returns the data using curl.

You could use one of these available options with the data stored in a variable to preform what you need to.

Upvotes: 9

Related Questions