Page 1 of 1

PHP File reference

Posted: Wed Feb 03, 2010 12:03 pm
by jo1234la
I have created an upload page that admin users can upload files to. I then created viewing page that end users can download files from. File types will be MS Excel files. This is working if I go directly to the directory URL I can see and link to the file and windows opens up the file and asigns EXCEL to open it up ....... all is good

What I need is a script of some sort that will display the file path or a link to the file in my viewing page. I need this to be automated in that for every file loaded the old file is deleted and the link is updated.

I am so stumped. Please help even if you can only show me the file <a href=""> side of things just need it to look in my directory for the file and show me a link.

Re: PHP File reference

Posted: Wed Feb 03, 2010 4:04 pm
by JNettles

Code: Select all

if ($handle = opendir('.')) 
{
    while (false !== ($file = readdir($handle))) 
    {
        if ($file != "." && $file != "..") 
        {
            echo "$file<br/>";
        }
    }
    closedir($handle);
}
For future reference, the PHP manual is a wonderful repository of information for tasks like this.

Re: PHP File reference

Posted: Wed Feb 03, 2010 5:05 pm
by jo1234la
Thank you very much I had something similar but yours works better however what I need is for the result "$file" to show as a hyper link to the actual file itself. I want to enable my end users to click on the link and open up that particular file (MS Excel File)

I have tried using <a href=" "></a> and it doesn't work.Without the <a href=""> statement it works fine but the result is not a hyperlink, it is just a string of text.

Do you have any other suggestions? Thanks again for your help.

Re: PHP File reference

Posted: Wed Feb 03, 2010 9:01 pm
by JNettles

Code: Select all

<a href="$file">$file</a>
$file holds each URL as it loops through the folder, so just adapt the $file for your link.

Re: PHP File reference

Posted: Thu Feb 04, 2010 12:46 am
by jo1234la
Wow that was awesome I almost have it. There is one snag. When I used your code this is what my code looks like:

<?php
if ($handle = opendir ('/update/upload1')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href=$file>$file</a></br>";

}
}
closedir($handle);
}
?>

The one snag is that the URL returns and completely skips over a directory so when you click on the link it does not make it to the file.

the returned URL: http://www.--------.com/update/testdoc.doc

The actual file URL is http://www.-------.com/update/upload1/testdoc.doc

Can you tell me where I am screwing up here? Thanks you have been of great help, and props on the great explanation.

Re: PHP File reference

Posted: Thu Feb 04, 2010 8:38 am
by JNettles
Specify your directory path in a variable so that you can reference it again later, since $handle is going to become a directory resource. So your scripts ends up looking like.....

Code: Select all

 
//place your path in $path
$path = '/update/upload1/';
if ($handle = opendir($path)) {
    while (false !== ($file = readdir($handle)))
    {
        if ($file != "." && $file != "..")
        {
                        //attach our path to the filename for our url
            $filelist .= '<a href="'.$path.$file.'">'.$file.'</a><br/>';
        }
    }
    closedir($handle);
}
 
 
//place this anywhere to display the list
echo $filelist;

Re: PHP File reference

Posted: Thu Feb 04, 2010 9:02 am
by jo1234la
YOU SOOOOOO FREAKIN ROCK!!!! Woo Hoo! Thank you so much I love this site. I doubt I know more than you anytime soon but if I could ever be of assistance please let me know. By trade I am a network guy and freelance in this by night. Thanks again man!!!