Help me append a line to a text file in a WebDAV directory from a php script
May 25, 2006 1:44 PM Subscribe
I want to append a line of text to the end of a text file in a password protected WebDAV directory on my webserver from a normal php webpage outside this directory. Can this be done?
Ideally I have a simple form with a submit button. Upon pressing submit, it should call a php script to do this. I just have no idea how to write the php script. I know enough about php to do this in a regular context. I would also like to be able to run other scripts on this text file and read the text file into other webpages that aren't trapped within the WebDAV directory.
Currently I'm using the iDisk WebDAV but I'm not attached to that and have other WebDAV accounts elsewhere. My webserver is shared Linux hosting.
Ideally I have a simple form with a submit button. Upon pressing submit, it should call a php script to do this. I just have no idea how to write the php script. I know enough about php to do this in a regular context. I would also like to be able to run other scripts on this text file and read the text file into other webpages that aren't trapped within the WebDAV directory.
Currently I'm using the iDisk WebDAV but I'm not attached to that and have other WebDAV accounts elsewhere. My webserver is shared Linux hosting.
ontic: in case you're new to php, don't actually use this code. it's potentially dangerous.
posted by rbs at 2:19 PM on May 25, 2006
posted by rbs at 2:19 PM on May 25, 2006
Response by poster: This is what I use for normal contexts:
posted by ontic at 2:41 PM on May 25, 2006
$addedline = trim($_POST['line']); $addingfile = $_POST['file'] ; //open file for writing $fh = fopen($addingfile, 'ab'); //write to it fwrite($fh, "$addedline \n"); //close it up fclose($fh); echo "Added $addedline to $addingfile .";It just throws up an error when used with a file in the WebDAV directory -- I'm guessing because the directory isn't really mine in terms of permissions and/or because the username and password aren't transmitted by the script.
posted by ontic at 2:41 PM on May 25, 2006
ontic: in case you're new to php, don't actually use this code. it's potentially dangerous.
I guess the objection is that anyone could call that URL to do stuff to your log file.
My code should be okay for an intranet deal or where you have already validated a user session or the like. I'm only indicating how you'd actually handle the POST form data and the append action.
You'd want to make sure that the PHP is doing other good and necessary stuff, like making the sure the POST request only comes from a valid user.
posted by Mr. Six at 3:08 PM on May 25, 2006
I guess the objection is that anyone could call that URL to do stuff to your log file.
My code should be okay for an intranet deal or where you have already validated a user session or the like. I'm only indicating how you'd actually handle the POST form data and the append action.
You'd want to make sure that the PHP is doing other good and necessary stuff, like making the sure the POST request only comes from a valid user.
posted by Mr. Six at 3:08 PM on May 25, 2006
It just throws up an error when used with a file in the WebDAV directory -- I'm guessing because the directory isn't really mine in terms of permissions and/or because the username and password aren't transmitted by the script.
Your script will operate with the same permissions as the web server. If the web server does not have access to the directory and file you are writing to, you'll get an error.
Can you let us know what error message you see in the system log?
posted by Mr. Six at 3:11 PM on May 25, 2006
Your script will operate with the same permissions as the web server. If the web server does not have access to the directory and file you are writing to, you'll get an error.
Can you let us know what error message you see in the system log?
posted by Mr. Six at 3:11 PM on May 25, 2006
I'm guessing because the directory isn't really mine in terms of permissions and/or because the username and password aren't transmitted by the script.
That sounds like a good guess. One solution would be to edit the files via PHP WebDAV rather than through the local filesystem. Another would be to change either the file permissions (and probably default permissions of files written by WebDAV), or the user PHP and/or WebDAV are running as.
posted by scottreynen at 5:16 PM on May 25, 2006
That sounds like a good guess. One solution would be to edit the files via PHP WebDAV rather than through the local filesystem. Another would be to change either the file permissions (and probably default permissions of files written by WebDAV), or the user PHP and/or WebDAV are running as.
posted by scottreynen at 5:16 PM on May 25, 2006
« Older Help me find a portable Disgaea | PhysicsFilter: The Great Racquetball-in-the-Eye... Newer »
This thread is closed to new comments.
posted by Mr. Six at 2:01 PM on May 25, 2006