Page 1 of 1
Showing the uploaded Files. index.php in Uploads directory.
Posted: Mon Sep 11, 2006 10:27 pm
by JustinMs66
i have a upload script, and i wana have a "index.php" INSIDE the "uploads/" directory that shows the files, but in an organized manner, and has links on the files, To the files. here is my current upload script:
http://www.csscobalt.com/uploads/upload_php.txt
and what i was thinking, was that somehow, instead of listing the files in the Dir, every time something is uploaded, it simply adds the upload to the index.php file. but i don't even know where to begin.
any help?
Posted: Mon Sep 11, 2006 11:03 pm
by s.dot
If you're meaning a script that will read the contents of the uploads directory..
readdir() would be useful.
I think i read a bit too much into the question, but if you're wanting the index.php file to not read the directory, but still list contents, you will need to set a script that reads the directory and writes the file up on CRON.
Posted: Mon Sep 11, 2006 11:14 pm
by JustinMs66
well i don't want it to read the directory, but i do want it to list all/most of the files. and i want to do that by adding the file name to the index.php each time a upload is uploaded. i think that would be MUCH easier.
so how would i go about doin that?
Posted: Mon Sep 11, 2006 11:18 pm
by s.dot
=/ I'm a bit confused.
The reason i'm confused is because I *think* you're a bit confused on what you want to do (well, how you want to do it). By using this following piece of code on /uploads/index.php, you can list the entire contents of the directory.
Code: Select all
<?php
if ($handle = opendir('uploads')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file\n";
}
}
closedir($handle);
}
?>
Posted: Mon Sep 11, 2006 11:24 pm
by JustinMs66
your exactly correct, i am confused on how i want to do it. but let my try again:
here is the process:
1) user will upload a file with my working upload script
2) the script will then log the name, and send that name over to "index.php" in the "uploads/" directory, which will then add that name to the list.
kinda like what u had, but this way i can add like <br> and other html code so it's not all jumbled up

Posted: Mon Sep 11, 2006 11:27 pm
by s.dot
You can do that using the script I had. Modify it a bit..
Code: Select all
<?php
if ($handle = opendir('uploads')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "$file<br />";
}
}
closedir($handle);
}
?>
That will put line breaks in there. You can even link to the uploaded file.
Code: Select all
<?php
if ($handle = opendir('uploads')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href=\"$file\">$file</a><br />";
}
}
closedir($handle);
}
?>
Like that.
However if you wish to go about it by updating the file. Then you will need to read the directory on a cron script and have that script write to the index.php file (which is much more complicated than the codes above.. but can be done).
Hope that helps a bit. =]
Posted: Mon Sep 11, 2006 11:35 pm
by JustinMs66
o i totaly get what ur sayin now. ok that is AWESOME thnx man!!!
but... it gives this error message:
Parse error: syntax error, unexpected T_VARIABLE, expecting ',' or ';' in list_dir_v01.php on line 13
and like 13 is:
echo "<a href="$file">$file</a><br/>";
what should i do?
Posted: Mon Sep 11, 2006 11:39 pm
by s.dot
I forgot to escape the quotes. I updated the code above to fix that error. =]
Posted: Mon Sep 11, 2006 11:40 pm
by JustinMs66
k, i fixed it just b4 reading it ur post

. but i just did this:
Code: Select all
<?php
if ($handle = opendir('uploads')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
echo "<a href=" . $file . ">" . $file . "</a><br/>";
}
}
closedir($handle);
}
?>
and it worked. so wutev. but is there a better/worse way to do it?
and also, that code displays files that are in directories as well :/ and i don't want that. for example,
it's here:
/uploads/index.php
and it displays files even here:
/uploads/files/personal/etc/
and i JUST want it to display files in:
/uploads/
so how do i fix that?
Posted: Mon Sep 11, 2006 11:42 pm
by s.dot
That's pretty much the only way to do it, unless you want to store the filenames in a database as they're uploaded. Then query the database on index.php.
Posted: Mon Sep 11, 2006 11:45 pm
by JustinMs66
k, but new problem :/
that code displays files that are in directories as well :/ and i don't want that. for example,
it's here:
/uploads/index.php
and it displays files even here:
/uploads/files/personal/etc/
and i JUST want it to display files in:
/uploads/
so how do i fix that?
Posted: Mon Sep 11, 2006 11:46 pm
by s.dot
Code: Select all
<?php
if ($handle = opendir('uploads')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && !is_dir($file)) {
echo "<a href=" . $file . ">" . $file . "</a><br/>";
}
}
closedir($handle);
}
?>
Posted: Tue Sep 12, 2006 12:05 am
by JustinMs66
that worked, thank you for your help. here take a look
http://www.csscobalt.com/uploads/