Help me append a line to a text file in a WebDAV directory from a php script
-
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.
-
Answer:
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); }}?>
ontic at Ask.Metafilter.Com Visit the source
Other answers
ontic: in case you're new to php, don't actually use this code. it's potentially dangerous.
rbs
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.
ontic
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.
Mr. Six
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?
Mr. Six
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 http://freshmeat.net/projects/class_webdav_client/ 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.
scottreynen
Related Q & A:
- How to delete a line from a .txt file in node.js?Best solution by c-sharpcorner.com
- How to paste a line at a time?Best solution by Ask Ubuntu
- How to use BitmapFont.TextBounds to center a line of the text on the screen?Best solution by Stack Overflow
- Is there a limit on the size of a new file or a text file?Best solution by Stack Overflow
- How to start creating a php script, that will be installed on many servers?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.