Cameron Swyft
Cameron Swyft

Reputation: 458

Alternative to file_put_contents

So if you checked my last post, I was uploading a 400MB file that ended up causing my VPS to lose all of its memory (2000MB memory to be exact) because it is uploading a file from one server to another server via cURL AND becasue I think file_put_contents is also the reason why it's such a resource hog.

So is there any alternatives to this code to save my memory usage?

$file = base64_decode($_POST['file']);
file_put_contents($_POST['filename'], $file);

Upvotes: 0

Views: 6454

Answers (1)

Meggis
Meggis

Reputation: 146

You can use cUrl: cURL can be used to grab data, information, or even a whole webpage from a designated URL. This can be very useful for grabbing information between sites. Example code:

$url = "http://yourwebsite.com/path/imgtoread.jpg";
$filetosave = PATH_ON_SERVER . "filetosave.jpg";

$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$fileraw = curl_exec($ch);
curl_close ($ch);
if(file_exists($filetosave)){
    unlink($filetosave);
}
$fp = fopen($filetosave,'x');
fwrite($fp, $fileraw);
fclose($fp);

@var url url your website @filetosave path on your server where save file @fileraw content of file to save

Note: and ensure that in php.ini allow_url_fopen is enable

Upvotes: 2

Related Questions