Picture upload with new folder

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
bazsika
Forum Newbie
Posts: 1
Joined: Fri Jan 15, 2010 9:37 am

Picture upload with new folder

Post by bazsika »

Dear Friends...

I am looking for some assistance...

My case is this. I want to enable users to upload pictures to my site. I have got the picture uploading script with thumbnail creator. Very simple but useful script. What i need is that, i want to create a specific folder for each user to upload their pictures. Then the name of that folder should be generated by a random character generator, and that path should be securely stored in the database. Then the picture uploading is coming.

1. Random character generation (at least 32 characters, numbers and letters mixed)
2. Provide this character line as name for the new folder
3. Store this path securely in database
4. Upload the picture

Here is the upload script. It is working very nicely, but please recommend some security improvements if there is any.

Moreover, i want to maximize the number of images that can be uploaded. I want max. 5, then as additional option they can subscribe for more option, just like unlimited pictures. So i will need to include somehow this limitation option as well.

In the thumbnail creation, the whole picture is resized into a small thumbnail, but i wish to include a part of the real picture. Please recommend...

It would be also great, if i could include a processing bar, just to show the stage of uploading.

All opinion, suggestion are welcome, please do recommend your ideas.

Have a great day,

Bazsika

<?php

$upload_image_limit = 5; // How many images you want to upload at once?
$upload_dir = ""; // default script location, use relative or absolute path
$enable_thumbnails = 1 ; // set 0 to disable thumbnail creation


function make_thumbnails($updir, $img){

$thumbnail_width = 100;
$thumbnail_height = 100;
$thumb_preword = "thumb-";

$arr_image_details = GetImageSize("$updir"."$img");
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);



if($arr_image_details[2]==1) { $imgt = "ImageGIF"; $imgcreatefrom = "ImageCreateFromGIF"; }
if($arr_image_details[2]==2) { $imgt = "ImageJPEG"; $imgcreatefrom = "ImageCreateFromJPEG"; }
if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG"; }


if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);
$imgt($new_image,"$updir"."$thumb_preword"."$img");
}

}

################################# UPLOAD IMAGES

foreach($_FILES as $k => $v){

$img_type = "";

### $htmo .= "$k => $v<hr />"; ### print_r($_FILES);

if( !$_FILES[$k]['error'] && preg_match("#^image/#i", $_FILES[$k]['type']) && $_FILES[$k]['size'] < 1000000){

$img_type = ($_FILES[$k]['type'] == "image/jpeg") ? ".jpg" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/gif") ? ".gif" : $img_type ;
$img_type = ($_FILES[$k]['type'] == "image/png") ? ".png" : $img_type ;

$img_rname = $_FILES[$k]['name'];
$img_path = $upload_dir.$img_rname;

copy( $_FILES[$k]['tmp_name'], $img_path );
if($enable_thumbnails) make_thumbnails($upload_dir, $img_rname);
$feedback .= "Image and thumbnail created $img_rname<br />";

}
}

############################### HTML FORM
while($i++ < $upload_image_limit){
$form_img .= '<label>Image '.$i.': </label> <input type="file" name="uplimg'.$i.'"><br />';
}

$htmo .= '
<p>'.$feedback.'</p>
<form method="post" enctype="multipart/form-data">
'.$form_img.' <br />
<input type="submit" value="Upload Images!" style="margin-left: 50px;" />
</form>
';

echo $htmo;
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: Picture upload with new folder

Post by Jonah Bron »

Why do you need folders? I believe they should be organized in a database (not the files themselves, just the paths).
thinsoldier
Forum Contributor
Posts: 367
Joined: Fri Jul 20, 2007 11:29 am
Contact:

Re: Picture upload with new folder

Post by thinsoldier »

Some people just like folders. There's nothing wrong with that. I especially love folders when I decide to move a site to a new host. Nothing more annoying to me than starting a download via ftp and having it die on the 3,286th file. I prefer folders with a few scores of file each.

Bazsika, most of what you want to do has already been written in various file uploading and image resizing classes. You'd be better off using one of those. My coworker and I wrote one ages ago and since then I've refactored and updated it a dozen times and it's still not as good as eZ Components image conversion classes.

http://www.ezcomponents.org/docs/api/la ... ation.html
http://www.ezcomponents.org/docs/api/la ... ilter.html
http://www.ezcomponents.org/docs/api/la ... lters.html
Warning: I have no idea what I'm talking about.
Post Reply