Ok więc robimy sobie plik class.upload.php i wrzucamy do niego:
<?
// mz@zwyrol.com
class Upload {
private $mime = false;
private $thumb_data = array(); private $max_size = false;
private $size;
private $tmp_name;
private $type;
private $file;
private $dir;
private $name;
private $file_name;
public function __construct($data) {
foreach($data as $opt => $val)
$this->$opt = $val;
}
public function SetDir($dir) {
if($char != '/')
$dir .= '/';
$this->dir = $dir;
}
public function SetName($name) {
$this->file_name = $name;
}
public function FileName() {
if(!$this->dir)
return false;
if(!$this->file_name) {
$parts = explode('.', $this->name); $rozszerzenie = $parts[count($parts)-1
];
$this->file_name = date('YmdHis') . rand(1, 1000) . '.' . $rozszerzenie;
$this->file_name = date('YmdHis') . rand(1, 1000) . '.' . $rozszerzenie;
}
return $this->dir . $this->file_name;
}
public function ThumbName($thumb) {
if(!$this->dir || !$this->file_name)
return false;
return $this->dir . $thumb . '-' . $this->file_name;
}
public function SetMime($mime_array) {
$this->mime = $mime_array;
}
public function SetMaxSize($size) {
$this->max_size = ($size * 1024);
}
public function Valid() {
if(!in_array($this->type, $this->mime)) { return -1;
}
}
if(!empty($this->max_size) && ($this->size > $this->max_size)) { return -2;
}
if($this->error > 0)
return -3;
return true;
}
public function Msg($int) {
$msg = '';
switch($int) {
case -1:
$msg = 'Błędny typ pliku, obsługiwane: ' . $this->ValidMimes();
break;
case -2:
$msg = 'Plik jest za duży (' . (float
) round(($this->size / 1024)) . 'kb), maksymalny rozmiar wynosi: ' . ($this->max_size / 1024) . 'kb'; break;
case -3:
$msg = 'Przesyłanie pliku nie powiodło się, spróbuj ponownie';
break;
}
return '<br>' . $msg;
}
public function SaveFile() {
return $this->FileName();
return false;
}
public function MakeThumb($array) {
list
($width_orig, $height_orig) = getimagesize($this->FileName());
foreach($array as $opt => $val) {
$width = $val['width'];
$height = $val['height'];
$proporcja = $val['proporcja'];
if($proporcja) {
$ratio_orig = $width_orig/$height_orig;
if ($width/$height > $ratio_orig) {
$width = $height*$ratio_orig;
} else {
$height = $width/$ratio_orig;
}
}
$image_p = imagecreatetruecolor($width, $height);
$image = imagecreatefromjpeg($this->FileName());
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
$thumb[$opt] = $this->ThumbName($opt);
$img = imagejpeg($image_p, $thumb[$opt], 100);
}
return $thumb;
}
public function ValidMimes() {
if(!is_array($this->mime)) return false;
$mimes = array('image/gif' => 'gif', 'image/png' => 'png',
'image/x-MS-bmp' => 'bmp',
'image/jpeg' => 'jpg',
'image/pjpg' => 'jpg',
'image/pjpeg' => 'jpg',
'image/jpg' => 'jpg',
'image/tiff' => 'tif',
'text/html' => 'html',
'text/plain' => 'txt',
'application/x-gzip' => 'tgz',
'application/x-tar' => 'tar',
'application/zip' => 'zip',
'application/mac-binhex40' => 'hqx',
'application/msword' => 'doc',
'application/pdf' => 'pdf',
'application/postcript' => 'ps',
'application/rtf' => 'rtf',
'application/x-dvi' => 'dvi',
'application/x-latex' => 'latex',
'application/x-shockwave-flash' => 'swf',
'application/x-tex' => 'tex',
'audio/midi' => 'mid',
'audio/basic' => 'au',
'audio/mpeg' => 'mp3',
'audio/x-pn-realaudio' => 'ram',
'audio/x-realaudio' => 'ra',
'audio/x-wav' => 'wav',
'video/mpeg' => 'mpg',
'model/vrml' => 'wrl',
'video/quicktime' => 'mov',
'video/x-msvideo' => 'avi'
);
foreach($this->mime as $m) {
$mime[] = $mimes[$m];
}
}
}
}
?>
Robimy plik upload.php i wrzucamy tam:
<?
// mz@zwyrol.com
include('class.upload.php');
if(!empty($_FILES['foto']['name'])) { $up = new Upload($_FILES['foto']);
$up->SetDir('../img/'); // ustawia katalog do ktorego będzie mial wrzucic zdjęcia i miniatury
$mime_types = array('image/jpeg','image/pjpg', 'image/pjpeg', 'image/jpg'); $up->SetMime($mime_types); // Metoda, która ustawia valid mime types dla plików z tablicy $mime_types
if(($num = $up->Valid()) === true) {
$file = $up->SaveFile();
echo $up->Msg(-3
); // pokazuje błąd, że plik się nie uploadnął else {
// tablica do miniatur
// Jeśli proporcja = 1 - ustawi proporcje, jeśli = 0 ustawi rozmiar miniatury taki jak w podanych wartościach
// Moze byc dowolna ilość elementów tworzyć po prostu:
// $thumb = array('element' => array('width' => 'szerokość', 'height' => 'wysokość', 'proporcja' => 'wartośc 0 lub 1'));
// Stworzy tyle miniatur ile jest elementów w poniższym przykładzie tworzy dwie miniatury
$thumb = array('thumb1' => array('width' => 100, 'height' => 100, 'proporcja' => 1), 'thumb2' => array('width' => 410, 'height' => 310, 'proporcja' => 1
), );
$array = $up->MakeThumb($thumb);
$array['foto'] = $file;
var_dump($array); // pokazuje zawartość tablicy $array // Zwróci tablice asocjacyjna klucze 'foto', 'thumb1', 'thumb2' i wartości to pathe do plików
}
} else {
echo $up->Msg($num); // pokazuje komunikat błędu jeśli validacja się nie powiedzie }
}
?>
I robimy formularz np. form.html:
<form action="upload.php" method="post" enctype="multipart/form-data"> <input type="file" name="foto"><br> <input type="submit" value="Dodaj zdjęcie">
Wchodzimy na
http://adresURL.pl/form.html i uploadujemy plik
Mam nadzieje, że pomogło i będzie działać. W razie problemów proszę pisać. Pozdrawiam.