View Full Version : fopen() problem
Schmoopy
01-08-2009, 09:17 PM
Hi all, just wanted to mess around with this function a bit and was trying a few examples from w3 schools, but for some reason it doesn't seem to work for me.
I have the following simple code:
<?php
$file = fopen("http://www.w3schools.com/","r");
?>
But although it doesn't give any errors when I open the page, nothing happens. That code is taken straight from the w3 website, is it something to do with my host not allowing this feature? I thought it would play quite a big part for some websites so it seems sort of illogical for it to be disabled, any ideas?
Thanks,
Jack
techietim
01-08-2009, 11:22 PM
It works!
You have to do something with that stream now. Might I suggest fread() (http://php.net/fread)?
You cannot open w3schools - you must chose a local file.
You can open W3Schools if allow_url_fopen is on in your host's config. As techietim said, the main problem is that you've got the stream, but you're not doing anything with it.
Schmoopy
01-09-2009, 11:47 AM
I see, well that was pretty silly of me - however, I'm trying to open a file now on my C: drive that I know is there and has the path of - "C:\hello.txt".
Here is my PHP code, and the errors I get:
<?php
$filename = 'C:\\hello.txt';
$file = fopen($filename,"r");
$contents = fread($file, filesize($filename));
fclose($file);
echo $contents;
?>
Warning: fopen(C:\hello.txt) [function.fopen]: failed to open stream: No such file or directory in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 12
Warning: filesize() [function.filesize]: stat failed for C:\hello.txt in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 14
Warning: fread(): supplied argument is not a valid stream resource in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 14
Warning: fclose(): supplied argument is not a valid stream resource in /mnt/local/home/garlandsails/bristoldnb.co.uk/php/index.php on line 15
I managed to get it to work for a file on the server, just not a local file - no errors but nothing showed up, any more suggestions?
I've also tried C:\hello.txt, C:/hello.txt but both got me nowhere.
Thanks for your help thus far,
Jack
You've gotta put the file or directory in:
/mnt/local/home/garlandsails/bristoldnb.co.uk/php/
Schmoopy
01-09-2009, 01:23 PM
Yea I have succesfully done that before, but I thought you could open a file on the local hard disk with fopen? At least that's what it says on the W3 website.
Can you post a link to it on the w3 site? I can't find where it says that on w3schools..
http://www.w3schools.com/php/func_filesystem_fopen.asp
Post the link from w3 though.
Schmoopy
01-09-2009, 08:10 PM
I seemed to have read the page here wrongly : http://uk2.php.net/fopen
I was getting confused with how it was saying "c:\\data\\info.txt", I interpreted as the C: drive.
The other reason I was led to believe you could open a local file was because I was reading this tutorial: http://www.allsyntax.com/tutorials/PHP/3/Simple-PHP-Tutorial/6.php
And on there it says "This can be a file on your system, or a file on a remote server."
Is this just false, or could you do it on earlier versions of PHP?
Thanks,
Jack
You can if your doing it local host such as:
C:/wamp/www/quick.php
Schmoopy
01-09-2009, 09:00 PM
Ah I see, that makes sense, I guess it would be a pretty big security issue if PHP could open files and edit them on someone's computer.
Haha, yeah:
$gl = glob('C://*');
Super unsecure.
Schmoopy
01-09-2009, 09:18 PM
Does that search through all files on their PC? lol
Well - if you were allowed to open files on the hard drive. But PHP doesn't allow it(thank god).
When it says 'your system' it means 'the system on which the PHP script is running' — i.e. your server. The webserver cannot (directly) access files on the client machine.
This isn't a question of what PHP allows: HTTP doesn't provide a mechanism to do it (for fairly obvious reasons). Of course, if you have an FTP or SSH server set up on the HTTP client machine, it's a different matter.
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.