JABpi
Posts: 22
Joined: Wed Feb 06, 2013 3:44 am

File access

Thu Mar 06, 2014 2:06 am

I guess this is a linux question but it's on a Pi so here is my question...
I have a script file located in /bin with a selection of streaming radio stations where all but one is remmed out "#"
This file is launched at boot time and plays the selected stream link.
I've installed mplayer and lamp on my Pi starting from "2014-01-07-wheezy-raspbian"

I have some PHP code that allows me to select what station i want to stream and this should modify my bash script file by changing where to remove the "#" in front of the streaming link.

My code is using file() but for some reason I get the die side of it and am unable to modify the file in /bin
To my understanding, it is probably related to the user rights to that file but as much as I've searched, I am finding nothing I can understand.
Here is the code in charge of modifying the script...

Code: Select all

<?php
$filename = "/bin/play-radio";
$file_content = file($filename);
$x = count($file_content);
$fp = fopen($filename, "w+") || die("Cannot open".$filename);
$y = 0;
$c = 1;
while($y < $x)
    {
		if (substr($file_content[$y],0,6) == '#title'){
			
			if ($_POST["rd"] == $c AND substr($file_content[$y+1],0,1) == '#'){
				$file_content[$y+1] = substr($file_content[$y+1],1,strlen($$file_content[$y+1])-1);
				fwrite($fp, $file_content[$y+1]);
				$y++;
			}
			else if ($_POST["sel"] == $c AND substr($file_content[$y+1],0,1) != '#'){
				$file_content[$y+1] = '#'.$file_content[$y+1];
				fwrite($fp, $file_content[$y+1]);
				$y++;
			}
			else {
			fwrite($fp, $file_content[$y]);
			}
		$c++;
		}
		else {
			fwrite($fp, $file_content[$y]);
		}
$y++;
}
fclose($fp);
/**header('Location: index.php');**/
?>

User avatar
lmarmisa
Posts: 1265
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: File access

Thu Mar 06, 2014 10:15 am

If you want to modify a file located inside the /bin directory, you have to get root privileges. Use the command sudo for it.

For example, you can edit the file typing this command:

Code: Select all

sudo nano /bin/path_and_filename_here
If you wish to start a root bash session, type:

Code: Select all

sudo bash
NOTE: be very carefull when you type commands with root privileges. If you do something wrong, you could brick your system!!!.

JABpi
Posts: 22
Joined: Wed Feb 06, 2013 3:44 am

Re: File access

Thu Mar 06, 2014 12:21 pm

I don't have a problem modifying the file myself, maybe i didn't explain properly...

The code above is running as PHP on Apache and I need this code to modify the file located in /bin
I know that Apache is running under www-data Group and I am guessing this Group does not have write permission to the files in /bin

User avatar
lmarmisa
Posts: 1265
Joined: Thu Feb 14, 2013 2:22 am
Location: Jávea, Spain

Re: File access

Thu Mar 06, 2014 12:31 pm

I recommend to define a symbolic link for the file you want to write. Define the target in a folder where php has write permission.

Code: Select all

ln -s target linkname
More info here:
http://www.penguintutor.com/raspberrypi ... ence-guide
http://www.bartbania.com/index.php/crea ... -in-linux/

JABpi
Posts: 22
Joined: Wed Feb 06, 2013 3:44 am

Re: File access

Thu Mar 06, 2014 12:35 pm

I finely found the solution:

Code: Select all

sudo chmod www-data /bin/play-radio

Return to “General discussion”