Changeset 2023

Show
Ignore:
Timestamp:
02/10/08 13:11:49 (6 months ago)
Author:
Shadowhand
Message:

Fixing Forge_Upload to default to not overwrite files, but generate a unique filename if the file already exists.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/modules/forge/libraries/Form_Upload.php

    r1923 r2023  
    2323        protected $upload; 
    2424 
    25         // Upload directory 
     25        // Upload directory and filename 
    2626        protected $directory; 
     27        protected $filename; 
    2728 
    28         public function __construct($name
     29        public function __construct($name, $filename = FALSE
    2930        { 
    3031                parent::__construct($name); 
     
    3940                                // Hack to allow file-only inputs, where no POST data is present 
    4041                                $_POST[$name] = $this->upload['name']; 
     42 
     43                                // Set the filename 
     44                                $this->filename = empty($filename) ? FALSE : $filename; 
    4145                        } 
    4246                        else 
     
    9195                        } 
    9296 
     97                        if (file_exists($filepath = $this->directory.$filename)) 
     98                        { 
     99                                if ($this->filename !== TRUE OR ! is_writable($filepath)) 
     100                                { 
     101                                        // Prefix the file so that the filename is unique 
     102                                        $filepath = $this->directory.'uploadfile-'.uniqid(time()).'-'.$this->upload['name']; 
     103                                } 
     104                        } 
     105 
    93106                        // Move the uploaded file to the upload directory 
    94                         move_uploaded_file($this->upload['tmp_name'], $filename = $this->directory.$filename); 
     107                        move_uploaded_file($this->upload['tmp_name'], $filepath); 
    95108                } 
    96109 
     
    98111                { 
    99112                        // Reset the POST value to the new filename 
    100                         $this->data['value'] = $_POST[$this->data['name']] = $filename
     113                        $this->data['value'] = $_POST[$this->data['name']] = $filepath
    101114                } 
    102115