Page 1 of 1
A "My Uploads" page?
Posted: Sat Sep 16, 2006 1:11 pm
by JustinMs66
hey so i have a working upload script, as well as a working user registration/login thing using mysql, but what i want to do now is have a "my uploads" page. which i believe i know kinda how to do. this is what i want:
i also have a working "show file contents in directory" script, and i want to tweak it so that the directory will change depending on what user is looking at their upload page. because i am going to have a different upload directory per user. so like yea, how would i get it to do that?
btw, here is my show directory script:
Code:
Code: Select all
<?php
//tells which directory to open. this is probubly where the tweaked code will go, i dunno tho.
if ($handle = opendir('.')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && !is_dir($file)) {
// shows the file name, as well as a link to the file:
echo "<a href='" . $file . "'>" . $file . "</a><br/>";
}
}
closedir($handle);
}
?>
Posted: Sat Sep 16, 2006 1:17 pm
by Luke
How is your user login system set up? You need to store the user's id in a session... then depending on what user id is in the session, load that particular user's directory.
Posted: Sat Sep 16, 2006 1:36 pm
by Todd_Z
You should have something like the following schema for your user database:
username
email
password
id
When a user logs in, the id of the session could be stored in $_SESSION['id']. Then, when you upload a file, you could append "{$_SESSION['id']}_" to the filename before saving it on the server. When you view the files, you can either show all the files, or just the files that start with the proper session id, for a my uploads page. This way, you have all the uploaded files in the directory, but still can keep track of which files belong to which user. When you output the filename on the webpage, use a simple regular expression: '#^(\d+)_(.*)$#' to determine the user id and the original filename.
Posted: Sat Sep 16, 2006 1:40 pm
by Luke
I prefer to just make a directory for each user
Code: Select all
-Filebin
-usr1
filename.jpg
image.gif
something.php
+usr2
+usr3
-usr4
somefile.jpg
someotherfile.gif
That way I don't need to worry about preg matching or any of that crapola
Posted: Sat Sep 16, 2006 2:27 pm
by Todd_Z
That way could cause one of two problems:
The amount of user directories gets so big that the system gets slowed down [this should happen around 1000 users i believe] ~ Rather Likely
A single user account has so many files that it slows down the system ~ Not Likely
How i deal with this, is I store all the files in a table, then depending on the id of the entry, i put it into a directory, For example,
id 1 = AA
id 2 = AB
id 3 = AC
and so on,
so at the end of the day, you have 26*26 folders, and the files are split up equally within those folders.
Posted: Sat Sep 16, 2006 2:51 pm
by JustinMs66
k, i'm gunna try Todd_Z, method, but i have a question. i used this:
Code: Select all
<?php
//this is the new code, making it go to the username folder:
$username = "uploads/users/" . $_SESSION['Uname'] . "/";
//normal code, but replaced "." with $username, to get it to go to that folder
if ($handle = opendir($username)) {
//end of new code, beginning of normal showing directory code:
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != "upload.php" && $file != "index.html" && !is_dir($file)) {
echo "<a href='" . $file . "'>" . $file . "</a><br/>";
}
}
closedir($handle);
}
?>
but it outputs this:
username1
username2
and then has a link to
http://www.mywebsite.com/username1
insted of showing the files in the
http://www.mywebsite.com/uploads/users/username1/ directory.
howdo i fix this?
Posted: Sat Sep 16, 2006 2:58 pm
by Todd_Z
Code: Select all
$base = "/home/www/htdocs/";
$append = "uploads/users/{$_SESSION['Uname']}/";
$files = scandir( $base.$append );
foreach ( $files as $file ) {
if ( ! preg_match( '#^\.#', $file ) ) { // Matches if the file is a hidden file or directory pointer
echo "<a href=\"{$append}{$file}\">{$file}</a><br />";
}
}
That code is untested, but you should be able to get the idea.
Posted: Sat Sep 16, 2006 3:31 pm
by JustinMs66
wait but thats a bit complicated, and i don't need it to test if it's hidden or whatever, i just need that 1 or 2 lines of code that correctly sets the directory to the session ID
Posted: Sat Sep 16, 2006 3:48 pm
by JustinMs66
o wait nvm i did it, i just needed to include a file and it worked
also, how would i make it so that when you register an account, it auto-creates a folder that is your username in the "uploads/users/" directory?
here is my registration code:
http://csscobalt.com/uploads/register.txt
Posted: Sat Sep 16, 2006 4:02 pm
by Todd_Z
mkdir()
Posted: Sat Sep 16, 2006 4:18 pm
by JustinMs66
kk. but on the "My Uploads" page, i wana have a like X image next to each file so that they can delete their uploads. so like yea, what would i add to this code to make that happen?
Code: Select all
<?php
include("inc2.php");
$username = "uploads/users/" . $_SESSION['Uname'] . "/";
if ($handle = opendir($username)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file != "index.php" && $file != "upload.php" && $file != "index.html" && !is_dir($file)) {
echo "<a href='/uploads/users/" . $_SESSION['Uname'] . "/" . $file . "'>" . $file . "</a><br/>";
}
}
closedir($handle);
}
?>
Posted: Sat Sep 16, 2006 5:05 pm
by Todd_Z
Either have an action with that file, for example, you could append
&remove=filename.txt
to the current url then have an if statement in your code to remove the file using if ( array_key_exists('remove',$_GET) ) { }
Or, have a link to a remove.php?filename=filename.txt script that will remove the file using the unlink() command
Posted: Sat Sep 16, 2006 5:23 pm
by JustinMs66
o. ok, so unlink() is like rmdir() except for files?
and would it be something like this, if it were to JUST output the delete part (no image, just text)?
Code: Select all
echo "<a href='remove.php?filename=" . $file . ">remove_file</a><br/>";
and then the remove.php would be somethinglike:
?
Posted: Sat Sep 16, 2006 5:29 pm
by Todd_Z
yep.
Make sure that you validate the input before removing the file. What if the variable $file stores "../../index.php"
BAM you're site is down