danielandlisa
03-19-2010, 03:25 PM
hi, im trying out this code to get the content from a flatfile and it works but
after submitting it dont change the content
can somebody take a look at this code or try it together with a flat filet o see if it works
/ thanks Lisa
<?php
if ($changefile) {
$slash = stripslashes($_POST['filetest']);
$filetochange = "http://yoursite.com/file.txt ";
$filetochangeOpen = fopen($filetochange,"w") or die ("Error editing.");
fputs($filetochangeOpen,$slash);
fclose($filetochangeOpen) or die ("Error Closing File!");
}
?>
<form method=post action="">
<textarea rows="40" cols="60" name="filetest">
<?
// Implode CSS
$filetochange = "http://yoursite.com/file.txt";
print (implode("",file($filetochange)));
?>
</textarea><br />
<br />
<input type="submit" value="Change File" name="changefile">
</form>
SKDevelopment
03-19-2010, 03:37 PM
You would be able to write to file if it was on the local file system and had permissions for writing. You would be unable to write to a file if you open it over HTTP. The corresponding wrapper does not support writing (http://www.php.net/manual/en/wrappers.http.php).
danielandlisa
03-19-2010, 03:47 PM
ok but how do they mean this code should be used then?
i got this for another forum and they say this code can be used to update
flatfiles through php
tomws
03-19-2010, 03:51 PM
ok but how do they mean this code should be used then?
i got this for another forum and they say this code can be used to update
flatfiles through php
Answer:
You would be able to write to file if it was on the local file system and had permissions for writing.
You're not accessing the local file system when you're using http.
danielandlisa
03-19-2010, 03:52 PM
so what would i need to do make it writable
SKDevelopment
03-19-2010, 03:58 PM
Are you trying to write to a file located at the same server where the script is run ? Then use local file system path instead of URL. If the file has write permissions (for the user under which PHP works), you would be able to write to it.
Under *nix it could be something like this:
$filetochangeOpen = fopen("/home/myfolder/file.txt", "w");
Under Windows it could be something like this:
$filetochangeOpen = fopen("C:\\myfolder\\file.txt", "w");
danielandlisa
03-19-2010, 04:21 PM
thanks for the replies, does it matter if i use the www path or the local path when i use the local path i get failed to open stream but when i use the www path i get the content from the text file. the problem is that it wont write to the file . the txt file is chmod to 777
SKDevelopment
03-19-2010, 04:33 PM
Try to check that your local path to file is correct. You could check it e.g. with file_exists() (http://php.net/file_exists).
danielandlisa
03-19-2010, 04:54 PM
thanks i will try it, maybe the search path is the problem
could you confirm if the code works for you , if you got time that is
SKDevelopment
03-19-2010, 05:05 PM
If I change the line
if ($changefile) {
to
if ($_POST['changefile']) {
and change the path in fopen() to local file system path, then saving works.
I would recommend to change that condition in case you have register_globals off (as I do).
danielandlisa
03-19-2010, 05:10 PM
ok thanks alot
hi just to clarify its the absolute path i should use?
danielandlisa
03-19-2010, 05:51 PM
thanks alot skd, found out the absolute path now it works good
SKDevelopment
03-19-2010, 06:05 PM
You are always welcome.
I tested with relative. In the production environment I would probably use absolute ...