Link to home
Create AccountLog in
Avatar of aej1973
aej1973

asked on

creating a symbolic link using php...

Hi, I am trying to create a symbolic link in my www folder using php but the code does not seem to work. My vbox is user root with permission 777 and /50 folder has permission as follows:

-rw-r--r-- 1 www-data www-data . My code is
<?php
$target = "/var/lib/ast/music/50";
$filename = "CCN.wav";
$structure = $target.$filename;
$src="/var/www/vbox/testlink";
symlink($structure,$src);
echo readlink($src);
?>

Open in new window

.

Can someone let me know how I can get this to work. Thank you.
SOLUTION
Avatar of Hugh McCurdy
Hugh McCurdy
Flag of United States of America image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
If that doesn't work, try making the symbolic link manually and report what happens.  In the report, gives us both the command you used and the result.  Do this at a $ (not a #).

Also, if the above doesn't work, please insert this code between lines 5 and 6

echo "symlink (" . $structure . ", " . $source . ")" . PHP_EOL;

Run the script and report the output back to us.
Avatar of aej1973
aej1973

ASKER

hmccurdy, I tried what you said and it does not work.

<?php
$target = "/var/lib/ast/music/50/";
$filename = "CCN.wav";
$structure = $target.$filename;
$src="/var/www/vbox/testlink";
symlink($structure,$src);
echo readlink($src);
?>

Open in new window

Avatar of aej1973

ASKER

the output is : symlink (/var/lib/ast/music/50/CCN.wav, /var/www/vbox/testlink)

The output of running the following comnand :

ln -s '/var/lib/ast/music/50/CCN.wav' '/var/www/vbox/testlink'

gave me the following output;

lrwxrwxrwx 1 root root      25 2013-02-20 14:18 testlink -> /var/lib/ast/music/50/CCN.wav
Avatar of aej1973

ASKER

sorry, actually the command I ran was with sudo, if not it does not work;

sudo ln -s '/var/lib/ast/music/50/CCN.wav' '/var/www/vbox/testlink'
If you are intending to run php from the command line, you could use sudo.  However, this isn't a solution if you intend to let non-root users run the command.  This means, if you intend to have Apache run the script, sudo isn't a solution.

Since it works with sudo and doesn't work otherwise, that indicates a problem with permissions.  It might also be SELinux stopping you depending on your distribution of Linux and if SELinux is on.

You can test if it's SELinux by
# setenforce 0

and then trying your script.

If it's SELinux, you probably don't want to leave it turned off
# setenforce 1
to turn it back on

You can then try
#chcon -R -t public_content_rw_t /var/lib/ast/music/50

If it's not SELinux then you might still have a regular permissions problem while will need to be tracked down.  First, at a $ can you change-directory to both the source and destination directories?
Hi,
I requested that a moderator add this question to the Linux zone as well so we can get more eyes on it.  That and I have to go teach and won't be able to check back until late tonight (EST).  I don't want you to have to wait that long for an answer if I haven't given you enough of an answer already.
Hugh
Avatar of aej1973

ASKER

Thank you for your help Huge, I appreciate it. The other option I am looking at is to create an alias on my apache server.

A
Avatar of Gary
Does Apache have permissions on the folder/file?
What user is Apache running as?
Avatar of aej1973

ASKER

apache is running as www-date
Assume you mean www-data
Does www-data have permissions on /var/lib/ast/music/50?
It would need pemissions from var upto the 50 folder.
Avatar of aej1973

ASKER

It looks like only folder 50 and the files in that folder have user-group as www-data. So what are my options? I am concerned that if I change the user-group for the var and lib folder I will run into some problems. Thank you for the help.

A
Why are they in the folder that they are in?
Avatar of aej1973

ASKER

no particular reason, it is in the root folder. I can go ahead and test it out.
ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of aej1973

ASKER

Gary, I made those chages; change the var,lib,ast,moh folder to have user and group as www-data but I am still not able to create the link. My code is as follows;
<?php
$target = "/var/lib/ast/music/50/";
$filename = "CCN.wav";
$structure = $target.$filename;
$src="/var/www/vbox/link123";
echo "symlink (" . $structure . ", " . $src . ")" . PHP_EOL;
symlink($structure,$src);
echo readlink($src);
?>

Open in new window

Avatar of aej1973

ASKER

you are right it may be the right thing to do, I will need to check a few things before I do that.
Are you creating the symlink from a shell/command prompt.
Check symlinks are enabled in httpd.conf (Options FollowSymLinks)
Addendum to that..
Where are you running that script from? A browser or command line?
You shouldn't be getting root as the owner as www-data will not be have access to it.
lrwxrwxrwx 1 root root      25 2013-02-20 14:18 testlink -> /var/lib/ast/music/50/CCN.wav
Avatar of aej1973

ASKER

I  am running it from a browser. If I run it from a command line I  have not problem, the link gets created.
Have you tried running the script as sudo -u apache   (or whatever it's username is on your system)?

Also, I see Gary made  good comment that it would be easier if all this was in the web directory.  Is there some reason it can't be?  If nothing else, could you make a symbolic link from the web directory to outside of the web directory?  That is link directories by hand instead of trying to link files as you go?
Avatar of aej1973

ASKER

Gary/Huge, thank you for you time/suggestions. I moved the folder to the web directory and it looks good now, thank you. I am still curious to know how to get the symlink to work, as I may use it some other time, but for now my issue is take care of, thank you.

A
Glad you solved it.  

If you haven't tried the link as user apache, I'd try it.  You shouldn't be able to login to apache or su apache but you might be able to sudo -u apache and run the script to see if it works for fails.
Avatar of aej1973

ASKER

Will give that a try, thank you. Appreciate all your suggestions.