Open a file URL and read it in PHP? - php

Given a URL that points to a file, how can I open it and read the contents in PHP?
$file = fopen("http://www.alinktomyfile.com/kasfafa", "r");
This line has an error. The docs say to use stream_get_contents() for a URL, however I get an error:
$file_contents = stream_get_contents($file);
fclose($file);
Error:
Warning: stream_get_contents() expects parameter 1 to be resource, null given in /Users/donald/Desktop/php_boilerplate.php on line 24

try file_get_contents()
$file = file_get_contents("http://www.alinktomyfile.com/kasfafa");

Related

PHP $filename - expects parameter 1 to be a resource error

I am trying to write this file to a json file on my server and have successfully done this in many other places but for some reason this one is throwing an error that I don't understand because the $filename is correct. This works for other feeds but not this one - it is roughly 3.5mb in size so not huge. Any ideas as to why it would throw this error when public_html/data/GeoMAC-Current-Fires.json is correct? This may seem like I duplicate but I did read every suggested question/answer to no avail.
[14-Aug-2018 16:44:21 UTC] PHP Warning: fopen(public_html/data/GeoMAC-Current-Fires.json): failed to open stream: No such file or directory in /home4/theprfk5/public_html/cronjobs/GeoMAC-Current-Fires.php on line 5
[14-Aug-2018 16:44:21 UTC] PHP Warning: fwrite() expects parameter 1 to be resource, boolean given in /home4/theprfk5/public_html/cronjobs/GeoMAC-Current-Fires.php on line 6
[14-Aug-2018 16:44:24 UTC] PHP Warning: fclose() expects parameter 1 to be resource, boolean given in ///public_html/cronjobs/GeoMAC-Current-Fires.php on line 8
Code:
<?php
$site = 'https://wildfire.cr.usgs.gov/arcgis/rest/services/geomac_dyn/MapServer/3/query?where=Active%20=%20%27Y%27&geometryprecision=4&outfields=incidentname,datecurrent,active,gisacres&f=geojson';
$homepage = file_get_contents($site);
$filename = 'public_html/data/GeoMAC-Current-Fires.json';
$handle = fopen($filename,"w");
fwrite($handle,$homepage);
echo file_get_contents($site);
fclose($handle);
?>
These two lines cause the issue:
$filename = 'public_html/data/GeoMAC-Current-Fires.json';
$handle = fopen($filename,"w");
When fopen() tries to open $filename it returns false. Therefore fwrite() is trying to write to false which causes fwrite() expects parameter 1 to be resource, boolean given in ...
You will need to make sure that your path in your $filename is correct. As – Héctor mentioned above, you will need use the real path to that file.

Php fwrite/fclose warning

I have the following code fragment which works btw:
$txt = "<?php include 'work/uploads/".$php_id.".html';?>";
$slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND);
fwrite($slot, $txt);
fclose($slot);
$theCounterFile = "../offer/count.txt";
$oc = file_put_contents($theCounterFile, file_get_contents($theCounterFile)+1);
fwrite($oc);
fclose($oc);
But the following warnings get logged when running it:
Line 81 : fwrite() expects parameter 1 to be resource, integer given
Line 82 : fclose() expects parameter 1 to be resource, integer given
Line 85 : fwrite() expects at least 2 parameters, 1 given
Line 86 : fclose() expects parameter 1 to be resource, integer given
Probably my logic is wrong here. Maybe someone can shed some light here?
You don't need fwrite() or fclose() at all when you use file_put_contents(). From the docs for file_put_contents():
This function is identical to calling fopen(), fwrite() and fclose() successively to write data to a file.
Your code should look like:
$file = fopen("../offer/work/uploads/".$php_id.".html","w");
fwrite($file,$data); // Note: you could use file_put_contents here, too...
fclose($file);
$txt = "<?php include 'work/uploads/".$php_id.".html';?>";
$slot = file_put_contents('../offer/slots.php', $txt.PHP_EOL , FILE_APPEND);
$theCounterFile = "../offer/count.txt";
$oc = file_put_contents($theCounterFile, file_get_contents($theCounterFile)+1);
As for why you get errors with your current code: fwrite() and fclose() expect the first parameter to be a resource (the type of return value you get from fopen()). But you're passing them the value returned by file_put_contents(), which is an integer. So, you get an error.
file_put_contents handles the operations open, write and close all in one go – there is not need to call fwrite and fclose after it. (Not only no need – it doesn’t even make any sense, because with file_put_contents you do not even have a file handle to begin with.)
file_put_contents returns the number of bytes written, an integer value – and that’s why you get those warnings.

