PDA

View Full Version : writing to / creating new files to write to


Ankun
11-25-2002, 09:56 PM
I am working on a script to help me add content for my site, I've started by making it possible to chose a file to write to and to change the content of the file. I run into problems though when the file I am trying to add content to doesnt exist, I believe the problem is somewhere toward the bottom of the coding where it creates the file and writes the content to it, if someone could help me clean this up or fix my mistake(s) it would be greatly appreciated :)





Here is the code:
---------------------------------

<?php

if(file_exists($f))
{

if (is_writable($f)) {


if (!$fp = fopen($f, 'r+')) {
print "Cannot open file ($f)";
exit;
}

// Write $c to our opened file.
if (!fwrite($fp, $c)) {
print "Sorry, you are unable to edit the file $f";
exit;
}

echo $c;

fclose($fp);

} else {
print "Sorry, you are unable to edit the file $f";
}
}
else


$new_file = fopen($f, 'r+')
fputs($new_file, $c);
print($c);
fclose($new_file);

?>

Wichetael
11-26-2002, 08:58 AM
In order to create a file with fopen you'll have to use 'w' or 'w+' as an argument, actually I'm wondering why you're using 'r+' since you are only writing to the file and not reading.

Also you'll want to enclose the last 4 statements in brackets otherwise the last 3 statements will always execute, even if the file does exist.

Ankun
11-26-2002, 05:47 PM
the reason I used r+ is because this is only a part of a script i'm working on (my first if you cant tell ;)) and once its complete it will have functions to open a file to display its contents prior to writing it..but..now that I think of it I really dont need r+ right now.. because this has nothing to do with the final project.

Ankun
11-26-2002, 09:25 PM
ok..here is the revamped code, yet it still doesnt work

<?php

if(file_exists($f))
{

if (is_writable($f)) {


if (!$fp = fopen($f, 'w+')) {
print "Cannot open file ($f)";
exit;
}

if (!fwrite($fp, $c)) {
print "Sorry, you are unable to edit the file $f";
exit;
}

echo $c;

fclose($fp);

} else {
print "Sorry, you are unable to edit the file $f";
}
} else {

$new_file = fopen($f, 'r+')
fputs($new_file, $c); //line 29 (parse error)
print($c);
fclose($new_file);

}
?>


now I get a parse error on line 29 (I marked line 29 in the code so ya dun have to count :\)

Galdo
11-26-2002, 10:04 PM
<?php
$new_file = fopen($f, 'r+');
fputs($new_file, $c); //line 29 (parse error)
print($c);
fclose($new_file);
?>


You are missing a ';' after the fopen($f, 'r+').

Ankun
11-26-2002, 10:56 PM
lol haha yeh I just changed that, but that wasnt the only problem I guess.. I get
Warning: fopen("", "w+") - No such file or directory in c:\phpdev\www\test\test2.php on line 9
Cannot open file () as my error now, I have a feeling I coded this completely wrong..?

Galdo
11-27-2002, 08:52 AM
If that is your whole script then you are asking to open $f, which as far as i can see you haven't set to anything.

You need to put something like $f="yourfilenameandpath"; before trying to fopen.

Wichetael
11-27-2002, 10:04 AM
Yes, looking at the error it looks like you haven't assigned $f, also you'll want to change the r+ to w+ in the second fopen call too, otherwise it still won't create the file when you want to.

Ankun
11-27-2002, 06:52 PM
ahhh..i'm so sorry I havent even explained that, this is just like a sample to see if I can do it.. the values for $f and $c will be inputted by the user as the file they want to edit / create the directory, etc etc, as well as the content and so forth.

Ankun
11-27-2002, 07:24 PM
and..it appears that the script works perfectly...if firepages would like to explain why php dev might not work with this script? That'd be great, I dunno if its a bug or wut, Maybe i should manually configure everything :\...but yeh, the script works exactly as it should when I put it on my real server..just doesnt wanna work on my computer :(