/** * Get file contents from $_FILE server variable and move * the uploaded file to a location specified in $location * * @param string $location (optional) : upload media directory * @throws Steelcode_Media_Exception */ public function uploadFile($location = null) { if (!isset($_FILES) || !is_array($_FILES)) { $this->_lastFileError = 'No files are selected to upload'; throw new Steelcode_Media_Exception($this->_lastFileError); } if (Steelcode_String_Helper::isNull($location)) { $mediaDir = $this->_mediaLocation; } else { $mediaDir = $location; } if (Steelcode_String_Helper::isNull($mediaDir)) { $this->_lastFileError = 'No destination is set to move uploaded files'; throw new Steelcode_Media_Exception($this->_lastFileError); } $fileName = $_FILES[$this->_inputIndex]['name']; $fileSize = $_FILES[$this->_inputIndex]['size']; $fileType = $_FILES[$this->_inputIndex]['type']; $fileExt = Steelcode_Media_Helper::fileExtension($fileName); if ($fileSize > $this->_fileMaxSize) { $this->_lastFileError = 'File size is larger than the limit'; throw new Steelcode_Media_Exception($this->_lastFileError); } if (false === in_array($fileExt, $this->_extensionList)) { $this->_lastFileError = 'You can not upload this kind of file'; throw new Steelcode_Media_Exception($this->_lastFileError); } do { $shaName = sha1($fileName); $shaName = str_shuffle($shaName) . ".{$fileExt}"; $newFilePath = "{$mediaDir}{$shaName}"; } while (file_exists($newFilePath)); if ($_FILES[$this->_inputIndex]['error'] > 0) { $this->_lastFileError = 'Server set an error code (' . $_FILES[$this->_inputIndex]['error'] . ')'; $this->_lastFileErrorCode = $_FILES[$this->_inputIndex]['error']; throw new Steelcode_Media_Exception($this->_lastFileError, $this->_lastFileErrorCode); } if (move_uploaded_file($_FILES[$this->_inputIndex]['tmp_name'], $newFilePath)) { $this->_lastFileName = $fileName; $this->_lastFileType = $fileType; $this->_lastFileExt = $fileExt; $this->_lastFileSize = $fileSize; $this->_lastFileHash = $shaName; } else { $this->_lastFileError = 'There was an error while uploading the file'; throw new Steelcode_Media_Exception($this->_lastFileError); } }
/** * Get the properties of a file * * @param string $file : name of the file * @return array : array of properties */ public function getFileProperties($file) { $fileType = $this->getFileType($file); $fileSizeInBytes = filesize($file); $fileSize = (int) $fileSizeInBytes; $fileSize = Steelcode_File_Helper::sizeString($fileSize); $extension = Steelcode_Media_Helper::fileExtension($file); $fileModifiedTime = date('d M Y', filemtime($file)); return array('type' => $fileType === 'unknown' ? strtoupper($extension) . ' file' : ucfirst($fileType), 'size_as_string' => $fileSize, 'size_in_bytes' => $fileSizeInBytes, 'date_modified' => $fileModifiedTime); }