Fileupload , php

i’am working on php fileupload and i’am trying to creat image displaying mean view the image before uploading .

can someone tell me whats worng with the code ?

[php]<!doctype html>

birdupload: upload gallery | home

browse:

<?php if(isset($_POST['upload'])){ $image_name = md5($_FILES['image']['name']); $image_type = $_FILES['image']['type']; $image_size = $_FILES['image']['size']; $image_tmp_name= $_FILES['image']['tmp_name']; @$desc = $_POST['desc']; $allowed=array("image/jpeg", "image/gif", "image/png"); if(!in_array($image_type,$allowed)) echo "only jpg, gif, and png files are allowed."; else{ move_uploaded_file($image_tmp_name,"uploads/$image_name"); echo "
"; echo "your image:"; } } ?>[/php]

so ?

Patience Danielson for it takes time to reply to questions, plus this forum hasn’t been very busy.

I’ll get back to you after I have looked at the script or someone in the meantime might answer your question.

[size=24pt]First of all STOP SUPPRESSING ERRORS by using @ in front of php functions or variables! [/size]

I don’t know why you are using md5 for uploading images either… ???

And until I get back to you or someone else has ring-in in the meantime the following is a class that I wrote and while you might not understand it totally it might give you an idea.
[php]<?php

namespace Library\ProcessImage;

use PDO;
use Library\Database\Database as DB;

class ProcessImage {

protected static $allowedExts = array("jpg", "jpeg", "gif", "png");
protected static $allowedTypes = array("image/gif", "image/jpeg", "image/png", "image/pjpeg");
protected $name;  // Image Name:
public $newName = \NULL;
protected $type;  // Image Type:
protected $extension;   // Image Extension:	
protected $error = \NULL; // Image Error:
protected $size;  // Image Size:
protected $tmpDir;
protected $tmpName;  // Image Temporary Name:
protected $preExt = '../public/assets/uploads/img-';
protected $unique = \NULL;
protected $myDate = \NULL;
public $username = \NULL;
public $file = \NULL;
public $status = \NULL;

/*
 * 
 */

public function __construct($file = \NULL, $username = "Strider") {

    $this->file = $file;
    $this->username = $username;
}


protected function setImageExt() {
    return pathinfo($this->file['name'], PATHINFO_EXTENSION);
}

/*
 * Searches the contents of a file for a PHP embed tag
 * The problem with this check is that file_get_contents() reads 
 * the entire file into memory and then searches it (large, slow).
 * Using fopen/fread might have better performance on large files.
 */
protected function file_contains_php() {
    $contents = file_get_contents($this->file['tmp_name']);
    $position = strpos($contents, '<?php');
    return $position !== false;
}

public function processImage() {
    $this->status = $this->file_contains_php();

    if ($this->status) {
        return $this->status; // Bad Image
    } else {
        $this->extension = $this->setImageExt(); // Set Extension if Image is valid:
        return $this->status; // Good Image
    }
}


public function checkFileType() {
    if (!in_array($this->file['type'], self::$allowedTypes)) {
        $this->status = TRUE; // Improper Image Type
    } else {
        $this->status = FALSE;
    }
    return $this->status;
}

public function checkFileExt() {
    if (!in_array($this->extension, self::$allowedExts)) {
        $this->status = TRUE; // Improper Image Extension:
    } else {
        $this->status = FALSE;
    }
    return $this->status;
}

public function checkFileSize() {
    $this->size = $this->file['size'];
    if ($this->size > 800000) {
        $this->status = TRUE; // Failed image size:
    }
}

/*
 * If image passes validation then name the file and move the image to assets/uploads
 */
protected function uniqueName() {
    $this->myDate = new \DateTime("NOW", new \DateTimeZone("America/Detroit"));
    return $this->username . $this->myDate->format("U") . ".";
}

protected function getTMPName() {
    return $this->file['tmp_name'];
}

public function saveIMG() {
    $this->unique = $this->uniqueName();
    $this->tmpName = $this->getTMPName();
    $this->newName = strtolower($this->preExt . $this->unique . $this->extension);
    if (!$this->file['error']) {
        move_uploaded_file($this->tmpName, $this->newName);
        return $this->newName;
    }
}

}[/php]

Sponsor our Newsletter | Privacy Policy | Terms of Service