brendosthoughts
brendosthoughts

Reputation: 1703

Writint to a .txt file with php in unix enviroment where file need root permisions

okay now this is utterly bewildering to me and I can't figure out why this script is not working basically I am attempting to write a line of data to a .txt file in php here is the part of the script which matters:

    set_include_path('/etc/apache2/rewrite/');
    if($_GET['type']=="14"){ //video is documentary
         $file = '/etc/apache2/rewrite/Documentaries.txt';
         $line= clean_url($content[0]['title']) . " " . $content[0]['content_id'] ."\n";
         file_put_contents($file, $line, FILE_APPEND);

            echo "inseted into documentaries <br>" . $line;
    }else if ($_GET['type']=="17"){//video is a talk
         $file = '/etc/apache2/rewrite/Talks.txt';
         $line= clean_url($content[0]['title']) . " " . $content[0]['content_id'] ."\n";
         file_put_contents($file, $line, FILE_APPEND);
            echo "inseted into talks";

    }else if($_GET['type']=="15"){//video is debate
         $file = '/etc/apache2/rewrite/Debates.txt';
         $line= clean_url($content[0]['title']) . " " . $content[0]['content_id'] ."\n";
         file_put_contents($file, $line, FILE_APPEND);
                            echo "inseted into debates";
   }

and note that after this block i echo the $line which is something like this

   inside-nature-s-giants-the-leatherback-turtle 13443

and also the echo's inside the if statement are also echoe'd however nothing is modified in any of the files for a reason I can't seem to figure out I have given the script both write and execute permission and still can't seem to get this to work .. I am unsure what is going on As I use similar logic in other scripts without problem any help would be greatly greatly appreciated as I've sworn at my terminal a little to much for my liking Thanks, Brendan


*Update * I do not have the correct file permisions thanks to @SyntaxLAMP for reminding me to check error logs

okay here is the permision for the php file

-rwxrwxrwx 1 root root 9202 Jan 25 07:48 update_untagged.php

and the permisions of the three files I am attempting to write to

-rw-r--r-- 1 root root      7 Jan 25 05:05 Debates.txt
-rw-r--r-- 1 root root 110691 Jan 25 07:16 Documentaries.txt
-rw-r--r-- 1 root root  45747 Jan 25 05:05 Talks.txt

they both have the same permisions so I dont understand why this still isn't working any help with this would be greatly appreciated stil

Upvotes: 0

Views: 93

Answers (1)

Jammerx2
Jammerx2

Reputation: 864

You need to give the user the script is running as access to those files. You can either give everyone access to them like this:

chmod 666 Debates.txt Documentaries.txt Talks.txt

Or set the owner to whatever the script runs as (possibly www-data):

chown www-data Debates.txt Documentaries.txt Talks.txt

Edit:

Just to be clear, don't give everyone access unless there's a valid reason to do so, it's a lot safer just to change the owner to whatever the script runs as.

Upvotes: 2

Related Questions