Jump to content

Renaming Image With Md5


MDCode

Recommended Posts

I have an image upload page and I want to rename the file using md5. I've run into some problems and I'm not sure how to solve it. I'm successfully renaming the file but I am not able to add the extension (i.e. .jpg) no matter what I do >:(

 

if(!$_SESSION['username']) {
echo "You must be logged in.";
} else {

// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
include('connection.php');

if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
{



$max_upload_width = "500";
$max_upload_height = "250";


// if uploaded image was JPG/JPEG
if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){
$image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
}
// if uploaded image was GIF
if($_FILES["image_upload_box"]["type"] == "image/gif"){
$image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
}
// BMP doesn't seem to be supported so remove it form above image type test (reject bmps)
// if uploaded image was BMP
if($_FILES["image_upload_box"]["type"] == "image/bmp"){
$image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
}
// if uploaded image was PNG
if($_FILES["image_upload_box"]["type"] == "image/x-png"){
$image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
}
if(file_exists("Upload/image_files/".$_FILES["image_upload_box"]["name"])) {
header("Location: upload.php?upload_message=The+requested+file+already+exists,+Please+rename+the+file+and+try+again.&upload_message_type=error");
exit;
} else {

// RENAME FILE
$new_file = md5($_FILES["image_upload_box"]["name"]);
$remote_file = "Upload/image_files/".$new_file;
imagejpeg($image_source,$remote_file,100);
chmod($remote_file,0644);




// get width and height of original image
list($image_width, $image_height) = getimagesize($remote_file);

if($image_width>$max_upload_width || $image_height >$max_upload_height){
$proportions = $image_width/$image_height;

if($image_width>$image_height){
$new_width = $max_upload_width;
$new_height = round($max_upload_width/$proportions);
}
else{
$new_height = $max_upload_height;
$new_width = round($max_upload_height*$proportions);
}

$new_image = imagecreatetruecolor($new_width , $new_height);
$image_source = imagecreatefromjpeg($remote_file);

imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
imagejpeg($new_image,$remote_file,100);

imagedestroy($new_image);
}

$image_link = $_FILES["image_upload_box"]["name"];

$path = "--deleted website name--/Upload/image_files/$image_link";
$sql = "UPDATE `User_Profiles` SET `avatar` = '$path' WHERE `username` = '$username'";
$result = mysql_query($sql,$connection) or die("Error connecting to the database.");


imagedestroy($image_source);


header("Location: upload.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
exit;
}
}
else{
header("Location: upload.php?upload_message=Error:+Please+make+sure+the+file+is+jpg,+gif+or+png+and+that+it+is+smaller+than+4MB&upload_message_type=error");
exit;
}
}
?>

 

Sorry pasting removed all organization...any tips would be greatly appreciated! :)

Link to comment
Share on other sites

Was it really necessary to post the entire script rather than just the relevant sections? It's your prerogative, but many people are turned off by people that do that and are less likely to respond - I almost didn't

 

Anyway, you are always creating a jpg image so, just add .jpg to the end of the md5() name

$new_file = md5($_FILES["image_upload_box"]["name"]) . '.jpg';

 

Also, note that using an MD5() hash on the name will not prevent problems if users upload files with the same names. Since you can't reverse the hash into the name anyway, the value used to create the hash is not important. You could append a timestamp and/or random number to the value being hashed to prevent issues with duplicates.

Edited by Psycho
Link to comment
Share on other sites

Errors and other issues resulting in a solution may occur by other sections of code based on past experiences. And that's not all of it; just the uploading part. As for names I just want them different from the original at this point. There shouldn't been an issue with duplicate names in the nature it's being used. But ty for your response lol I feel dumb now :)

Edited by ExtremeGaming
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.