file_put_contents from fopen creates different file than a browser - php

This is driving me crazy. I can download a file from internet through browser and its working fine. However if I write a script that does this for me using PHP, the file ends up being completely different (as per online diff tools).
$link = "https://torcache.net/torrent/9AE1726935FF9C08DF422CCE3C4445FC9484478B.torrent?title=[kat.cr]the.big.bang.theory.s08e24.720p.hdtv.x264.dimension.rartv";
file_put_contents($file_name, fopen( $link, 'r'));
First I tried to play around with encoding, but that should not matter as the file is binary, right? Also tried file_get_contents instead of fopen first, the same problem.
My PHP app is running on UTF-8 w/o BOM files. Can someone help? What am I doing wrong?

I think I got what you mean. The responses from torcache.net are all compressed with gzip. If you download the file from your browser, the file is automatically decoded, but if you do the same with php you get the same file but still encoded. You can use gzdecode to decodes it.
file_put_contents($file_name, gzdecode(file_get_contents($link)));

You have some errors in your code.
First you try to add a string to a variable without parentheses.
$link = "https://torcache.net/torrent/9AE1726935FF9C08DF422CCE3C4445FC9484478B.torrent?title=[kat.cr]the.big.bang.theory.s08e24.720p.hdtv.x264.dimension.rartv"
The next one you try to download that file over https. Check if your openssl is enabled and if allow_url_fopen is set correctly in your php.ini
https://secure.php.net/manual/en/features.remote-files.php

There are 3 ways to solve your problem:
enable openssl for https calls
Use curl and set the ssl verifier to 0
Replace the https with http to make a normal call.
Option 3 is the easiest one.

Related

Including a remote php file as a resource

I am trying to include remote php files as a resource but I am having a bit of trouble. I went into the php.ini files and set allow_url_fopen to ON. I also looked for the setting allow_url_include but it was not in the file, I added it to the php.ini file and also set that to on.
If I try to include using
include ('http://somewebsite.com/lib/somescript.php');
The server / php spits out a message saying:
URL file-access is disabled in the server configuration
I also get a message saying:
failed to open stream: no suitable wrapper could be found in blah blah blah
The seconed way I am trying to acomplish the same result is using fopen but I am just getting the content of the file, thats not what I need I need my local script to see the remote script as an executabel rescource.
$myscript = fopen("http://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
// include in the contents of my_script.php
echo $incmyscript;
I have to be doing something wrong? I know echoing out the variabel $incmyscript is wrong, but I can't think of a way to place in the code. I am not sure if fopen is the best best way to get what I want.
Any ideas?
The message you are getting:
URL file-access is disabled in the server configuration
Indicates that the allow_url_include setting in your php.ini is set to Off. Enabling that option will allow you to do remote file inclusion, but be very careful with this as it's a pretty big security risk once the other site would be compromised (A hacker could easily inject their own remote code to your site).
Instead of echo, you could use eval.
Only do this if you want to execute PHP code from the other server, not if you just want to include HTML!
Even if you really want to execute PHP code from the other server, a man-in-the-middle could execute arbitrary PHP code on your server. You should therefore better use HTTPS or avoid the inclusion of the remote file at all.
Example:
$myscript = fopen("https://someotherwebsite/lib/my_script.php", "r");
$incmyscript= fread($myscript , 9999);
fclose($myscript);
$incmyscript);
Instead of the echo you could use this:
eval($incmyscript);
But be careful, this is very bad practice!
READ THIS: http://php.net/manual/en/function.eval.php
If you can trust remote script then you can call eval:
eval ($incmyscript);
If http://somewebsite.com/lib/somescript.php served by server supporting PHP you're trying to include it's output, not the code itself! Otherwise it's a just wrong and may be considered as security hole!
What you're trying to do is opening of a major security hole!
If the remote server is configured to process .php files, you won't be able to get the source for it. The server will process the PHP and then return any output. If getting remote PHP sources were possible, hackers would be grabbing our code and looking for vulnerabilities way too easily!

Shortcut web page file in PHP

