casey64 Posted January 25, 2009 Share Posted January 25, 2009 Could someone help me understand why this isn't work or how to isolate the issue? Originally I wanted to open a file called stats.txt, but I couldn't get that to work so I thought I would try opening my myFunctions.php file because I am using it as an include in my main file and it is obviously working as an include, so I know the path and access should be correct since the include is working. I get 'unable to open file in each case'. My include code is: include("./myFunctions.php"); My file open code is: $fh = fopen("./myFunctions.php", "r"); if($fh==false) { echo("Unable to open file"); } I've also tried $fh = fopen("myFunctions.php", "r"); if($fh==false) { echo("Unable to open file"); } $fh = fopen("/myFunctions.php", "r"); if($fh==false) { echo("Unable to open file"); } $fh = fopen('./myFunctions.php', 'r'); if($fh==false) { echo("Unable to open file"); } Some other info, I try opening a directory as well and that doesn't work: if ($handle = opendir("./")) { while (($file = readdir($handle)) !== false) { echo "filename: $file : filetype: " . filetype($dir . $file) . "\n"; } } else { echo "<br>Directory not opened."; } closedir($dh); If I do phpinfo it shows the server is running version: PHP Version 4.3.11 Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/ Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 To read a text file do echo file_get_contents("file.txt"); Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-745940 Share on other sites More sharing options...
casey64 Posted January 25, 2009 Author Share Posted January 25, 2009 I don't think I can read it yet since it doesn't even open, as this code always returns "Unable to open file". $fh = fopen('./myFunctions.php', 'r'); if($fh==false) { echo("Unable to open file"); } if($fh==True) { echo ("I got the file opened"); } Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-745945 Share on other sites More sharing options...
DeanWhitehouse Posted January 25, 2009 Share Posted January 25, 2009 Try the code i just said, lol Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-745950 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2009 Share Posted January 25, 2009 Add the following two lines of code immediately after your first opening <?php tag to get php to tell you why the file cannot be accessed - ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-745956 Share on other sites More sharing options...
casey64 Posted January 25, 2009 Author Share Posted January 25, 2009 Blade, Ok, I tried that and it works. It opened the file and dumped the text to the screen. So thanks, but my goal I guess was to be able to open the file, read it in line by line and do some processing on the data. Not to just dump the data out. PFMaBiSmAd, Thanks that would be great if I could see why there appears to be an error opening the file. So now that I know how to throw the error messages out I can troubleshoot things more myself. However, in this case, it didn't give me an error message at all. My Ouput is: Unable to open file abcde This is a data dump from a text file From this code: // Show error messages ini_set ("display_errors", "1"); error_reporting(E_ALL); // Open file and then check to see if it was opened $fh = fopen("stats.txt", "r"); if(!$fh) { echo("<br>Unable to open file<br>"); } if($fh) { echo ("<br>I got the file opened<br>"); } // Close file fclose("stats.txt"); // Print contents of a text file echo file_get_contents("stats.txt"); Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-745976 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2009 Share Posted January 25, 2009 Either the error messages are not being displayed in the page because your code is inside of some HTML such as a form element (in which case a "view source" should show them), or that is not your actual code being executed. The fclose() statement is the wrong syntax and would have displayed - Warning: fclose(): supplied argument is not a valid stream resource for both the posted code and code using $fh when the file did not open. What is your actual code? Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-746016 Share on other sites More sharing options...
casey64 Posted January 25, 2009 Author Share Posted January 25, 2009 Thanks for giving me another suggestion. Based on that I created an entirely new page called fielopen.php that has no html code in it at all, not even in the echo's. I still get the 'unable to open file' message I programmed. I left in the bad fclose hoping to get an error message but didn't. This is the page http://casey64.com/php/fileopen.php Is it possible it could be something with the server config? That is on this page: http://casey64.com/php/phpinfo.php Thanks!! <?php // Show error messages ini_set ("display_errors", "1"); error_reporting(E_ALL); // Open file and then check to see if it was opened $fh = fopen("stats.txt", "r"); if(!$fh) { echo("Unable to open file"); } if($fh) { echo ("I got the file opened"); } // Close file fclose("stats.txt"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-746091 Share on other sites More sharing options...
PFMaBiSmAd Posted January 25, 2009 Share Posted January 25, 2009 Since that code is not producing a Warning for that fclose() statement, something is not as expected with your error_reporting. The phpinfo() shows a huge list of disabled functions, among them is fopen() and ini_set(). Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-746104 Share on other sites More sharing options...
casey64 Posted January 26, 2009 Author Share Posted January 26, 2009 Thank you!! I guess I will need to contact my hosting provider and see if I can enable those functions or if I have to find another provider. Of course I'm assuming there are other providers which will allow me to enable them. Or is there just another way I should be going about doing what I want to do? Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-746164 Share on other sites More sharing options...
casey64 Posted January 26, 2009 Author Share Posted January 26, 2009 I just figured out my web hosting is on Windows and configured to use IIS6 and php4 (in safe mode). Apparently they have an option to use IIS7 and php5. So I am trying to figure out how I upgrade to that and hopefully then have access to the fopen(). Thanks Again! This seems to be a great board! I posted this same problem on another board (since it came up first when I searched for a board). My question has been there ALL day with no response at all!!! Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-746183 Share on other sites More sharing options...
PFMaBiSmAd Posted January 26, 2009 Share Posted January 26, 2009 That they disabled fopen(), but not file_get_contents() makes me wonder why they are disabling functions at all. It does not do any good to stick your finger just part way into the hole in the dike. The water will still leak out. Quote Link to comment https://forums.phpfreaks.com/topic/142368-solved-file-open-problem/#findComment-746203 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.