Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- FILE 1 : index.php
- <html>
- <body>
- <form action="upload.php" enctype="multipart/form-data" method="post">
- <input name="file" size="35" type="file"/>
- <input name="submit" type="submit" value="Upload"/>
- </form>
- </body>
- </html>
- FILE 2 : upload.php
- <?php
- require_once "upload.class.php";
- if(isset($_FILES['file'])) {
- $uploader = new uploader($_FILES['file']);
- $uploader->upload();
- $ok = $uploader->getInfo();
- if(!empty($ok))
- {
- $photo = $uploader->dir . $uploader->file['name'];
- echo $uploader->getInfo();
- echo "<br/>";
- }
- else
- {
- echo $uploader->getError();
- echo "<br/>";
- }
- }
- ?>
- FILE 3 : upload.class.php
- <?php
- class uploader{
- private $type = array("jpg","jpeg","gif","png"),$width = 240,$height = 240,$info = '',$error='';
- function __construct($file,$dir="./"){
- $this->file = $file;
- $this->dir = $dir;
- }
- /**
- * Resize and upload an image.
- * @return String fileName or False on error
- */
- public function upload(){
- $ext = explode(".",$this->file['name']);
- $ext = strtolower(end($ext));
- if(file_exists($this->dir.$this->file['name'])){
- $this->error .= '<b>Error: file already exists.</b>';
- return false;
- }
- if (!in_array($ext,$this->type)){
- $this->error .= '<b>Error: file format not supported.</b>';
- return false;
- }
- // Get image size
- list($imwidth,$imheight) = getimagesize($this->file['tmp_name']);
- $hx = (100 / ($imwidth / $this->width)) * .01;
- $hx = round ($imheight * $hx);
- if ($hx < $this->height) {
- $this->height = (100 / ($imwidth / $this->width)) * .01;
- $this->height = round ($imheight * $this->height);
- } else {
- $this->width = (100 / ($imheight / $this->height)) * .01;
- $this->width = round ($imwidth * $this->width);
- }
- // Create a new true color image
- $image = imagecreatetruecolor($this->width, $this->height);
- // Create a new image from file
- if($ext == "jpg" || $ext == "jpeg") {
- $im = imagecreatefromjpeg ($this->file['tmp_name']);
- } else if($ext == "gif") {
- $im = imagecreatefromgif ($this->file['tmp_name']);
- } else if($ext == "png") {
- $im = imagecreatefrompng ($this->file['tmp_name']);
- }
- // Copy and resize part of an image with resampling
- if(imagecopyresampled($image, $im, 0, 0, 0, 0, $this->width, $this->height, $imwidth, $imheight)){
- $this->info .= '<b>Image Uploaded Successfully!</b>';
- }
- // Output image
- if($ext == "jpg" || $ext == "jpeg") {
- imagejpeg($image, $this->dir.$this->file['name'], 100);
- } else if($ext == "gif") {
- imagegif ($image, $this->dir.$this->file['name']);
- } else if($ext == "png") {
- imagepng ($image, $this->dir.$this->file['name'], 0);
- }
- // Destroy an image
- imagedestroy($im);
- return $im;
- }
- /**
- * Get uploading information.
- */
- public function getInfo(){
- return $this->info;
- }
- /**
- * Get uploading error.
- */
- public function getError(){
- return $this->error;
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment