Create Hash Directory when uploading image file

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
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Create Hash Directory when uploading image file

Post by ninethousandfeet »

hello,
i am trying to create a hash directory for image files when they are uploaded by my users. my path is valid if i remove the '/' in the $filename string; however, that is my whole point. When a new file is uploaded I want it to go to: mysite.com/upload/post_id/sha1(post_id)/sha1(post_id)...

my current code produces this error:
Warning: move_uploaded_file(/home/me/domains/mysite.com/public_html/upload/0/da39a3ee5e6b4b0d3255bfef95601890afd80709/a3/5e) [function.move-uploaded-file]: failed to open stream: No such file or directory in /vz/home/me/domains/mysite.com/public_html/TEST.php on line 119

Code: Select all

 
$uploadDIR = '/home/me/domains/mysite.com/public_html/upload';
$file = $_FILES['image_data']['name'];
str_replace(' ', '_', '/', $file);
$fileTemp = $_FILES['image_data']['tmp_name'];
 
$random_file = hash(sha1, $_POST['post_id']);
$strip1 = substr($random_file, 4, 2);
$strip2 = substr($random_file, 8, 2);
$filename = "$random_file/$strip1/$strip2";
 
$max = number_format(MAX_FILE_SIZE/1024, 1).'KB';
// create an array of permitted MIME types
$permitted = array('image_data/gif', 'image_data/jpeg', 'image_data/pjpeg', 'image_data/png');
 
if (!$permitted) {
    $error[] = 'Sorry, your file type is not recognized.';
} else {
    $type == $_FILES['image_data']['type'];
}
// check that file is within the permitted size
if ($_FILES['image_data']['size'] > MAX_FILE_SIZE) {
    $error[] = 'Sorry, but your file is too large.';
}
if (!$error) {
if (!file_exists("$uploadDIR/$filename")) {
  move_uploaded_file($fileTemp, "$uploadDIR/$file/$filename");
}
 
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

Re: Create Hash Directory when uploading image file

Post by waylon999 »

I think you need to create the directory first using mkdir(), then move your file into it.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: Create Hash Directory when uploading image file

Post by ninethousandfeet »

i tried that, but i was given an error message that said i didn't have the authority to do that with my host... do you have to use mkdir() when creating a hash directory? i thought they were separate from each other?
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

Re: Create Hash Directory when uploading image file

Post by waylon999 »

Well, I'm not sure about hash directories, but I would assume that if you are putting an image in a new directory that you would have to create it first.

I seem to remember doing something similar to this a couple years back. When I get home tonight I can take a look and see if I still have it, and what the method was that I used.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: Create Hash Directory when uploading image file

Post by ninethousandfeet »

that would be great, thank you!
waylon999
Forum Commoner
Posts: 26
Joined: Mon Mar 23, 2009 5:29 pm

Re: Create Hash Directory when uploading image file

Post by waylon999 »

Ok, I checked my code for resizing and uploading images and I am using mkdir before I move the files. I can give you some of my code but I'm not sure that would help much since you got the error message of not being able to create dirs. Maybe you should ask your host what you can do.

Sorry I can't be more help.
ninethousandfeet
Forum Contributor
Posts: 130
Joined: Tue Mar 10, 2009 4:56 pm

Re: Create Hash Directory when uploading image file

Post by ninethousandfeet »

would you mind providing what you have? i messed around a bit and i was able to get the file to the directory, but the problem is that i'm not sending the file properly... i think there is something wrong with the way i setup my variables but i can't figure it out... anything you can provide, it would be a great help. i'll keep you posted if i come across the solution in the meantime.

thank you
Post Reply