UPLOAD_DIR plus subdirectories

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
ack123
Forum Newbie
Posts: 3
Joined: Thu Aug 28, 2008 3:06 pm

UPLOAD_DIR plus subdirectories

Post by ack123 »

Hi all, I am trying to upload images based on certain subdirectory that is chosen from a dropdown menu. What I am attempting to do is somehow manage the images being uploaded to the UPLOAD_DIR by allowing the user to choose a sub directory. For some reason I can't figure out how to pass the variable to the move_uploaded_file. Here is my code:

define('UPLOAD_DIR', '/home/user1/php/images/');
//define('UPLOAD_DIR', '/home/user1/php/images/'.$_POST[dept]);
//define(UPLOAD_DIR, /home/user1/php/images/.$_POST['dept']);
mkdir (UPLOAD_DIR/$dept);

// move the file to the upload folder and rename it
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.'/'.$dept.'/'.$file);


mysql> select * from images;
+----------+---------------+----------------------+-----------------------+
| image_id | dept | image | caption |
+----------+---------------+----------------------+-----------------------+
| 1 | aunt | aunt-01.jpg | This is the text area |
| 2 | aunt | aunt-02.jpg | This is the text area |
| 10 | uncle | uncle-01.jpg | Text area |
| 11 | cousin | cousin-01.jpg | My cousin from NY |
+----------+---------------+----------------------+-----------------------+
4 rows in set (0.00 sec)

It appears the form information is passed successfully to the database, however I want to upload the images to a local folder and only use the database for informational purposes.

What I'm hoping to accomplish is to start with the UPLOAD_DIR - /home/user1/php/images/ and then be able to send a new image to a specific sub folder. I was wondering if anyone might be able to let me know if this is possible, or if there is a better solution. Any thoughts or suggestions are greatly appreciated.

Thank you,
-ack
ack123
Forum Newbie
Posts: 3
Joined: Thu Aug 28, 2008 3:06 pm

Re: UPLOAD_DIR plus subdirectories

Post by ack123 »

Doing a global search and replace of $dept to $deptname did the trick (I'm sure there was a typo somewhere)
$deptname= $_POST['dept'];
$success = move_uploaded_file($_FILES['image']['tmp_name'], UPLOAD_DIR.$deptname.'/'.$file);
Post Reply