Jump to content

Problem with file_exists and fopen


hitorsplit

Recommended Posts

I am trying to follow a tutorial - http://www.tutorialized.com/view/tutorial/High-score-list-using-PHP/2404 - but I am running into errors, and I cannot resolve them with the information I have found online.

 

As per the tutorial, I have a file called "scores.php" in my root directory (www.hitorsplit.com), and I have a subdirectory called "scores" in the root directory.  The scores subdirectory also contains a file called "score.sco" (www.hitorsplit.com/scores/score.sco).  Permissions are set to read, write, execute for both files and the "scores" subdirectory.

 

The following scores.php code is supposed to check if a file exists.  If it does not exist, it should attempt to create it, then close it. 

 

// Create a Blank File if it doesn't already exist

if (!file_exists($filename))

{

$file=fopen($filename, "w");

fclose ($file);

}

 

However, when I type - http://www.hitorsplit.com/scores.php?filename=scores/score.sco&scoresize=10&action=VIEW&viewtype=HTML - into a web browser, it returns the following error from this portion of the scores.php file:

 

Warning: fclose(): supplied argument is not a valid stream resource in /home/content/n/m/a/nmarwah/html/scores.php on line 9 (in bold above)

 

Basically, what is happening is that file_exists($filename) is returning false (problem #1), and then fopen does not work (problem #2).  As a result, there is no open file to fclose.  Even when I remove the "score.sco" file, fopen still does not create the file.

 

The most common resolutions to this type of problem I have seen are:

1) set permissions - which I think I have done

2) change the way the file names are referenced - which I have tried a couple different ways (eg., $DOCUMENT_ROOT . $filename)

 

Does anyone have any ideas?

 

Link to comment
https://forums.phpfreaks.com/topic/142912-problem-with-file_exists-and-fopen/
Share on other sites

By the way, it is $_SERVER['DOCUMENT_ROOT']

 

Thank you.  I had the wrong syntax for $_SERVER['DOCUMENT_ROOT'].  Here is my new code:

 

// Create a Blank File if it doesn't already exist

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . $filename))

{

$file=fopen($_SERVER['DOCUMENT_ROOT'] . $filename, "w");

fclose ($_SERVER['DOCUMENT_ROOT'] . $file);

die("file dne");

}

else

{

die("file exists");

}

 

After removing "score.sco" from the "scores" directory (this page does not exist: http://www.hitorsplit.com/scores/score.sco), this code still somehow returns "file exists" when I type:

http://www.hitorsplit.com/scores.php?filename=scores/score.sco&scoresize=10&action=VIEW&viewtype=HTML

echo the path before checking to see if it exists to ensure that it is being passed the path you think it should be.  I think you may need to add a / to the path before the filename, but it should throw an error, not tell you the file exists.

You are passing it a relative path.  You need to provide an absolute path to the fopen command.

Uh.... since when?

OK, so it's not an absolute requirement, but it is generally a good idea.  Otherwise PHP has to do the path resolution itself and effectively search for the file in the file structure (read: extra processing time).  The way this is being handled by this user is also somewhat insecure.  I would never pass a path in my file system with a specific file name via URL.  You're also creating a file based on user input.  ALWAYS a bad idea.  And I know you're just creating a blank file at this point, but I can only assume that there will be more to the script about the contents of the file.

You are passing it a relative path.  You need to provide an absolute path to the fopen command.

 

Uh.... since when?

 

This is a long shot here, but the folder is named 'scores' exactly, right?  Not 'Scores' or something?

 

the directory is definitely 'scores'

 

interestingly, $filename seems to be empty or blank or null because when i try the following code, it does not echo anything (but if i uncomment the first echo line, it displays the root directory)...how is that possible?

 

//echo $_SERVER['DOCUMENT_ROOT'] . "/";

echo $filename;

 

// Create a Blank File if it doesn't already exist

if (!file_exists($_SERVER['DOCUMENT_ROOT'] . "/" . $filename))

{

 

die("file dne");

$file=fopen($_SERVER['DOCUMENT_ROOT'] . "/" . $filename, "w");

fclose ($_SERVER['DOCUMENT_ROOT'] . "/" . $file);

}

else

{

die("file exists");

}

register_globals is most likely turned off.  corbin is probably right and you need to do something like:

 

$filename = $_GET['filename'];

 

And don't forget to clean it.  Only bad programmers accept URL input blindly.

 

register_globals is definitely off.  I am extremely new to php, so I don't quite understand what register_global does, what GET does, and what "clean it" means.  But I get the idea: I need to learn the basics.  Thanks for your help!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.