PHP - Files from folder on a webpage
#1

Edit: Thanks guys (Kwarde) it is working now!

Hey guys,

I'm using the following PHP script:

Код:
<?php  
if ($handle = opendir('uploads')) {  
    while (false !== ($file = readdir($handle))) {   
        if ($file != "." && $file != "..") {   
            echo "<u><a href=\"$file\">$file</a></u><br />";   
        }   
    }  
    closedir($handle);   
}  
?>
This script will show the files from the directory called: 'uploads' on a webpage. But now I have a problem. When I click a file in the list, Internet says: Not found. But when I go to the path of the file (www.blahblah.com/upload/blahblah.jpg) Then it DOES work :S

Does anybody know how I can edit this script so it will download the file whem you click it?

Thanks!

-J
Reply
#2

Try:
Код:
echo "<u><a href=\"uploads/'.$file.'\">$file</a></u><br />";
Or:
Код:
echo "<u><a href=\"'.$file.'\">$file</a></u><br />";
Reply
#3

Quote:
Originally Posted by viKKmaN
Посмотреть сообщение
Try:
Код:
echo "<u><a href=\"uploads/'.$file.'\">$file</a></u><br />";
Or:
Код:
echo "<u><a href=\"'.$file.'\">$file</a></u><br />";
ah, so you mean:

Код:
<?php  
if ($handle = opendir('uploads')) {  
    while (false !== ($file = readdir($handle))) {   
        if ($file != "." && $file != "..") {   
            echo "<u><a href=\"'.$file.'\">$file</a></u><br />";  
        }   
    }  
    closedir($handle);   
}  
?>
hmmm, that doesn't work too, thanks anyway!

-J
Reply
#4

What about this... I know your problem is with parsing $file into the echo... Just can't figure out why...
Код:
<?php  
if ($handle = opendir('uploads')) {  
    while (false !== ($file = readdir($handle))) {   
        if ($file != "." && $file != "..") {   
            echo "<u><a href=".$file.">$file</a></u><br/>";  
        }   
    }  
    closedir($handle);   
}  
?>
Reply
#5

Quote:
Originally Posted by viKKmaN
Посмотреть сообщение
What about this... I know your problem is with parsing $file into the echo... Just can't figure out why...
Код:
<?php  
if ($handle = opendir('uploads')) {  
    while (false !== ($file = readdir($handle))) {   
        if ($file != "." && $file != "..") {   
            echo "<u><a href=".$file.">$file</a></u><br/>";  
        }   
    }  
    closedir($handle);   
}  
?>
nope... too bad it isn't possibble in HTML because that would be freakin simple :P

thanks!
Reply
#6

PHP код:
<?php
    $files 
"http://dl.foco.co/samp/sa-mp-0.3c-install.exe";
    echo 
"<u><a href=".$files.">$files</a></u><br/>"
?>
works fine for me
dunno what your problem is though it
must be a problem with your $files variable
Reply
#7

Quote:
Originally Posted by saiberfun
Посмотреть сообщение
PHP код:
<?php
    $files 
"http://dl.foco.co/samp/sa-mp-0.3c-install.exe";
    echo 
"<u><a href=".$files.">$files</a></u><br/>"
?>
works fine for me
dunno what your problem is though it
must be a problem with your $files variable
Thanks for your quote!

So should I add my http://www.blahblah.com/uploads/ line in this:
Код:
$files = "http://dl.foco.co/samp/sa-mp-0.3c-install.exe";
??
Thanks!
Reply
#8

Quote:
Originally Posted by Jantjuh
Посмотреть сообщение
Thanks for your quote!

So should I add my http://www.blahblah.com/uploads/ line in this:
Код:
$files = "http://dl.foco.co/samp/sa-mp-0.3c-install.exe";
Thanks!
no it was just a test if inserting variables in href works^^
Reply
#9

Quote:
Originally Posted by saiberfun
Посмотреть сообщение
no it was just a test if inserting variables in href works^^
Oooh lol
Reply
#10

Well, where do the links take you?

pawn Код:
<?php  
if ($handle = opendir('uploads')) {  
    while (false !== ($file = readdir($handle))) {  
        if ($file != "." && $file != "..") {  
            echo "<u><a href=\"uploads/$file\">$file</a></u><br />";  
        }  
    }  
    closedir($handle);  
}  
?>
Works as intended for me.

The problem with your original code is that you're pointing to the root directory of the file that contains this code, but the files you're linking to are actually contained in the uploads subdirectory. So you need to point in there, someone else actually posted this solution earlier, but you ignored it
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)