Skip to content

Instantly share code, notes, and snippets.

@debuglee
Created August 10, 2012 18:18
Show Gist options
  • Save debuglee/3316395 to your computer and use it in GitHub Desktop.
Save debuglee/3316395 to your computer and use it in GitHub Desktop.
File Upload Move
<?php
/**
* file store
*/
class Store
{
const TMP_FOLDER = '/tmp/'; // 临时文件储存目录
const FOLDER = '/file/'; // 存储目录
var $FILE_MIME = array (
//applications
'ai' => 'application/postscript',
'eps' => 'application/postscript',
'exe' => 'application/octet-stream',
'doc' => 'application/vnd.ms-word',
'xls' => 'application/vnd.ms-excel',
'ppt' => 'application/vnd.ms-powerpoint',
'pps' => 'application/vnd.ms-powerpoint',
'pdf' => 'application/pdf',
'xml' => 'application/xml',
'odt' => 'application/vnd.oasis.opendocument.text',
'swf' => 'application/x-shockwave-flash',
// archives
'gz' => 'application/x-gzip',
'tgz' => 'application/x-gzip',
'bz' => 'application/x-bzip2',
'bz2' => 'application/x-bzip2',
'tbz' => 'application/x-bzip2',
'zip' => 'application/zip',
'rar' => 'application/x-rar',
'tar' => 'application/x-tar',
'7z' => 'application/x-7z-compressed',
// texts
'txt' => 'text/plain',
'php' => 'text/x-php',
'html' => 'text/html',
'htm' => 'text/html',
'js' => 'text/javascript',
'css' => 'text/css',
'rtf' => 'text/rtf',
'rtfd' => 'text/rtfd',
'py' => 'text/x-python',
'java' => 'text/x-java-source',
'rb' => 'text/x-ruby',
'sh' => 'text/x-shellscript',
'pl' => 'text/x-perl',
'sql' => 'text/x-sql',
// images
'bmp' => 'image/x-ms-bmp',
'jpg' => 'image/jpeg',
'jpeg' => 'image/jpeg',
'gif' => 'image/gif',
'png' => 'image/png',
'tif' => 'image/tiff',
'tiff' => 'image/tiff',
'tga' => 'image/x-targa',
'psd' => 'image/vnd.adobe.photoshop',
//audio
'mp3' => 'audio/mpeg',
'mid' => 'audio/midi',
'ogg' => 'audio/ogg',
'mp4a' => 'audio/mp4',
'wav' => 'audio/wav',
'wma' => 'audio/x-ms-wma',
// video
'avi' => 'video/x-msvideo',
'dv' => 'video/x-dv',
'mp4' => 'video/mp4',
'mpeg' => 'video/mpeg',
'mpg' => 'video/mpeg',
'mov' => 'video/quicktime',
'wm' => 'video/x-ms-wmv',
'flv' => 'video/x-flv',
'mkv' => 'video/x-matroska'
);
// function __construct(argument)
// {
// # code...
// }
function save($file_name, $tmp_file)
{
// -------------------------------------
// 将临时文件改名为上传的文件名
// -------------------------------------
$new_tmp_file = $this->TMP_FOLDER . $file_name;
$rename_tmp_file = $this->moveFile($tmp_file, $new_tmp_file);
// -------------------------------------
// 取得上传的文件类型
// -------------------------------------
$file_mime = $this->_getMimeType($rename_tmp_file);
echo json_encode($file_mime);
}
/**
* 建立文件夹
*
* @param string $aimUrl
* @return viod
*/
function createDir($aimUrl) {
$aimUrl = str_replace('', '/', $aimUrl);
$aimDir = '';
$arr = explode('/', $aimUrl);
foreach ($arr as $str) {
$aimDir .= $str . '/';
if (!file_exists($aimDir)) {
mkdir($aimDir);
}
}
}
/**
* 建立文件
*
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
function createFile($aimUrl, $overWrite = false) {
if (file_exists($aimUrl) && $overWrite == false) {
return false;
} elseif (file_exists($aimUrl) && $overWrite == true) {
FileUtil::unlinkFile($aimUrl);
}
$aimDir = dirname($aimUrl);
FileUtil::createDir($aimDir);
touch($aimUrl);
return true;
}
/**
* 移动文件夹
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
function moveDir($oldDir, $aimDir, $overWrite = false) {
$aimDir = str_replace('', '/', $aimDir);
$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
$oldDir = str_replace('', '/', $oldDir);
$oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
if (!is_dir($oldDir)) {
return false;
}
if (!file_exists($aimDir)) {
FileUtil::createDir($aimDir);
}
@$dirHandle = opendir($oldDir);
if (!$dirHandle) {
return false;
}
while(false !== ($file = readdir($dirHandle))) {
if ($file == '.' || $file == '..') {
continue;
}
if (!is_dir($oldDir.$file)) {
FileUtil::moveFile($oldDir . $file, $aimDir . $file, $overWrite);
} else {
FileUtil::moveDir($oldDir . $file, $aimDir . $file, $overWrite);
}
}
closedir($dirHandle);
return rmdir($oldDir);
}
/**
* 移动文件
*
* @param string $fileUrl 临时文件地址
* @param string $aimUrl 目标文件地址
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
function moveFile($fileUrl, $aimUrl, $overWrite = false) {
if (!file_exists($fileUrl)) {
return false;
}
if (file_exists($aimUrl) && $overWrite = false) {
return false;
} elseif (file_exists($aimUrl) && $overWrite = true) {
FileUtil::unlinkFile($aimUrl);
}
$aimDir = dirname($aimUrl);
FileUtil::createDir($aimDir);
rename($fileUrl, $aimUrl);
return true;
}
/**
* 删除文件夹
*
* @param string $aimDir
* @return boolean
*/
function unlinkDir($aimDir) {
$aimDir = str_replace('', '/', $aimDir);
$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';
if (!is_dir($aimDir)) {
return false;
}
$dirHandle = opendir($aimDir);
while(false !== ($file = readdir($dirHandle))) {
if ($file == '.' || $file == '..') {
continue;
}
if (!is_dir($aimDir.$file)) {
FileUtil::unlinkFile($aimDir . $file);
} else {
FileUtil::unlinkDir($aimDir . $file);
}
}
closedir($dirHandle);
return rmdir($aimDir);
}
/**
* 删除文件
*
* @param string $aimUrl
* @return boolean
*/
function unlinkFile($aimUrl) {
if (file_exists($aimUrl)) {
unlink($aimUrl);
return true;
} else {
return false;
}
echo "string";
}
/**
* 复制文件夹
*
* @param string $oldDir
* @param string $aimDir
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
function copyDir($oldDir, $aimDir, $overWrite = false) {
$aimDir = str_replace('', '/', $aimDir);
$aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';
$oldDir = str_replace('', '/', $oldDir);
$oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir.'/';
if (!is_dir($oldDir)) {
return false;
}
if (!file_exists($aimDir)) {
FileUtil::createDir($aimDir);
}
$dirHandle = opendir($oldDir);
while(false !== ($file = readdir($dirHandle))) {
if ($file == '.' || $file == '..') {
continue;
}
if (!is_dir($oldDir . $file)) {
FileUtil::copyFile($oldDir . $file, $aimDir . $file, $overWrite);
} else {
FileUtil::copyDir($oldDir . $file, $aimDir . $file, $overWrite);
}
}
return closedir($dirHandle);
}
/**
* 复制文件
*
* @param string $fileUrl
* @param string $aimUrl
* @param boolean $overWrite 该参数控制是否覆盖原文件
* @return boolean
*/
function copyFile($fileUrl, $aimUrl, $overWrite = false) {
if (!file_exists($fileUrl)) {
return false;
}
if (file_exists($aimUrl) && $overWrite == false) {
return false;
} elseif (file_exists($aimUrl) && $overWrite == true) {
FileUtil::unlinkFile($aimUrl);
}
$aimDir = dirname($aimUrl);
FileUtil::createDir($aimDir);
copy($fileUrl, $aimUrl);
return true;
}
/**
* 获取文件类型,判断系统是否支持该函数
*/
function _getMimeDetect() {
if (class_exists('finfo')) {
return 'finfo';
} else if (function_exists('mime_content_type')) {
return 'mime_content_type';
} else if ( function_exists('exec')) {
$result = exec('file -ib '.escapeshellarg(__FILE__));
if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
return 'linux';
}
$result = exec('file -Ib '.escapeshellarg(__FILE__));
if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
return 'bsd';
}
}
return 'internal';
}
function _getMimeType($path) {
$mime = $this->FILE_MIME;
$fmime = $this->_getMimeDetect();
switch($fmime) {
case 'finfo':
$finfo = finfo_open(FILEINFO_MIME, '/usr/share/misc/magic');
if ($finfo) {
$type = @finfo_file($finfo, $path);
}
break;
case 'mime_content_type':
$type = mime_content_type($path);
break;
case 'linux':
$type = exec('file -ib '.escapeshellarg($path));
break;
case 'bsd':
$type = exec('file -Ib '.escapeshellarg($path));
break;
default:
$pinfo = pathinfo($path);
$ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
$type = isset($mime[$ext]) ? $mime[$ext] : 'unkown';
break;
}
$type = explode(';', $type);
//需要加上这段,因为如果使用mime_content_type函数来获取一个不存在的$path时会返回'application/octet-stream'
if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') {
$pinfo = pathinfo($path);
$ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
if (!empty($ext) AND !empty($mime[$ext])) {
$type[0] = array('type' => $ext, 'mime' => $mime[$ext]);
}
}
return $type[0];
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment