2010-01-04 19:44:12 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright 2010 City of Bloomington, Indiana
|
2011-06-14 13:29:37 +00:00
|
|
|
* @license http://www.gnu.org/licenses/agpl.txt GNU/AGPL, see LICENSE.txt
|
2010-01-04 19:44:12 +00:00
|
|
|
* @author Cliff Ingham <inghamn@bloomington.in.gov>
|
|
|
|
*/
|
|
|
|
class Map
|
|
|
|
{
|
|
|
|
public static $extensions = array(
|
|
|
|
'jpg'=>array('mime_type'=>'image/jpeg','media_type'=>'image'),
|
|
|
|
'gif'=>array('mime_type'=>'image/gif','media_type'=>'image'),
|
|
|
|
'png'=>array('mime_type'=>'image/png','media_type'=>'image'),
|
|
|
|
'tiff'=>array('mime_type'=>'image/tiff','media_type'=>'image')
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $directory Where to store the file
|
|
|
|
* @param array|string $file Either an entry from $_FILES or a path to a file
|
2010-01-04 20:23:16 +00:00
|
|
|
* @param string $newName The filename to use
|
2010-01-04 19:44:12 +00:00
|
|
|
*/
|
2010-01-04 20:23:16 +00:00
|
|
|
public static function saveFile($directory,$file,$newName)
|
2010-01-04 19:44:12 +00:00
|
|
|
{
|
|
|
|
// Handle passing in either a $_FILES array or just a path to a file
|
|
|
|
$tempFile = is_array($file) ? $file['tmp_name'] : $file;
|
|
|
|
if (!$tempFile) {
|
|
|
|
throw new Exception('media/uploadFailed');
|
|
|
|
}
|
|
|
|
|
|
|
|
# Find out the mime type for this file
|
|
|
|
$filename = is_array($file) ? basename($file['name']) : basename($file);
|
|
|
|
preg_match("/[^.]+$/",$filename,$matches);
|
|
|
|
$extension = strtolower($matches[0]);
|
|
|
|
|
|
|
|
// Make sure it's a known file type
|
|
|
|
if (!array_key_exists(strtolower($extension),self::$extensions)) {
|
|
|
|
throw new Exception('unknownFileType');
|
|
|
|
}
|
|
|
|
|
2010-01-04 20:23:16 +00:00
|
|
|
// Clean out any previous version of the file
|
|
|
|
foreach(glob("$directory/$newName.*") as $file) {
|
|
|
|
unlink($file);
|
|
|
|
}
|
|
|
|
|
2010-01-04 19:44:12 +00:00
|
|
|
// Move the file where it's supposed to go
|
|
|
|
if (!is_dir($directory)) {
|
|
|
|
mkdir($directory,0777,true);
|
|
|
|
}
|
2010-01-04 20:23:16 +00:00
|
|
|
$newFile = "$directory/$newName.$extension";
|
2010-01-04 19:44:12 +00:00
|
|
|
rename($tempFile,$newFile);
|
|
|
|
chmod($newFile,0666);
|
|
|
|
|
|
|
|
if (!is_file($newFile)) {
|
|
|
|
throw new Exception('media/uploadFailed');
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|