<?php
$oldErr = error_reporting(0);
class UPLOAD{
var $_dosyalar = array();
var $_sayDosya = 0;
var $_hataMsg = array();
var $_bilgiVer = array();
var $_yolDizin = './upload';
var $_yazUstune = false;
var $_imgUzanti = array('png', 'jpg', 'jpeg', 'jpe', 'gif', 'bmp');
var $_gdEtkin = false;
function UPLOAD($_FILES)
{
$this->_gdEtkin = extension_loaded('gd');
$dosyalar = array();
$dosyaSay = count($_FILES['name']);
if( $dosyaSay===1 && !is_array($_FILES['name']) ) {
$dosyalar['name'][] = $_FILES['name'];
$dosyalar['type'][] = $_FILES['type'];
$dosyalar['tmp_name'][] = $_FILES['tmp_name'];
$dosyalar['error'][] = $_FILES['error'];
$dosyalar['size'][] = $_FILES['size'];
} else {
$dosyalar = $_FILES;
}
for( $i=0; $i<$dosyaSay; ++$i )
{
if( !empty($dosyalar['name'][$i]) )
{
$dosyalar['ext'][$i] = $this->_dosyaUzantisi( $dosyalar['name'][$i] );
$this->_dosyalar['name'][] = $dosyalar['name'][$i];
$this->_dosyalar['ex_name'][] = $dosyalar['name'][$i];
$this->_dosyalar['ext'][] = $dosyalar['ext'][$i];
$this->_dosyalar['type'][] = $dosyalar['type'][$i];
$this->_dosyalar['tmp_name'][] = $dosyalar['tmp_name'][$i];
$this->_dosyalar['error'][] = $dosyalar['error'][$i];
$this->_dosyalar['size'][] = $dosyalar['size'][$i];
if( $dosyalar['error'][$i] !== 0 ){
$this->_hataMsg[] = "<strong>{$dosyalar['name'][$i]}</strong> ".$this->_phpDurumMsj($dosyalar['error'][$i]);
}
if( $this->_mimeKontrol($dosyalar['ext'][$i], $dosyalar['type'][$i]) === false ){
$this->_hataMsg[] = "<strong>{$dosyalar['name'][$i]}</strong> dosyasının MIME tipine izin verilmiyor.";
}
if( in_array($dosyalar['ext'][$i], $this->_imgUzanti) ) {
if( $this->_imgKontrol($dosyalar['type'][$i], $dosyalar['tmp_name'][$i])===false ) {
$this->_hataMsg[] = "<strong>{$dosyalar['name'][$i]}</strong> dosyası geçerli bir resim dosyası değil !";
}
}
$this->_sayDosya++;
}
}
}
function yolDizin($yol)
{
if( !is_dir($yol) && !mkdir($yol, 0755) ){
$this->_hataMsg[] = "<strong>$yol</strong> klasörü bulunamıyor";
}
if( !is_writable($yol) && !chmod($yol, 0755) ){
$this->_hataMsg[] = "<strong>$this->_yolDizin</strong> klasörü yazılabilir değil";
}
$this->_yolDizin = $yol;
}
function minDosya($min)
{
if( $this->_sayDosya < $min ) {
$this->_hataMsg[] = "En az <strong>$min</strong> dosya yüklemeniz gerekiyor.";
}
}
function maxDosya($max)
{
if( $this->_sayDosya > $max ) {
$this->_hataMsg[] = "Aynı anda en fazla <strong>$max</strong> dosya yükleyebilirsiniz.";
}
}
function minBoyut($min)
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
if( $this->_dosyalar['size'][$i] < $this->_kb2bayt($min) ){
$this->_hataMsg[] = "<strong>{$this->_dosyalar['ex_name'][$i]}</strong> dosyasının boyutu <strong>$min KB</strong> altında olamaz.";
}
}
}
function maxBoyut($max)
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
if( $this->_dosyalar['size'][$i] > $this->_kb2bayt($max) ){
$this->_hataMsg[] = "<strong>{$this->_dosyalar['ex_name'][$i]}</strong> dosyasının boyutu <strong>$max KB</strong> üstünde olamaz.";
}
}
}
function tipYasak($uzantilar, $ayirici=',')
{
$uzantilar = $this->_dosyaUzantisiParselle($ayirici, $uzantilar);
if( !is_array($uzantilar) ) {
$this->_hataMsg[] = "<strong>tipYasak</strong> ayarı yanlış belirlenmiş.";
}
for( $i=0; $i<$this->_sayDosya; ++$i)
{
if( in_array($this->_dosyalar['ext'][$i], $uzantilar) ){
$this->_hataMsg[] = "<strong>{$this->_dosyalar['ex_name'][$i]}</strong> dosyası izin verilmeyen bir türde.";
}
}
}
function tipKabul($uzantilar, $ayirici=',')
{
$uzantilar = $this->_dosyaUzantisiParselle($ayirici, $uzantilar);
if( !is_array($uzantilar) ) {
$this->_hataMsg[] = "<strong>tipKabul</strong> ayarı yanlış belirlenmiş.";
}
for( $i=0; $i<$this->_sayDosya; ++$i)
{
if( !in_array($this->_dosyalar['ext'][$i], $uzantilar) ){
$this->_hataMsg[] = "<strong>{$this->_dosyalar['ex_name'][$i]}</strong> dosyası izin verilmeyen bir türde.";
}
}
}
function yeniAd($ad)
{
if( is_bool($ad) && $ad === true )
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
$this->_dosyalar['name'][$i] = md5(uniqid(mt_rand(), true)).'.'.$this->_dosyalar['ext'][$i];
}
}
elseif( is_bool($ad) && $ad === false )
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
$this->_dosyalar['name'][$i] = $this->_dosyaIsmiTemizle( $this->_dosyalar['name'][$i] );
}
}
else
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
$this->_dosyalar['name'][$i] = $ad.'.'.$this->_dosyalar['ext'][$i];
}
}
}
function basaEk($ek)
{
if( is_bool($ek) && $ek === true )
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
$oldName = $this->_dosyalar['name'][$i];
$newName = ($i+1).'_'.$oldName;
$this->_dosyalar['name'][$i] = $newName;
}
}
if( is_string($ek) )
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
$oldName = $this->_dosyalar['name'][$i];
$newName = $ek.'_'.$oldName;
$this->_dosyalar['name'][$i] = $newName;
}
}
}
function sonaEk($ek)
{
if( is_bool($ek) && $ek === true )
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
$oldName = $this->_dosyalar['name'][$i];
$extension = '.'.$this->_dosyalar['ext'][$i];
$noExtension = explode($extension, $oldName);
$newName = $noExtension[0].'_'.($i+1).$extension;
$this->_dosyalar['name'][$i] = $newName;
}
}
if( is_string($ek) )
{
for( $i=0; $i<$this->_sayDosya; ++$i )
{
$oldName = $this->_dosyalar['name'][$i];
$extension = '.'.$this->_dosyalar['ext'][$i];
$noExtension = explode($extension, $oldName);
$newName = $noExtension[0].'_'.$ek.$extension;
$this->_dosyalar['name'][$i] = $newName;
}
}
}
function yazUstune($durum=true)
{
if( is_bool($durum) && $durum === false ) {
$this->_yazUstune = false;
} else {
$this->_yazUstune = true;
}
}
function baslat()
{
if( !empty($this->_hataMsg) ) {
return false;
}
for( $i=0; $i<$this->_sayDosya; ++$i)
{
$isim = $this->_dosyaIsmiTemizle( $this->_dosyalar['name'][$i] );
$adres = $this->_yolDizin.'/'.$isim;
$this->_bilgiVer[$i]['yeniAd'] = $isim;
$this->_bilgiVer[$i]['eskiAd'] = $this->_dosyalar['ex_name'][$i];
$this->_bilgiVer[$i]['icerik'] = $this->_dosyalar['type'][$i];
$this->_bilgiVer[$i]['boyut'] = $this->_dosyalar['size'][$i];
$this->_bilgiVer[$i]['adres'] = $adres;
if( $this->_yazUstune === false && file_exists($adres) ) {
$this->_bilgiVer[$i]['durum'] = "ERROR";
$this->_bilgiVer[$i]['mesaj'] = "Dosya yüklenmedi! Çünkü aynı isimde bir dosya zaten var.";
}
elseif( move_uploaded_file($this->_dosyalar['tmp_name'][$i], $adres) ) {
$this->_bilgiVer[$i]['durum'] = "OK";
$this->_bilgiVer[$i]['mesaj'] = "Dosya yüklendi.";
} else {
$this->_bilgiVer[$i]['durum'] = "HATA";
$this->_bilgiVer[$i]['mesaj'] = 'Muhtemelen dosya taşıma hatası meydana geldi !';
}
}
return true;
}
function _imgKontrol($mime, $dosya)
{
if( $this->_gdEtkin && !imagecreatefromstring(file_get_contents($dosya)) ) {
return false;
}
if( ($mime == 'image/pjpeg' || $mime == 'image/jpeg') && !getimagesize($dosya) ) return false;
if( ($mime == 'image/png' || $mime == 'image/x-png') && !getimagesize($dosya) ) return false;
if( $mime == 'image/gif' && !getimagesize($dosya) ) return false;
return true;
}
function _mimeKontrol($uzanti, $mime)
{
$uzantilar = array(
'hqx' => 'application/mac-binhex40',
'cpt' => 'application/mac-compactpro',
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
'bin' => 'application/macbinary',
'dms' => 'application/octet-stream',
'lha' => 'application/octet-stream',
'lzh' => 'application/octet-stream',
'exe' => 'application/octet-stream',
'class' => 'application/octet-stream',
'psd' => 'application/x-photoshop',
'so' => 'application/octet-stream',
'sea' => 'application/octet-stream',
'dll' => 'application/octet-stream',
'oda' => 'application/oda',
'pdf' => array('application/pdf', 'application/x-download'),
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'ps' => 'application/postscript',
'smi' => 'application/smil',
'smil' => 'application/smil',
'mif' => 'application/vnd.mif',
'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
'ppt' => array('application/powerpoint', 'application/vnd.ms-powerpoint'),
'wbxml' => 'application/wbxml',
'wmlc' => 'application/wmlc',
'dcr' => 'application/x-director',
'dir' => 'application/x-director',
'dxr' => 'application/x-director',
'dvi' => 'application/x-dvi',
'gtar' => 'application/x-gtar',
'gz' => 'application/x-gzip',
'php' => 'application/x-httpd-php',
'php4' => 'application/x-httpd-php',
'php3' => 'application/x-httpd-php',
'phtml' => 'application/x-httpd-php',
'phps' => 'application/x-httpd-php-source',
'js' => 'application/x-javascript',
'swf' => 'application/x-shockwave-flash',
'sit' => 'application/x-stuffit',
'tar' => 'application/x-tar',
'tgz' => 'application/x-tar',
'xhtml' => 'application/xhtml+xml',
'xht' => 'application/xhtml+xml',
'zip' => array('application/x-zip', 'application/zip', 'application/x-zip-compressed'),
'mid' => 'audio/midi',
'midi' => 'audio/midi',
'mpga' => 'audio/mpeg',
'mp2' => 'audio/mpeg',
'mp3' => array('audio/mpeg', 'audio/mpg'),
'aif' => 'audio/x-aiff',
'aiff' => 'audio/x-aiff',
'aifc' => 'audio/x-aiff',
'ram' => 'audio/x-pn-realaudio',
'rm' => 'audio/x-pn-realaudio',
'rpm' => 'audio/x-pn-realaudio-plugin',
'ra' => 'audio/x-realaudio',
'rv' => 'video/vnd.rn-realvideo',
'wav' => 'audio/x-wav',
'bmp' => 'image/bmp',
'gif' => 'image/gif',
'jpeg' => array('image/jpeg', 'image/pjpeg'),
'jpg' => array('image/jpeg', 'image/pjpeg'),
'jpe' => array('image/jpeg', 'image/pjpeg'),
'png' => array('image/png', 'image/x-png'),
'tiff' => 'image/tiff',
'tif' => 'image/tiff',
'css' => 'text/css',
'html' => 'text/html',
'htm' => 'text/html',
'shtml' => 'text/html',
'txt' => 'text/plain',
'text' => 'text/plain',
'log' => array('text/plain', 'text/x-log'),
'rtx' => 'text/richtext',
'rtf' => 'text/rtf',
'xml' => 'text/xml',
'xsl' => 'text/xml',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mpe' => 'video/mpeg',
'qt' => 'video/quicktime',
'mov' => 'video/quicktime',
'avi' => 'video/x-msvideo',
'movie' => 'video/x-sgi-movie',
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
'word' => array('application/msword', 'application/octet-stream'),
'xl' => 'application/excel',
'eml' => 'message/rfc822'
);
if( !isset($uzantilar[$uzanti]) ) return false;
if( is_array($uzantilar[$uzanti]) ) {
return in_array($mime, $uzantilar[$uzanti]);
}
return ($mime===$uzantilar[$uzanti]);
}
function _dosyaIsmiTemizle($oldName)
{
$oldName = trim($oldName);
$extension = strrchr($oldName, '.');
$extRegex = "/(\\$extension)\$/";
$onlyName = preg_split($extRegex, $oldName, -1, PREG_SPLIT_NO_EMPTY);
$degistir = array(
'ı'=>'i', 'ğ'=>'g', 'ü'=>'u', 'ş'=>'s', 'ö'=>'o', 'ç'=>'c',
'İ'=>'i', 'Ğ'=>'g', 'Ü'=>'U', 'Ş'=>'s', 'Ö'=>'o', 'Ç'=>'c'
);
$onlyName[0] = strtr($onlyName[0], $degistir);
$onlyName[0] = preg_replace('/\W/', '_', $onlyName[0]);
return ($onlyName[0].$extension);
}
function _dosyaUzantisiParselle($ayirici, $uzantilar)
{
$sonuc = explode($ayirici, $uzantilar);
$sonuc = array_map('trim', $sonuc);
return $sonuc;
}
function _dosyaUzantisi($dosya)
{
$ext = strtolower(strrchr($dosya, '.'));
$ext = substr($ext, 1);
return $ext;
}
function _bayt2kb($bayt) { return round(($bayt/1024), 2); }
function _kb2bayt($bayt) { return round(($bayt*1024), 2); }
function bilgiVer()
{
return $this->_bilgiVer;
}
function sonHata()
{
return end($this->_hataMsg);
}
function ilkHata()
{
return $this->_hataMsg[0];
}
function tumHata()
{
return $this->_hataMsg;
}
function _phpDurumMsj($no)
{
$durum = array();
$durum[0] = 'dosyası başarıyla yüklendi';
$durum[1] = 'dosyası, php.ini içerisindeki upload_max_filesize direktifini aşıyor';
$durum[2] = 'dosyası, HTML formundaki MAX_FILE_SIZE direktifini aşıyor';
$durum[3] = 'dosyasının yalnızca bir kısmı yüklendi';
$durum[4] = 'dosyası yüklenemedi';
$durum[5] = 'Geçiçi klasör eksik';
return $durum[$no];
}
}
error_reporting($oldErr);
?>