help with link, PHP, apache, win2k

MaluMan

Member
Nov 23, 2002
183
0
0
I've been having some trouble with linking files outside of my root directory. I was wondering if anyone could help me out?

I'm running Apache 2.0.46, PHP 4.3.2, and win2k.

k, so my problem is that i have media files outside my "htdocs" folder that i want to link to from within my index.html which is located in my "htdocs" folder, is this possible? and if so what ways can i do this?

i tried using fopen() and fpassthru(), but i get something like:
failed to open stream: Permission denied in C:\..\htdocs\index.php on line 26

thanx for any input

maluman
 

MaluMan

Member
Nov 23, 2002
183
0
0
the alias seems to work, but then i also have different directories (close to 70 or so) and instead of making a bunch of aliases is there any other way to link to them??
 

darktubbly

Senior member
Aug 19, 2002
595
0
0
I'm assuming these 70 or so directories aren't in the same subdirectory, huh? The other option, as you suggested, would probably be the fpassthrough or fread. Both have excellent user-submitted examples posted on the PHP online manual. Regarding the access error, it's most likely a problem with your php.ini file or NTFS permissions. Do you have safe mode enabled or a base_dir option set?
 

MaluMan

Member
Nov 23, 2002
183
0
0
safe_mode is turned off and my open_basedir is set to empty, i've been trying both fread(), fopen() with no luck, same permission denied error.

maybe my code is wrong?? :

$dir = fopen("C:\Documents and Settings\user\My Documents\MP3s\", "r");
fpassthru($dir);
$handle = opendir("C:\Documents and Settings\user\My Documents\MP3s\");
while ($file = readdir($handle))
{
$files[] = $file;
}

foreach ($files as $item)
{
echo "<a href=\"{$dir}.{$item}\">{$item}</a>
";
}
closedir($handle);

within my MP3s folder i have a bunch of diff folders w/mp3s,,,, i'm just trying to link to them from my root dir w/o having to put everything into my root dir. with the code above i get an listing of the files and directories, but the links to each dont work.

i'm a noob to html/php n the such, so bare with me ;)

thanx
 

darktubbly

Senior member
Aug 19, 2002
595
0
0
Yeah, there are a few problems with the code...for one thing, while the href may point to C:\Documents and Settings\..., it only works if you're accessing it locally. Your best bet would still be that Apache Alias option if they're all in the same directory, or if you wanted something a little bit more advanced, check out kPlaylist. There are also several other PHP/MySQL based options. Post if you need any more help.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
Ok I noticed some more things so I'm going to critique this.

$dir = fopen("C:\Documents and Settings\user\My Documents\MP3s\", "r");
fpassthru($dir);

fopen opens files. http://php.net/fopen

Later on you try to use $dir as a directory prefix, so this is way wrong. I think you just want:

$dir = "C:\\Documents and Settings\\user\\My Documents\\MP3s\\";

Also note the double backslashes. MS OS's use backslashes for directory seperators, where backslashes have traditionally been used for escaping special characters, therefore, to stop php from interpreting your backslashes as something special, you need to double them.

$handle = opendir("C:\Documents and Settings\user\My Documents\MP3s\");
while ($file = readdir($handle))
{
$files[] = $file;
}

foreach ($files as $item)
{
echo "<a href=\"{$dir}.{$item}\">{$item}</a>
";
}
closedir($handle);
Ok first off, why do you put the files in a temporary array, only to loop back through them? I guess this could be important to the rest of the script though, assuming you have more code than you pasted. Loops with only one statement don't need braces either, they don't hurt, but they are visually cluttering IMO. Just MO though.

You are trying to concatenate with the period character here, which doesn't work inside of a string, just remove it. You also don't need the curly braces around the variable names since there's nothing there to conflict with their parsing.

Also, now that I think about it, you should take the trailing backslashes off of your directory name, and put them in between the variables themselves. I'm not sure if opendir will work correctly with trailing slashes, you're generally not supposed to have them.

Also, this whole scheme won't solve your problem anyways. You can't link to files outside of your webroot, otherwise people could just randomly follow links to any ol' file on your machine.
 

darktubbly

Senior member
Aug 19, 2002
595
0
0
Originally posted by: BingBongWongFooey
Also, this whole scheme won't solve your problem anyways. You can't link to files outside of your webroot, otherwise people could just randomly follow links to any ol' file on your machine.

You can send files outside the base directory, but it's rather insecure. MaluMan, you're best off using the built in Apache feature or some sort of PHP/MySQL backend.
 

darktubbly

Senior member
Aug 19, 2002
595
0
0
I'm pretty sure he is; since Apache can't link to files outside the basedir without an Alias tag, the only other way (that I know of) using PHP is to read the file contents and spew it to the output buffer.
 

Barnaby W. Füi

Elite Member
Aug 14, 2001
12,343
0
0
That's what he *should* do, that's not what he's doing currently.

echo "<a href=\"{$dir}.{$item}\">{$item}</a>";

That would just give:

<a href="C:\Documents and Settings\user\My Documents\MP3s\foo.mp3">foo.mp3</a>

Which obviously won't work.
 

MaluMan

Member
Nov 23, 2002
183
0
0
good stuff, thanks guys.... so this is what i ended up doing, its not complete yet but i think it'll work out, just a lil more work

i made an alias as:
Alias /mp3 "C:/Documents and Settings/user/My Documents/MP3s/"
<Directory "C:/Documents and Settings/user/My Documents/MP3s/">
Order allow,deny
Allow from all
</Directory>

so from my root, i can use http://root/mp3 and i get access to that folder (wit mp3s).... now i figure i'd write something else in the "/MP3s/" folder that goes thru all the diff folders n files...cuz i think i have access to the subdirectories in the alias n all files within also

i was gonna use a prog (Dynamic MP3 Lister) from the link darktubbly posted, but for that too the files needed to b in the root.

and about the <a href> file path, makes more sense when it was written out fully,,,,i'm not used to the windows folders thingy, this is the first time working on windows.... thanx BingBongWongFooey

thanks again for all the help, really appreciate it