jakob jense - 2009-04-05 20:55:16
Hey guys.
Sorry for my bad english :)
I have a great upload-class, but how do i customize it so that the allowed file-types are jpeg, jpg, gif, png???
<?php
class uploader {
private $folder;
private $file;
private $name;
private $max_file_size;
private $allowed_file_types
private $file_type;
// Konstruktør
public function __construct(){
$this->max_file_size = 2097152; // = 2MB
}
// Hvilke filer der er tilladt
public function setAllowed_file_types($type){
$this->allowed_file_types[] = $type;
}
/**
* Methoden bestemmer destinations-mappen
* Methoden er obligatorisk
*
*
* Mappen oprettes, hvis den ikke findes.
*
* @param String(mappe-navn) $dir
*/
//Hvilken mappe den skal uploade til
public function setDir($dir){
$this->folder = $dir;
if(!is_dir($this->folder)) {
mkdir($this->folder);
}
//hvis bruger har undladet "/"
if (substr($this->folder, -1) != "/"){
$this->folder .= "/";
}
}
// sætter filnavnet
public function setFile($fil){
$this->file = $fil;
$this->name = $fil['name'];
$dot = strrpos($this->name, ".");
$extension = substr($this->name, $dot);
$this->file_type = strtolower(ltrim($extension, "."));
}
// medsendt argument til filnavn
public function setName($name){
$this->name = $name . "." . $this->file_type;
}
// tilføjer timestamp til filnavn
public function addTimeStamp(){
$this->name = time() . "_" . $this->name;
}
// sætter maxstørrelse på fil du kan uploade
public function setMax_file_size($bytes){
$this->max_file_size = $bytes;
}
// UPLOADEREN
public function Upload(){
if (!in_array($this->file_type, $this->allowed_file_types)) {
return "Forkert filetype";
}
if ($this->max_file_size >= $this->file['size']) {
if (move_uploaded_file($this->file['tmp_name'], $this->folder . $this->name)) {
chmod($this->folder . $this->name, 0777);
return $this->name;
}else {
return "Filen blev ikke Uploaded, Prøv Venligst igen ";
}
}else {
return "Fejl. Filen er for stor";
}
}
}
?>
THANKS