Jump to content

[SOLVED] File Open Problem


casey64

Recommended Posts

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!

 

 

Link to comment
Share on other sites

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"); }

Link to comment
Share on other sites

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");

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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");
?>

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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!!!

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.