Showing the uploaded Files. index.php in Uploads directory.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Showing the uploaded Files. index.php in Uploads directory.

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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);
}
?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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 :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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. =]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

I forgot to escape the quotes. I updated the code above to fix that error. =]
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

k, i fixed it just b4 reading it ur post :P. 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?
Last edited by JustinMs66 on Mon Sep 11, 2006 11:44 pm, edited 1 time in total.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post 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?
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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); 
} 
?>
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
JustinMs66
Forum Contributor
Posts: 127
Joined: Sun Sep 03, 2006 4:18 pm

Post by JustinMs66 »

that worked, thank you for your help. here take a look ;)

http://www.csscobalt.com/uploads/
Post Reply