read file from a server http url in php - php

i want to print/echo a file that's save on my server and have a specific URL and try this below code to read
$lines = file($files['file_path']);
foreach ($lines as $line_num => $line) {
echo htmlspecialchars($line) . "<br />\n";
}
but it returned the following error :
file(http://myweburl/uploads/files/1506514901_txt.txt): failed to open stream: HTTP request failed! HTTP/1.1 404 Not Found
and i also try fgets with fopen function that return same error
i also follow How to read a file line by line in php but don't get any solutions
while above function is working on localhost but not server
please help

Try to enable :
allow_url_fopen = on
in php.ini file on your server

Related

readfile return open stream: HTTP request failed! HTTP/1.1 403 Forbidden in php

I have create one function in Codeigniter controller for download file, here is code for that.
function download_example($file_name) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.$file_name);
ob_clean();
flush();
readfile(base_url().'download_example/'.$file_name);
}
But after download in file show 403 Forbidden error like this.
A PHP Error was encountered
Severity: Warning Message:
readfile(http://example.com/download_example/example.txt): failed to
open stream: HTTP request failed! HTTP/1.1 403 Forbidden
Filename: controllers/controllers.php Line Number: 371
Change to something like:
readfile(dirname(__FILE__).'/download_example/'.$file_name);
If it is local file and current PHP script's directory is same as download_example directory. Change to match your system. Also make sure that file does exists and that user where web server is running has permission to access this file.
Using base_url() will get you the http address of the file, like http://example.com/download_example/file_name, but it's a better idea to use the server sistem path like C:\path\to\site_dir\download_example_dir\file_name.
I personally use something like this:
dirname($this->input->server('SCRIPT_FILENAME')).'/download_example_dir/file_name'
This is not a problem with your script, but with the resource you are requesting. The web server is returning the "forbidden" status code.
It could be that it blocks PHP scripts to prevent scraping.
You should probably talk to the administrator of the remote server.

simplexml_load_file: failed to open stream: HTTP request failed

I am trying to display rss feeds for this link. http://vuconnect.com/controls/cms_v2/components/rss/rss.aspx?sid=1643&gid=2&calcid=664&page_id=13
But it is showing this error
Warning:
simplexml_load_file(http://vuconnect.com/controls/cms_v2/components/rss/rss.aspx?sid=1643&gid=2&calcid=664&page_id=13):
failed to open stream: HTTP request failed! in
G:\wamp\www\scripts\rss_feeds\demo-1.php on line 48
Here is the code i am using.
$url = 'http://vuconnect.com/controls/cms_v2/components/rss/rss.aspx?sid=1643&gid=2&calcid=664&page_id=13';
$xml = simplexml_load_file($url);
echo '<pre>';
print_r($xml->channel);
die;
?>
needs allow_url_fopen to be On.
Check you php.ini and change it to On, if it is Off.
I hope this solves your problem

PHP file get content error

In the following code i get error failed to open stream: HTTP request failed! HTTP/1.0 400 Bad Request
$url = "https://graph.facebook.com/$orderId";
$url_with_token = $url . "?access_token=$token";
$result= json_decode(file_get_contents($url_with_token));
Please let me know where I am going wrong
Just add two lines in your php.ini file.
extension=php_openssl.dll
allow_url_include = On
its working for me.

PHP XML Get Error

I wrote a PHP Application (that works) and I have moved it to a different directory. As a result it needs to get a file from a directory above it, but the file must remain hidden, ie:
Root
--config
---users.xml (Permissions 600)
--lib
----loginUtil.php
I have tried two different methods of getting the file:
$xml = file_get_contents("../config/users.xml");
Which returns the following Error:
[13-Jun-2012 08:54:04] PHP Warning: file_get_contents(../config/users.xml) [<a href='function.file-get-contents'>function.file-get-contents</a>]: failed to open stream: No such file or directory in public_html/test/lib/loginUtil.php on line 18
and
$xml = simplexml_load_file("../config/users.xml");
which returns
[13-Jun-2012 08:56:17] PHP Warning: simplexml_load_file() [<a href='function.simplexml-load-file'>function.simplexml-load-file</a>]: I/O warning : failed to load external entity
I have tried using the full URL, and that works, but I must then make the users.xml file public (as PHP is doing a URL Request).
What can I do to resolve this issue? (Thanks for your help)
try setting the full path and not the relative
$path = '/var/full/path_dir/file.xml';
if(!is_file($path)){
// if this is the is output then you need to check permissions
// double check the file exists also.
// check the web service can read the file
echo "FILE NOT FOUND FOR $path";
}
else{
$xml = file_get_contents($path);
echo $xml;
}

PHP fopen for URL: Alternatives on Windows

I'm using the following code in PHP5 on *nix systems to call a remote URL (handover of 2 parameter to a remote system:
$fp = fopen($url, "r");
$contents = stream_get_contents($fp);
fclose($fp);
Unfortunately it fails for https (http works correctly) on Windows/IIS with the following error message (although allow_url_fopen is definitely enabled):
fopen(https://www.example.com/?method=abc&par=xyz) [function.fopen]: failed to open stream: No error in C:\Inetpub\wwwroot\libs\externalSys.php on line 122
Hence my questions:
Has anyone else found a solution?
Are there any alternatives? Curl
is unfortunately not
installed/available.
This looks like you may not have included the php_openssl.dll in your php.ini, so https fopens will fail. Does it work with just standard http?
You can try using file_get_contents.
Usage:
$contents = file_get_contents($url);

Categories