MaxG
MaxG

Reputation: 199

file_put_contents won't save file

Hi I'm trying to use PHP file_put_contents to save a raster file to my local drive. The raster file is acquired by curl request, the url link works perfectly fine if open with browser(file is downloadable), but the file_put_contents won't save anything to the folder. Below is my php pseudo code:

$ch = curl_init(working_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$save = curl_exec($ch); 
curl_close($ch);
file_put_contents($path_of_file, $save);

I have chmod to 777 and have the full write and read to the folder and the save path is correct.

Upvotes: 0

Views: 1374

Answers (2)

MaxG
MaxG

Reputation: 199

So I have figured out the problem, it was a path issue; I'm using Javascript in my index.html to ajax information to my php file; executed curl in php file, download data and return the location path back to index.html in JSON data stream. Because my data structure, the path returned back to my index.html is not the right path(I used "../../filesave". Thanks everyone for helping!

In the future, if anyone had similar issue, be sure to check the save path first and also check to see if have the write and read permission to the workspace.

Upvotes: 1

Peter
Peter

Reputation: 741

Try this:

$save = file_get_contents($working_url);
file_put_contents($path_of_file, $save);

And please check:

  • you can write the destination folder
  • you have the right content in $save variable

Maybe provide us the URL to check it.

Upvotes: 0

Related Questions