The online racing simulator
PHP: Getting files from specific directory..
I'm trying to make an upload / download system to my sites..

I already got the upload working, the files goes to ..upload/ directory but now I'm trying to make a php code that could download the files from that directory.. help? I've googled for 5hours now so don't tell me to google..

So far I've only managed to show the file names in the list..

<?php

if ($handle = opendir('upload/')) {
echo "Directory handle: $handle\n";
echo "Files:\n";

closedir($handle);
}
?>

Not quite sure I understand what you want exactly, but perhaps you mean having a script output file contents? You can simply use readfile for that, or fopen/fread/fclose and echo, however you'll have to deal with sending the correct headers in both cases
I'm sure there're more ways but I'm a little rusty in PHP

If your script is located in the root dir (or you're rewriting the URL to look like that), say for example "http://mydomain.tld/download.php?fid=1234", the script would translate the file ID into a valid filename (by looking it up in your DB), read the file from wherever it is located and pass it through without the need to redirect the client to the actual location.
What I mean is, I need to get FTP like list of the files that are uploaded to the server, and you can click to download the files..
I'm a total noob with PHP
Oh, in that case something simple like

<?php 
$files 
= array();
$dir opendir('.');
while(
$file readdir($dir))
{
    if(
$file != '.' && $file != '..' && !is_dir($file))
    {
        
$files[] = $file;
    }
}
sort($files);
foreach(
$files as $file)
{
    echo 
'<a href="' $file '">' $file '</a><br />';
}
?>

will do.


Give me this..

E: I've tried those notanillusion.. I suck with php so can't get them working... Can someone do the code for me?
Attached images
Untitled-1.jpg
Missing php start and end tag perhaps?

<?php 
php
(...)
?>

€: There you go

<?php 
$files 
scandir('.');
foreach(
$files as $file)
{
    if(!
is_dir($file))
        echo 
'<a href="' $file '">' $file '</a><br />';
}
?>

Quote from morpha :Missing php start and end tag perhaps?

<?php 
php
(...)
?>


yea lol sorry now it's working ty
blah it wont download the file it's like it doesn't even exist, it find it but when i click it, it goes to some random site

with this code

<?php
$files = array();
$dir = opendir('upload');
while($file = readdir($dir))
{
if($file != '.' && $file != '..' && !is_dir($file))
{
$files[] = $file;
}
}
sort($files);
foreach($files as $file)
{
echo '<a href="' . $file . '">' . $file . '</a><br />';
}
?>

Same is with both codes...
are you using some kind of mod_rewrite based cms?
or have you written the complete website on your own?
try to copy the link and see how it looks like. or even better, check the generated source code!
Quote from ORION :are you using some kind of mod_rewrite based cms?
or have you written the complete website on your own?
try to copy the link and see how it looks like. or even better, check the generated source code!

umm.. what?
It goes to that link when you go to some nonexisting site.. I've uploaded my sites to 110mb..
Try

<?php 
$files 
scandir('/upload');
foreach(
$files as $file)
{
    if(!
is_dir($file))
        echo 
'<a href="http://yourdomain.tld/upload/' $file '">' $file '</a><br />';
}
 
?>

Ok now it ignored the link but doesn't download anything..

<?
$files = scandir('upload/');
foreach($files as $file)
{
if(!is_dir($file))
echo '<a href="http://*****.110mb.com/download.php/' . $file . '">' . $file . '</a><br />';
}
?>

with this code.. Maybe I should put the "upload" directory to that a href=...?
OK works wow 6hours of trying, ty mates.

<?
$files = scandir('upload/');
foreach($files as $file)
{
if(!is_dir($file))
echo '<a href="upload/' . $file . '">' . $file . '</a><br />';
}
?>

Quote from morpha :Oh, in that case something simple like

<?php 
$files 
= array();
$dir opendir('.');
while(
$file readdir($dir))
{
    if(
$file != '.' && $file != '..' && !is_dir($file))
    {
        
$files[] = $file;
    }
}
sort($files);
foreach(
$files as $file)
{
    echo 
'<a href="' $file '">' $file '</a><br />';
}
?>

will do.

Double Loop Indeed.


<?php 
$path 
'.'# Must not have a forward slash at the end.
while($file readdir($path)) {
    if(!
is_dir($path '/' $file)) {
        echo 
'<a href="' $file '">' $file '</a><br />'
    
}
}
?>

Quote from NotAnIllusion :http://fi.php.net/manual/en/function.scandir.php

Interesting, I was wondering why I had not used this in some of my programs, then I found out that this is a PHP5 only function. You can emulate this function in PHP4 as shown in the bottom of the documentation.


<?php 
function scandir($directory$sorting_order 0) {
    while (
false !== ($filename readdir(opendir($directory)))) {
        
$files[] = $filename;
    }
    if (
$sorting_order == 0)
        
sort($files);
    else
        
rsort($files);
    return 
$files;
}
?>

Quote from Tomba(FIN) :OK works wow 6hours of trying, ty mates.

<?php
$files = scandir('upload/');
foreach($files as $file) {
if(!is_dir($file))
echo '<a href="upload/' . $file . '">' . $file . '</a><br />';
}
?> [/php]


Cool, posted final code too, very nice of you!

FGED GREDG RDFGDR GSFDG