paths not working correctly with fopen/fwrite

I think it is something simple that has to do with my paths, because when I just create the files in the current folder, everything works. That being said, I am pretty unfamiliar with fopen(), fwrite(), and fclose(). The errors I am getting are (I think once the first one is fixed, the rest will work, too):
Warning: fopen(test/b/shipLabels/1_8154_0.pdf) [function.fopen]:
failed to open stream: No such file or directory in
/public_html/test/b/lib/FedEx/ShipWebServiceClient/Ground/Domestic
MPS/ShipWebServiceClient.php5 on line 116
Warning: fwrite() expects parameter 1 to be resource, boolean given in
/public_html/test/b/lib/FedEx/ShipWebServiceClient/Ground/Domestic
MPS/ShipWebServiceClient.php5 on line 117
Warning: fclose() expects parameter 1 to be resource, boolean given in
/public_Html/test/b/lib/FedEx/ShipWebServiceClient/Ground/Domestic
MPS/ShipWebServiceClient.php5 on line 118
The lines that references are:
$fp = fopen(SHIP_MASTERLABEL, 'wb');
fwrite($fp, ($masterResponse->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image));
fclose($fp);
I define SHIP_MASTERLABEL using this:
define('SHIP_MASTERLABEL', 'test/b/shipLabels/'.$clientId.'_'.$invoiceId.'_0.pdf');
In this case, $clientId=1, $invoiceId=8154. For the most part, this syntax has been provided by the FedEx docs.
Thanks so much for your help!
you 'd better use the absolute path ,and use the const like DIR FILE etc...

fgetcsv no longer working

I have been using this code on a webpage for about 2 years and all of the sudden it started giving me an error. Is there any noticeable reason why?These are lines 116 through 120 that are referenced in the error
$file = fopen("http:/xxxxxx/climo/reports/".$fileName, "r");
$content = fgetcsv($file, 1000, ",");
$id = 1;
while ($content = fgetcsv($file, 1000, ",") != FALSE) {
error
.gov/climo/reports/today_raw_hail.csv) [function.fopen]: failed to open stream: no suitable wrapper could be found in /homepages/27/xxxx/htdocs/xxxxx/weather/php/mainEngine.php on line 116
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /homepages/27/xxxxx/htdocs/xxxx/weather/php/mainEngine.php on line 117
Warning: fgetcsv() expects parameter 1 to be resource, boolean given in /homepages/27/xxxx/htdocs/xxxx/weather/php/mainEngine.php on line 120
Your host probably updated the 'allow_url_fopen' directive in the php.ini
http://php.net/manual/en/filesystem.configuration.php
Apparently your hosting provider dropped support for http:// urls (as you can see in the errors).
Try reading the csv with curl instead.

Warning: fopen() expects parameter 1 to be string, array given in /home/speedycm/public_html/speedyautos/carphoto.php on line 47

I am trying to upload a jpeg image on a website i am working on but keep receiving the following error:
Warning: fopen() expects parameter 1 to be string, array given in /home/speedycm/public_html/speedyautos/carphoto.php on line 47
Warning: filesize() [function.filesize]: stat failed for Array in /home/speedycm/public_html/speedyautos/carphoto.php on line 48
Warning: fread(): supplied argument is not a valid stream resource in /home/speedycm/public_html/speedyautos/carphoto.php on line 48
Warning: fclose(): supplied argument is not a valid stream resource in /home/speedycm/public_html/speedyautos/carphoto.php on line 49
the file carphoto.php has the following code on lines 47-49
$fp = fopen($_FILES["pics"]["tmp_name"], 'rb');
$contents = fread($fp, filesize($_FILES["pics"]["tmp_name"]));
fclose($fp);
This is usually because you're using multiple file uploads in your html form and using an array to group them together. Like:
<input type="file" name="pics[]"> ...
So php groups these tmp_files into an array accordingly.
You'll probably want to reference them individually
foreach($_FILES["pics"]["tmp_name"] as $file) {
....
}

Categories