file_get_contents() how to fix error Failed to open stream, No such file
The URL is missing the protocol information. PHP thinks it is a filesystem path and tries to access the file at the specified location. However, the location doesn't actually exist in your filesystem and an error is thrown.
You'll need to add http
or https
at the beginning of the URL you're trying to get the contents from:
$json = json_decode(file_get_contents('http://...'));
As for the following error:
Unable to find the wrapper - did you forget to enable it when you configured PHP?
Your Apache installation probably wasn't compiled with SSL support. You could manually try to install OpenSSL and use it, or use cURL. I personally prefer cURL over file_get_contents()
. Here's a function you can use:
function curl_get_contents($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
Usage:
$url = 'https://...';
$json = json_decode(curl_get_contents($url));
Warning: file_get_contents failed to open stream: No such file or directory?
Try string like this it may help
Just use . for concatenating.
$localFile='/var/www/uploads/$file_name';
to
$localFile='/var/www/uploads/'.$file_name;
and
$remoteFile='/home/nsadmin/$file_name';
to
$remoteFile='/home/nsadmin/'.$file_name;
how to fix 'file_get_content' failed to open stream
Try to make use of Laravel's base_path()
helper.
base_path()
is equal to the root of your project. So, let's, for example, you want to access estimated_dates.pdf
. The correct solution would be:
base_path('storage/app/public/estimated_dates.pdf');
However, you could make use of:
php artisan storage:link
and you can access the file by: asset('estimated_dates.pdf')
I guess your helloworld.pdf
is in root of the application, so:
base_path('helloworld.pdf')
base_path(): https://laravel.com/docs/5.8/helpers#method-base-path
storage:link: https://laravel.com/docs/5.8/filesystem#configuration
Message: file_get_contents(sso.json): failed to open stream: No such file or directory
If you change to
$url = __DIR__.'/sso.json';
This should work.
You can learn more about magic constants from HERE
file_get_contents gives me failed to open stream
I don't know Why the problem happened but here is the code that fixed it, thanks to "NikiC" who found out that witespace " " was the problem, I fixed it.
This Code doesn't work
$image = explode("\n", $_POST['images']);
for ($i = 0; $i < count($image); $i++){
if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){
file_put_contents('images/'.$title_url.'_'.$i.'.jpg', file_get_contents($image[$i]));
$image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg';
}
}
$poster = $image[0];
$images = implode("\n", $image);
This is working code
$image = explode("\n", $_POST['images']);
for ($i = 0; $i < count($image); $i++){
if (substr($image[$i], 0, strlen($this->host) - 1) != $this->host){
if ($i < count($image)-1){
$kk = substr($image[$i], 0, strlen($image[$i]) - 1);
} else {
$kk = $image[$i];
}
if ($this->download($kk, 'images/'.$title_url.'_'.$i.'.jpg')){
$image[$i] = $this->host.'/images/'.$title_url.'_'.$i.'.jpg';
}else{
echo($image[$i]);
}
}
}
//****************************************************
public function download($url, $path){
if (file_put_contents($path, file_get_contents($url))){return TRUE;}else{return FALSE;}
}
file_get_contents() failed to open stream: No such file or directory
remove extra .php from url
$filename = "http://www.hf-live.com/codeupdate/Get_Files_Name.php";
You will get error if you are doing this way...
$filename = "marynyhof.com/Info.php"; // will give you error, which exactly you are getting
use full url like this
$filename = "http://www.marynyhof.com/Info.php"; //will work
file_get_contents(): failed to open stream: No such file or directory
Use $tmp_name
when calling file_get_contents()
. This is the actual location of the file on disk until the end of the request. $name
contains the original name of the uploaded file in case you need it.
Related Topics
Getting List Ips from Cidr Notation in PHP
How Would I Stop This Foreach Loop After 3 Iterations
Soapfault Exception: Could Not Connect to Host
Difference Between File, File_Get_Contents, and Fopen in PHP
In PHP How Does Usort() Function Works
Copy File from Remote Server or Url
PHP Session Ids -- How Are They Generated
Best Way to Internationalize Simple PHP Website
How to Convert All Characters to Their HTML Entity Equivalent Using PHP
How to Find Array/Dictionary Value Using Key
How to Include File Outside Document Root