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.
posted by ontic to Computers & Internet (6 answers total)
 
You don't have to worry about WebDAV. Your PHP script would be similar to the following:
<?php 
if ($_SERVER['REQUEST_METHOD'] != 'POST') 
{ 
	<form method="post" action="?do=something">
	<input name="Submit" type="submit" value="Submit">
	</form>
}
else
{ 
	if (isset($_REQUEST['do']) && ($_REQUEST['do'] == "something"))
	{
		// set the value of the $text and $filename variables here
		// append $text to $filename
		exec("cat $text >> $filename",$results);
	}
}
?>

posted by Mr. Six at 2:01 PM on May 25, 2006


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


Response by poster: This is what I use for normal contexts:
	$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


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


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


« Older Help me find a portable Disgaea   |   PhysicsFilter: The Great Racquetball-in-the-Eye... Newer »
This thread is closed to new comments.