Chesse18's avatar

User uploading images into user created folder to display

Hi guys, new here. So, I am new to PHP, and even though I know a few things, I've been following some tutorials and things like that to help increase my knowledge. One of the things I recently did, was follow this tutorial: I have everything working, and I like how it works. What I want, is for the users who are logged in (eventually from their own profile page/etc) to be able to upload images to a folder that is generated automatically by their username (not one that I myself, the developer, made). Say I am a standard user on the site. My username is JohnDoe01, which is stored and saved with the method in the link I provided above (I believe in the $username variable). Let's say there is already a user profile function on the website. I go to my profile, and I want to upload images to my profile, and the profile page will call for those images to display on the just MY profile page. How it should work, is EITHER when the user registers, it makes a profile folder for that user's profile, (say: ".../.../users/$username/images" ) is placed in-- OR, it generated a new folder specifically for images, (say: ".../.../users/images/$username" ). I can then just create some code that calls on the images folder for $username and displays them on the user's profile page (which I already have a script for).

My question is, how can I use the login/registration system that was described in the link above, and do something like this? I've looked at a few tutorials/guides/etc, but they are all broken because their variable assignment is way different, or their login system is different, etc.

I hope I explained this all good enough.

P.S. I realized after I wrote this I couldn't insert links "on my first day"... so maybe we can still find a solution regardless. Thanks

0 likes
7 replies
Snapey's avatar

each user has an id, just create a folder with that name, in say storage/app/public/userimages/

You can create the folder if it does not exist the first time the user uploads an image

1 like
Chesse18's avatar

@Snapey Alright, what would the best method be for that? Just create a variable equal to the user's ID in the database and then using that variable to create a folder for it?

Apologies for any ignorance, I'm new and still trying to learn :p

Chesse18's avatar

@Snapey I think I made it work. I did almost what I had just asked, assigning a variable equal to what my current login system defines as the user's profile name, and stores it. It then checks to see if that folder already exists, and if it doesn't, it will create it. Now I can probably move on to having the user be able to upload their images and have it save to their profile's folder (using the variable I made, $userfolder ) and then later, use a script I already have to display any/all images from that folder to their profile.

If you're curious, this is what I have so far, and it worked, at least so far in terms of making the profile based off the user who is logged in:

$userfolder = $_SESSION["username"];

// Define path where file will be uploaded to
//   User ID is set as directory name
$folderPath = "profiles/users/$userfolder";

// Check to see if directory already exists
$exist = is_dir($folderPath);

// If directory doesn't exist, create directory
if(!$exist) {
mkdir("$folderPath");
chmod("$folderPath", 0755);
}
else { echo "Folder already exists"; }
?>
jlrdw's avatar

@Chesse18 if these are private files for that user only, you need to implement authorization so no one else could possibly view that folder.

I wouldn't put them so they are publicly accessible.

1 like
Chesse18's avatar

@jlrdw They're going to go on the user's wall, so as long as another user can view the other user's profile, it would be public to them to view on said profile page. A much more complex example would be, say, Facebook. I can upload images to my wall/profile, and other people can view my profile and see those pictures

Otherwise how could I do that?

Snapey's avatar

@Chesse18 there are lots of things in the Laravel framework that will help you with this task. You would be well to step back from this problem and studying the Laravel from scratch series on this site.

Are you not using Laravel's built in authentication?

You could use the Storage helpers so that you can easily switch between different storage providers and locations

tip: make sure username cannot contain spaces, full stops and slashes or the user can put their files in any folder they like. Better to use something generated on your side (like their id) or a random string

1 like
Chesse18's avatar

@Snapey Alright. I'll take a look at that scratch series.

Currently, at least for the time being, I'm going to just hash the folder name. This is a huge project for me so it will definitely take some time to complete :)

Please or to participate in this conversation.