I wish I knew how to make a file that is a principal access to a web page. I'm using PHP to do this. It occurred to me the following:
function crearArchivoUrl($url){
$archivo=str_replace(array("http://", "https://", "mailto://", "ftp://"), "", $url);
$archivo=str_replace(array("/"), "-", $archivo);
$this->checkNombreDestino($archivo);
$contenido="[InternetShortcut]\r\nURL=".$url."\r\n";
$fp = fopen($archivo, 'w');
chmod($archivo, 0644);
fwrite($fp, $contenido);
fclose($fp);
}
But when I test it (by double-clicking on it) I did not jump the browser.
Anybody can tell me how to make files that are shortcuts to web pages?
Thanks for the help.
Greetings!
My code was correct, all I needed was that the file extension should be .url, ie, my-web-shortcut.url
Thanks for everything.
Greetings!
Your PHP will only be executed if you have a PHP interpreter installed, and if the webserver is configured to run your PHP file. If you distribute this file to unconfigured computers, it will never be executed as PHP code, and will therefore never run as you expect.
If you are running this script on a webserver that is properly configured, consider using header("Location: <URL>") to redirect the user to the new page.
Actually, you are reading a file, and writing it to another file. Doing that sends nothing to the web browser.
A first approach would be to send a header Location to redirect the broswer. See examples in the manual to know how to use it.
Another approach would be to read the file contents, and to print it, using echo or any other printing command.

In PHP, how can i get fopen to timeout very fast when opening a local file?

I'm having a problem with PHP where i'm writing to a file very often, and sometimes it takes a long time to open that file.
The complete description is here:
fopen file locking in PHP (reader/writer type of situation)
My question is how can I get fopen to timeout in, say, 50ms.
I looked at stream-context-create but that seems to be for HTTP, or at least, if it'll work for local files, I'm not sure how to specify the option in the array.
Any ideas?
Thank you!
Daniel
I'm not sure what you're trying here, but in some platforms (not Windows, though), you can open a file in non-blocking mode with the n flag:
$f = fopen("/tmp/foo/bar", "wn+");
This should return immediately. You can then probably use stream_select with a timeout of 50 ms.
I say "probably" because this flag is not documented.
changing the default_socket_timeout variable in the php.ini to '1', would that helps?

Apache / PHP output caching

I'm working on a PHP script which generates large (multi-MB) output on the fly without knowing the length in advance. I am writing directly to php://output via fwrite() and have tried both standard output and using Transfer-Encoding: chunked (encoding the chunks as required) but no matter what I try the browser waits until all the data is written before displaying a download dialog. I have tried flush()ing too after the headers and after each chunk but this also makes no difference.
I'm guessing that Apache is caching the output as the browser would normally display after receiving a few kB from the server.
Does anyone have any ideas on how to stop this caching and flush the data to the browser as it is generated?
Thanks,
J
First of all, like BlaM mentioned in his comment, if in the PHP configuration OutputBuffering is enabled, it wont work, so it would be useful to know your phpinfo().
Next thing, try if it works with a big file that is stored on yor webserver, output it usinf readfile. And, together with this, check if you send the correct headers. Hints on how to readfile() and send the correct headers a provided here: StackOverflow: How to force a file download in PHP
And while you are at it, call ob_end_flush() or ob_end_clean() at the top of your script.

PHP fopen function timed out?

any idea why fopen would timeout for a file if it is on my server and I know the url is correct?
update: sorry, i should have mentioned this is in php.
the code is:
fopen($url, 'r');
It works if i put in a relative path for the file, but not if $url is a url in my server (but it works for google.com). Thanks for the help.
Alaitnik's answer was right. The problem only appears when i access my own server files through the ethernet interface. How can I fix this? I need to be able to access the file from the ethernet interface because the url loads dynamically (it's generated from a wordpress cms, so the url doesn't technically exist as a file on my server)
you can use
ini_set('default_socket_timeout',2);
before opening the fopen $url . This actually set the default socket connection timout without responding.
Stream_set_timeout sets time out on the stream that is established via fopn or socket opening functions.
Try this may be helpful for you.
It appears that you're trying to download a file from your own server using the HTTP protocol from a program running on that same server?
If so, the timeout problem is likely to be web server or network configuration related. Timeouts normally only happen because either:
the server really is taking a long time to send back the answer, or
the TCP connection is being blocked
For example, it may be that your local firewall rules only permit access to www.example.com if those queries come from the ethernet interface, but a locally made connection would try to go via the loopback interface.
maybe your "allow_url_fopen" is set to "Off"
check your php.ini file or phpinfo()
If you are trying to get the HTML of a URL, I suggest using curl instead of fopen.
fopen is best used with local files, coz it does not "know" how to deal with the idiosyncrasies of a network resource.
Check the comments on the documentation of fopen. There's a whole lot of gold in there.
Took me ages to solve this, but here I found it, thanks to Alnitak. Opening the file with localhost in the URL instead of the hostname was what did the trick for me.

Categories