Save file to user directory and link/href saved to mysql

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
cjmartinez2285
Forum Newbie
Posts: 2
Joined: Wed Jul 09, 2008 9:37 am

Save file to user directory and link/href saved to mysql

Post by cjmartinez2285 »

Hi All,

I have been trying to work on a project for a bit here and am having a little bit of trouble. What I am trying to do is have a registered user be able to upload multiple files to their own user directory through the front-end (logged in) of my site, and then be able to save the location of the file (href) to the Mysql db so that I can output it as a list to the user so that they can see their uploaded files and click to view them.

Any help would be greatly appreciated. I can also provide more information on my goals if I didn't explain too well.

Im actually trying to incorporate this into the modx cms.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Save file to user directory and link/href saved to mysql

Post by Benjamin »

What you want to accomplish is fairly simple.

Upload Process
1. Bob uploads a file.
1a. Validate the file.
2. Check if a folder exist in the uploads folder with Bob's user id
2a. If not, create it.
3. Save the file in Bob's folder
3a. CHMOD the file
4. Save the file name into a database table that contains user_id and file_name fields.

List Process
1. Query the uploads table for all files uploaded by a particular user_id
2. Output each item as "http://yoursite.com/" . "{$user_id}/{$file_name}"
cjmartinez2285
Forum Newbie
Posts: 2
Joined: Wed Jul 09, 2008 9:37 am

Re: Save file to user directory and link/href saved to mysql

Post by cjmartinez2285 »

Hi astions,

I understand the process it needs to go through but my code is not working, I have it pulling the array, no problem. The hard part is to somehow have it upload the file itself to the users created directory, and also, have it store the url (file location) into a database table which I will print to the viewer.

Any thoughts (specific), if you would like and allow me to, I can send you the source that I have so far.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Save file to user directory and link/href saved to mysql

Post by Benjamin »

There are good examples here: http://us2.php.net/manual/en/features.file-upload.php

Be sure to set the form enctype="multipart/form-data"

If you get stuck feel free to post your code here and either I or someone else will help you.
Post Reply