/    Sign up×
Community /Pin to ProfileBookmark

It is my understanding that …

$fp=fopen($currentfile,”w”) or die(“Cannot open $currentfile”); …

Should create the file and open the file for writing. The actual file name is generated by previous lines. But on my host server, I receive the cannot open file message. NOW …

when I create a “dummy file” by the name that is generated and chmod it for writing, the file is opened and the script goes on just fine. But I need this to happen dynamically.

Also, I have another script that worked for several months that also created and opened files for writing that suddenly stopped working. My host admitted recently switching servers.

What is going amuck? Is this my faullt? Can I correct this is some way?

to post a comment
PHP

8 Comments(s)

Copy linkTweet thisAlerts:
@JDM71488Sep 06.2007 — can i see an example output of $currentfile?
Copy linkTweet thisAlerts:
@PittsburghRedauthorSep 07.2007 — A little form on the website collects name, city, state, and comment. A text file holds the current # of the file to be created. Files are named Comment1, Comment2, etc ... Script will collect the info and create a little html file that will print out the comments formatted in a little table. Using flat files because we can only use one data base and that runs the Message Board.

Website is in early stage. You can view some test comments (5 are linked in)at http://www.endteacherabuse.info/WCC/Talk.html


$fp=fopen("QCcount.txt","r");

$count=fgets($fp);

fclose($fp);

$currentfile="Comment".$count.".html";

$prev=$count+1;

$next=$count-1;

$prevfile="Comment".$prev.".html";

$nextfile="Comment".$next.".html";

$count++;

$fp=fopen("QCcount.txt","w");

fwrite($fp,$count);

fclose($fp);



$fp=fopen($currentfile,"w") or die("Cannot open $currentfile");

fwrite($fp,"<HTML>n");

fwrite($fp,"<HEAD>n");

fwrite($fp,"<TITLE>White Chalk Crime: Comment</TITLE>n");

fwrite($fp,"</HEAD>n");

fwrite($fp,"<BODY>n");

fwrite($fp,"<TABLE WIDTH="400" BORDER="1" BORDERCOLOR="green" CELLPADDING="5">n");

fwrite($fp,"<TR><TD WIDTH="400">n");

fwrite($fp,"$today<BR>n");

fwrite($fp,"$_POST[username], $_POST[city], $_POST[state]<BR><BR>

n");

if ($_POST[category]=="question"){

fwrite($fp,"<FONT SIZE="+1">Q:</FONT><BR><BR>n");

}

fwrite($fp,"$_POST[remark]<BR><BR>n");

if ($_POST[category]=="question"){

fwrite($fp,"<FONT SIZE="+1">A:</FONT><BR><BR>n");

}

fwrite($fp,"</TD></TR>n");

fwrite($fp,"<TR><TD ALIGN="center" WIDTH="400">n");

fwrite($fp,"<A HREF="$prevfile">Previous</A> |

<A HREF="$nextfile">Next</A><BR>n");

fwrite($fp,"</TD></TR>n");

fwrite($fp,"</TABLE><BR><BR>n");

fwrite($fp,"</BODY>n");

fclose($fp);
Copy linkTweet thisAlerts:
@PittsburghRedauthorSep 10.2007 — Actually, what does the file output have to do with it?

My question is ... The open file for writing function creates the file if does not exist. How can the file get write permissions if it is [B][/B]created by the script?
Copy linkTweet thisAlerts:
@houseySep 11.2007 — Not sure if i'm understanding you but you can use the chmod() php function after doing the fopen http://uk2.php.net/manual/en/function.chmod.php

so like [code=php]chmod("/somedir/somefile", 777);[/code]

I can only think it used to work then someone changed permission settings on the server..

Not sure if this is what you mean though...
Copy linkTweet thisAlerts:
@PittsburghRedauthorSep 12.2007 — Yes ... this is surely close to the solution. But the line

$fp=fopen($currentfile,"w") or die("Cannot open $currentfile");

should create the file if does not exist. Previous lines create only the file name. If the file does not exist prior to this line, how can I use the chmod function to give it write permission? And after this line, it is too late. The program has already exited.

Here is another aspect, I have this same type of logic and line in another script which worked fine or months, until my host changed servers. Then the same line went amok. My host is no help.
Copy linkTweet thisAlerts:
@houseySep 12.2007 — I think you can use chmod after the fopen, like:

[code=php]$fp=fopen($currentfile,"w") or die("Cannot open $currentfile");
chmod("$currentfile", 777);[/code]


The change of servers probably means there are different user/dir permissions.

What happens when you try the above?
Copy linkTweet thisAlerts:
@PittsburghRedauthorSep 12.2007 — I didn't try chmod after the fopen because the script FAILS on the fopen precisely because the file does not have write permissions.

But I found this queer function in one of my php texts: touch(). It CREATES a file! So, after the script generates the name, touch() will create the file, THEN I can chmod() it, THEN I can fopen it for writing.


I put these two lines before the fopen():

touch($currentfile);

chmod($currentfile, 0777);

And it worked!!! The file was created and the script went on to generate the needed contents. I've been trying to do this for 3 weeks. Your pointing me to the chmod() function was the catalyst. Thank you. Thank you. It works!!!
Copy linkTweet thisAlerts:
@houseySep 12.2007 — no problem.. glad it helped..
×

Success!

Help @PittsburghRed spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...