Jump to content

File Uploading


phpnoob

Recommended Posts

HelloI have two problem :)I make a simple File upload, and it works perfectly :)First problem: i cant figure where can i put the md5, i want to rename the file name with md5

<?if($_SESSION['login'] == true){echo '<form action="galeria.php" method="POST" enctype="multipart/form-data"><input type="file" name="file"><p>Kategia:<p><input type="text" name="Kategoria" size="50"><p><input type="submit" name="submit" value="Feltöltés"></form>';} else{ echo 'You are not loged in!';} if ($_POST['submit']){if($_SESSION['login'] == true){$name = $_FILES["file"]["name"]; $type = $_FILES["file"]["type"];$size = $_FILES["file"]["size"];$temp = $_FILES["file"]["tmp_name"];$error = $_FILES["file"]["error"]; $maxwidth = 10000;$maxhigh = 10000;$isize=getimagesize($temp);list($width,$high) = $isize; if ((($type == "image/gif")|| ($type == "image/png")|| ($type == "image/jpg"))&& ($size <= 2000000) && ($width <= $maxwidth ) &&  ($high <= $maxhigh)){if ($error > 0){echo "Return Code: " . $error . "<br>";}else {if (file_exists("galeria/" . $name)){echo'The ' .$name . " file exist. ";}else{$resolution=$width.x.$high; $date=date('y-m-d  h:i'); $sql="INSERT INTO galeria (loginname, picturename, resolution, size, kategoria, time) VALUES ('$_SESSION["nick"]', '$name', '$resolution', '$size', '$_POST[Kategoria]', '$date')";if (!mysql_query($sql)){} else{move_uploaded_file($temp,"galeria/" . $name);echo 'The pictures uploaded!';}}}}else{echo "Not a valid file!";}}else{ echo 'You are not loged in!';}}}?>

Secord problemI find this php script on net.someone help me, to put this php script to my upload script

<? function resize($filename, $dest, $width, $height = "", $pictype = ""){$filename='frontend_images/headers/'.$filename;$dest='frontend_images/headers/'.$dest;$format = strtolower(substr(strrchr($filename,"."),1));switch($format){case 'gif' :$type ="gif";$img = imagecreatefromgif($filename);break;case 'png' :$type ="png";$img = imagecreatefrompng($filename);break;case 'jpg' :$type ="jpg";$img = imagecreatefromjpeg($filename);break;case 'jpeg' : $type ="jpg";$img = imagecreatefromjpeg($filename);break;default :die ("ERROR; UNSUPPORTED IMAGE TYPE, format:".$format);break;}list($org_width, $org_height) = getimagesize($filename);$xoffset = 0;$yoffset = 0;if($height == ""){$height=$org_height / ($org_width / $width);}if ($pictype == "thumb"){if ($org_width / $width > $org_height/ $height){$xtmp = $org_width;$xratio = 1-((($org_width/$org_height)-($width/$height))/2);$org_width = $org_width * $xratio;$xoffset = ($xtmp - $org_width)/2;}elseif ($org_height/ $height > $org_width / $width){$ytmp = $org_height;$yratio = 1-((($width/$height)-($org_width/$org_height))/2);$org_height = $org_height * $yratio;$yoffset = ($ytmp - $org_height)/2;}} else {$xtmp = $org_width/$width;$new_width = $width;$new_height = $org_height/$xtmp;if ($new_height > $height){$ytmp = $org_height/$height;$new_height = $height;$new_width = $org_width/$ytmp;}$width = round($new_width);$height = round($new_height);}$img_n=imagecreatetruecolor ($width, $height);imagecopyresampled($img_n, $img, 0, 0, $xoffset, $yoffset, $width, $height, $org_width,$org_height);if($type=="gif"){imagegif($img_n, $dest.'.'.$type);}elseif($type=="jpg"){imagejpeg($img_n, $dest.'.'.$type);}elseif($type=="png"){imagepng($img_n, $dest.'.'.$type);}elseif($type=="bmp"){imagewbmp($img_n, $dest.'.'.$type);}Return $dest.'.'.$type;} ?>

Thanks!

Link to comment
Share on other sites

Any chance of having that code on multiple lines? :)Anyway, for the first question, you can rename the file when you move it, on the move_uploaded_file($temp,"galeria/" . $name); line. You can apply the resize() function to the file after you move it.

Link to comment
Share on other sites

Any chance of having that code on multiple lines? :)Anyway, for the first question, you can rename the file when you move it, on the move_uploaded_file($temp,"galeria/" . $name); line. You can apply the resize() function to the file after you move it.
Resize? the first question, its not resize, rename it with md5
Link to comment
Share on other sites

Yeah, the first bit of the sentence was the answer to your first question, and the part after the comma was in reply to the second. :)For example, you could write the line as such:

move_uploaded_file($temp,"galeria/" . md5($name));

Link to comment
Share on other sites

Yeah, the first bit of the sentence was the answer to your first question, and the part after the comma was in reply to the second. :)For example, you could write the line as such:
move_uploaded_file($temp,"galeria/" . md5($name));

Its not working :) i know, because i try it yesterday.ups my failure :) im modifying a wrong file :)Ok its encrypted, but i have an another problem, the file lose the file type name, idea?
Link to comment
Share on other sites

You could get the type or extension (.jpg, .gif, etc) before you encode the name. Then add the extension after it's encoded. Something like:$ext = // get the extension$name = md5($name) . $ext;
done :)$ext=substr($name, strrpos($name, '.'));Only the second problem have, i want to thumbnail the image fileSomebody help me!
Link to comment
Share on other sites

The GD library can help you with that: http://www.php.net/manual/en/book.image.php . Building thumbnails on upload is the most efficient use of time, but building them on the fly is not unheard of.In any case, the result is vastly superior to "resizing" the image by altering the width/height attributes in your page.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...