Reputation: 329
I have code I'm using to create a file using the name of one field, then opening that file and writing the contents from another. That works fine.
However when users attempt to pull up this file and change information and save it it doesn't overwrite the information.
If a user deletes the file (which works) then recreates it using the same name, but attempts to input new data, it creates the new file (with the same name as the old file) however it retains the old information and doesn't update. I can't find out what's causing that.
I was originally using file_put_contents and I've attempted to use different parameters for the fopen() but it doesn't seem to work. They can create and save just fine, however the main issues are they can't edit as it doesn't overwrite data and they can't delete the file (even as a workaround) and recreate it using the same name.
edited to add: Also when they try and save information, instead of opening the file and overwriting it, it creates a second and NEW file filename.html.html
// Put together the full path of the file we want to create
$FILENAME = $USER_DIRECTORY.'/'.$_POST['CodeDescription'].'.html';
$CODE = $_POST['Code'];
if( !is_file( $FILENAME ) ):
// Open the text file, write the contents, and close it.
$fp = fopen($FILENAME, "w+") or die("Couldn't open $FILENAME for
writing!");
fwrite($fp, $CODE) or die("Couldn't write values to file!");
endif;
header('Location: mywebsite.comsaved=1&file='.$FILENAME);
}
?>
Upvotes: 0
Views: 2989
Reputation: 46
A quick hack might be to try this:
// Put together the full path of the file we want to create
$FILENAME = $USER_DIRECTORY.'/'.$_POST['CodeDescription'].'.html';
$CODE = $_POST['Code'];
//Delete file if exists.
if(is_file( $FILENAME)) {
unlink($FILENAME);
}
// Open the text file, write the contents, and close it.
$fp = fopen($FILENAME, "w+") or die("Couldn't open $FILENAME for
writing!");
fwrite($fp, $CODE) or die("Couldn't write values to file!");
header('Location: mywebsite.comsaved=1&file='.$FILENAME);
}
Upvotes: 3