Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-19-2010, 03:25 PM   PM User | #1
danielandlisa
New Coder

 
Join Date: Dec 2009
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
danielandlisa is an unknown quantity at this point
edit flatfile can somebody try this code

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>

Last edited by danielandlisa; 03-19-2010 at 03:28 PM.. Reason: type wrong
danielandlisa is offline   Reply With Quote
Old 03-19-2010, 03:37 PM   PM User | #2
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
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.
__________________
PHP Programmer

Last edited by SKDevelopment; 03-19-2010 at 03:39 PM..
SKDevelopment is offline   Reply With Quote
Old 03-19-2010, 03:47 PM   PM User | #3
danielandlisa
New Coder

 
Join Date: Dec 2009
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
danielandlisa is an unknown quantity at this point
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

Last edited by danielandlisa; 03-19-2010 at 03:47 PM.. Reason: type wrong
danielandlisa is offline   Reply With Quote
Old 03-19-2010, 03:51 PM   PM User | #4
tomws
Senior Coder

 
tomws's Avatar
 
Join Date: Nov 2007
Location: Arkansas
Posts: 2,644
Thanks: 29
Thanked 330 Times in 326 Posts
tomws will become famous soon enoughtomws will become famous soon enough
Quote:
Originally Posted by danielandlisa View Post
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:
Quote:
Originally Posted by SKDevelopment View Post
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.
__________________
Are you a Help Vampire?
tomws is offline   Reply With Quote
Old 03-19-2010, 03:52 PM   PM User | #5
danielandlisa
New Coder

 
Join Date: Dec 2009
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
danielandlisa is an unknown quantity at this point
so what would i need to do make it writable
danielandlisa is offline   Reply With Quote
Old 03-19-2010, 03:58 PM   PM User | #6
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
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:
PHP Code:
$filetochangeOpen fopen("/home/myfolder/file.txt""w"); 
Under Windows it could be something like this:
PHP Code:
$filetochangeOpen fopen("C:\\myfolder\\file.txt""w"); 
__________________
PHP Programmer

Last edited by SKDevelopment; 03-19-2010 at 04:01 PM..
SKDevelopment is offline   Reply With Quote
Old 03-19-2010, 04:21 PM   PM User | #7
danielandlisa
New Coder

 
Join Date: Dec 2009
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
danielandlisa is an unknown quantity at this point
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
danielandlisa is offline   Reply With Quote
Old 03-19-2010, 04:33 PM   PM User | #8
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
Try to check that your local path to file is correct. You could check it e.g. with file_exists().
__________________
PHP Programmer
SKDevelopment is offline   Reply With Quote
Old 03-19-2010, 04:54 PM   PM User | #9
danielandlisa
New Coder

 
Join Date: Dec 2009
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
danielandlisa is an unknown quantity at this point
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
danielandlisa is offline   Reply With Quote
Old 03-19-2010, 05:05 PM   PM User | #10
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
If I change the line
PHP Code:
if ($changefile) { 
to
PHP Code:
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).
__________________
PHP Programmer
SKDevelopment is offline   Reply With Quote
Users who have thanked SKDevelopment for this post:
danielandlisa (03-19-2010)
Old 03-19-2010, 05:10 PM   PM User | #11
danielandlisa
New Coder

 
Join Date: Dec 2009
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
danielandlisa is an unknown quantity at this point
ok thanks alot
hi just to clarify its the absolute path i should use?

Last edited by danielandlisa; 03-19-2010 at 05:11 PM.. Reason: type wrong
danielandlisa is offline   Reply With Quote
Old 03-19-2010, 05:51 PM   PM User | #12
danielandlisa
New Coder

 
Join Date: Dec 2009
Posts: 24
Thanks: 2
Thanked 0 Times in 0 Posts
danielandlisa is an unknown quantity at this point
thanks alot skd, found out the absolute path now it works good
danielandlisa is offline   Reply With Quote
Old 03-19-2010, 06:05 PM   PM User | #13
SKDevelopment
Regular Coder

 
Join Date: Mar 2006
Posts: 238
Thanks: 3
Thanked 37 Times in 37 Posts
SKDevelopment has a little shameless behaviour in the past
You are always welcome.

I tested with relative. In the production environment I would probably use absolute ...
__________________
PHP Programmer
SKDevelopment is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:26 PM.


Advertisement
Log in to turn off these ads.