fopen("names.txt","w"); does not create a file

Hi,
The names.txt file will still NOT accept entered names from the form.

I am not new to programming but I am new to php.
Macbookpro. OS X 10.8
Netbeans

Tutorial program objective:
A form opens in a browser.
The user types in their name.
The name should be inserted into to the names.txt file.

I am creating a $handle to open and create the files if it is not already created.
The browser form works fine. If I click submit w/o entering a name, it echoes, “please type a name”.

The file is not created at all.
I have been troubleshooting.
I even tried creating names.txt independently and it resides in the directory where all of my other tutorials programs reside.
This names.txt file will still NOT accept entered names from the form.

[php]<?php
$handle = fopen(‘names.txt’, ‘w’); // I tried w+ and double quotes too

Name:


[/php]

Hi Kevin. Thanks for replying. Please do Not take this wrong. I know that you are one of the administrators or moderators who donates tons of time to this forum.

When I learn something, I start with the fundamentals.
I am learning on-line and taking advantage of all the freely shared knowledge in our world.
I already paid plenty for formal education.

RESPONSE.
The FORM OPENS in the web browser just fine.
I type a name in the name field and click ‘submit’.
This is fopen( ) function is suppose to create the file names.txt and add that name into the file.

I have spend several hours over the past few days troubleshooting a few problems.
This is a big obstacle.
If I cannot get this to work I cannot proceed through the tutorials.
We will be using this data in names.txt and inputting to a database for instance using mySQL.
Does it have anything to do with my PATH ?
Or settings elsewhere.

Thanks again.

Your giving two different messages. Your subject says “does not create file” but your post says names.txt will not accept names from the form. So is it both problems or just one? Your code is enough to create the file and thats it.

You said

The names.txt file will still NOT accept entered names from the form.

Without fwrite nothing is ever going to be written to the names.txt

This is not correct:

This is fopen( ) function is suppose to create the file names.txt and add that name into the file.

fopen will not ever add anything to the file. you need fwrite.

[php]<?php
if ($_POST)
{
$handle = fopen(‘names.txt’, ‘w’);
fwrite($handle, “{$_POST[‘name’]}”);
}
?>

Name:


[/php]

Thanks again Kevin.
I am still troubleshooting this. [size=12pt]Please do not give up on me.[/size]
This is sort of ridiculous.
This is a simple command.
I used this command in another chapter of my life or something similar.
When I ask, please do not give up on me - because I am on a mac !!
I found this by scrutinizing the php.ini.

“If your scripts have to deal with files from Macintosh systems,
; or you are running on a Mac and need to deal with files from
; unix or win32 systems, setting this flag will cause PHP to
; automatically detect the EOL character in those files so that
; fgets() and file() will work regardless of the source of the file.
; http://php.net/auto-detect-line-endings
;auto_detect_line_endings = Off”

  1. I uncommented this line: ;auto_detect_line_endings = Off
  • sudo apachectl restart
  • closed and reopened ide netbeans.
  • file names2.txt NOT created
  1. I changed the line to auto_detect_line_endings = On
  • sudo apachectl restart
  • closed and reopened ide netbeans.
  • file names2.txt NOT created

Here is the code and the properties.
properties
projectfolder:/Library/WebServer/Documents/phpWithAlex
sourcefolder:/Library/WebServer/Documents/phpWithAlex
webServer/Documents/phpWithAlex
web root:
configuration
run as: Local Web Site running on local server
project url: http://localhost/phpWithAlex/
index file: index.php

index.php
[php]if ($_POST)
{
$handle =fopen(‘names2.txt’, ‘w’);
fwrite($handle, “{$_POST[‘name’]}”);
}
?>

Name:




[/php]

I type in a name and click on submit.

[php]Not Found
The requested URL /phpWithAlex/names2.txt was not found on this server.[/php]

when I use this command [php]$browser = $_SERVER[“PHP_SELF”];[/php]
in this test file server-path-self.php

[php]<?php
//$browser = $_SERVER[“HTTP_USER_AGENT”];
$browser = $_SERVER[“PHP_SELF”];
echo( “Hello World !\n”); ?>

<?php print("Hello World !\n");?> <?php print("Hello World !
");?>

You are using the <?php echo($browser);?> web browser.

<?php if(!$_POST["formName"]){ //switch to html mode to display the form ?> name: <?php }else{ ?>

Hello There <?php echo($_POST["formName"]); ?> !

<?php } ?> [/php]

[php]Hello World !
Hello World !
Hello World !
You are using the /phpWithAlex/server-php-self.php web browser.
name: [/php]

I hope this email CLARIFIES the problem.
You are probably on a linux server or on a pc but you must have acquaintance or colleagues that know this simple resolution. This has been an obstacle for days. I want to move into using these files with php and mysql.
thanks again. it is saturday but i am on this.

By the Way. This posting system is very nice. Many options and a preview.

Are you able to provide me a temporary login to your server? Send it in private email.

Do you have the permissions set properly for the folder you are trying to write in?

I had to set permissions on that folder as you brought to my attention.
Still having another issue with passwords as you know from other post.
Thanks.

Sponsor our Newsletter | Privacy Policy | Terms of Service