Показать сообщение отдельно

  #4007  
Старый 07.07.2008, 19:12
Naydav
Постоянный
Регистрация: 30.12.2006
Сообщений: 434
С нами: 10191686

Репутация: 210
По умолчанию

Цитата:
пожайлусто исправте ошибку
1) Скрипт работает только при register_globals = on

2) Вот тебе класс аплоада

Применение
Код нужно немного переделать для аплоада только 1 файла
PHP код:
foreach($_FILES as $key=>$data)
                {
                    
$new = explode('.',$data['name']);                
                    
$fupload=& FileUploader::getInstance();
                    
$fupload->_FILESKeyName=$key;
                    
$fupload->newName= $NewFileName
                    $fupload
->DestinationPath= $DestinationPath;
                    
$fupload->AddAllowedMIME('image/jpeg','image/pjpeg','image/gif','image/png');
                    
$file = $fupload->StartUpload();                
                    unset(
$_FILES[$key]['name']);
                } 


Сам класс:
PHP код:
class FileUploader {
        
        var 
$_FILESKeyName;
        
        var 
$newName= false;
        
        var 
$DestinationPath= './';
        
        var 
$AllowedMIME= array();
        
        var 
$isUploaded= false;
        var 
$ErrorMessage= '';
        
        var 
$UploadErrors = Array(
            
0=>'There is no error, the file uploaded with success.',
            
1=>'The uploaded file exceeds the upload_max_filesize directive in php.ini.',
            
2=>'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form.',
            
3=>'The uploaded file was only partially uploaded.',
            
4=>'No file was uploaded.',
            
6=>'Missing a temporary folder.',
            
7=>'Failed to write file to disk.',
            
8=>'File upload stopped by extension.'
        
);
        
        
        function 
FileUploader() {
            
        }
        
        function 
StartUpload() {            
            
$_file= $_FILES[$this->_FILESKeyName];
            if (
$_file['error']!=0) {
                
$this->ErrorMessage= $this->UploadErrors[$_file['error']];
                return;
            }
            else {
                if ( 
count($this->AllowedMIME)>0 && !in_array($_file['type'], $this->AllowedMIME)) {
                    
$this->ErrorMessage= 'Wrong MIME type of uploaded file.';
                    return;
                }
                
                
// start uploading file
                
$this->getNewName($_file['name']);
                
move_uploaded_file($_file['tmp_name'], $this->DestinationPath.$this->newName);
                return 
$this->newName;
            }
        }
        
        function 
getNewName($oldname) {
            
$oldname= explode('.', $oldname);
            
$extention= strtolower($oldname[count($oldname)-1]);
            
array_splice($oldname, count($oldname)-1, 1);
            
$oldname= implode('.', $oldname);
            
            
$this->newName= strtolower($this->newName!==false ? $this->newName : $oldname);
            
            
$new_name= preg_replace("/[^a-z0-9]+/i", '_', $this->newName);
            
$this->newName= $new_name . '.' . $extention;

            
$i= 1;
            while(
file_exists($this->DestinationPath.$this->newName)) {
                
$this->newName= $new_name . '_' . $i . '.' . $extention;
                
$i++;
            }
        }
        
        function 
AddAllowedMIME() {
            
$amount= func_num_args();
            if (
$amount>0) {
                
$arguments= func_get_args();
                for(
$i=0; $i<$amount; $i++)
                    
array_push($this->AllowedMIME, $arguments[$i]);
            }
        }
        
        function 
ClearAllowedMIME() {
            
array_splice($this->AllowedMIME, 0);
        }
        
        function &
getInstance() {
            static 
$instance;
            if (!
$instance) $instance= new FileUploader();
            return 
$instance;
        }
    } 
Класс под php4, т.е не использует преимуществ php5
Пользуйся классами - экономь время

Последний раз редактировалось Naydav; 07.07.2008 в 19:17..
 
Ответить с цитированием