Jump to content

[SOLVED] fopen()


Recommended Posts

Warning: fopen(C:\Program Files\Apache Software Foundation\Apache2.2 iles\counter.txt) [function.fopen]: failed to open stream: Invalid argument in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Counter.php on line 4

 

I have apache2.2 installed on my computer and running it as localhost so I can practice php, the problem is... I cannot figure out why I can't set it to allow me to create files and read them. I tried to make a simple page hit counter and it's driving me insane.

Link to comment
Share on other sites

Sorry I was just assuming it was apache permisions and not a mistake in code. I basically duplicated the script in my book and the error I got is in the book but it doesn't really give a straight fix besides the "mkdir" and "chmod" commands with a note to bear in mind that world writable directories are dangerous. It also stated this problem was more of a Unix issue which I am running on Windows...

 

<?php

 

$document_root = $_SERVER['DOCUMENT_ROOT'];

$countertxt = fopen('C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\files\counter.txt', 'w+');

 

$number = fgets($countertxt, 999);

echo $number;

 

$number++;

 

fwrite($countertxt, $number);

fclose($countertxt)

?>

 

That is my script, now the counter wasn't part of a tutorial in the book but I wanted to see if I could make one following the same principle as the example in my book.

 

I am very noob and I am sorry, if someone could explain to me

 

Link to comment
Share on other sites

Don't assume anything in programming. Computer's don't.

 

An error due to permissions would have stated there was a permissions problem with the folder or file. A message stating an Invalid argument means that one of the two arguments was invalid.

 

The posted code is not the code that generated that error (which I just tested). The only way you could get that error is if your actual code used double-quotes around the first parameter (tested), which would have caused the \ to be treated as escape characters and not a path separator.

 

Single-quotes would work around the first parameter, or if using double-quotes, you must escape the \ and make them double \\, or you can just use / with both single and double quotes, which would then work with all operating systems.

 

You also need to use the 'r+' mode and do an fseek() before you do the  fwrite().

Link to comment
Share on other sites

Well I don't know what to say then. Because the exact code that I Copied and pasted is the only thing I have in my counter.php file and it gives me that error. It has to be something with permissions because my book stated the exact error and said...

 

"If you receive this error, you need to make sure that the user under which the script runs has permission to access the file you are trying to use. Depending on how your server is set up, the script might be running as the web server user or as the owner of the directory where the script is located.

  On most systems, the script runs as the web server user. If your script is on a Unix system in the ~/public_html/chapter/ directory, for example, you could create a world writeable directory in which to store the order by typing the following:

 

mkdir ~/orders

chmod 777 ~/orders "

 

The problem is, I am running on a Windows server and I don't know if this pertains to me. And I am unsure if I want to use this command because I am not to sure exactly what it does. It says to bear in mind that directories and files that anybody can write to are dangerous. If it's dangerous to give permissions to a simple counter then why bother?

 

This post got moved to PHP help and I am still wondering why. I am very noob but I am certain that my book is correct. The only problem is that it doesn't go in depth about apache and server permissions.

 

Another thing is I am unsure why I need to use fseek because I thought w+ was meant to open the file for reading and writing starting from the begging of the document and if there was no file it would create one. and r+ would do the same as w+ besides it doesn't create a file if there isn't one.

Link to comment
Share on other sites

A permissions error for that code would look like the following (tested) -

 

Warning: fopen(C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\files\counter.txt) [function.fopen]: failed to open stream: Permission denied in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yourfile.php on line 4

 

An Invalid argument message means an invalid argument, i.e. an argument that was not valid.

Link to comment
Share on other sites

My fault, that was my trying to fix the problem myself. I didn't get that originally.

 

Warning: fopen(C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\files\counter.txt) [function.fopen]: failed to open stream: No such file or directory in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Counter.php on line 4

 

Warning: fgets(): supplied argument is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Counter.php on line 6

 

Warning: fclose(): supplied argument is not a valid stream resource in C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Counter.php on line 12

 

There is no invalid argument. The invalid argument was from me trying to mess around with things... Sorry.

Link to comment
Share on other sites

I'm going to venture a guess that PFMaBiSmAd is going to say something along the lines of "well, that means there is no such file or directory as C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\Counter.php"

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.