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!
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?
<?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);
}
?>
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.
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.
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.
<?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);
}
?>
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
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?
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)?