復制代碼 代碼如下:
//實例化上傳類
$upload = new Zend_File_Transfer();
//設置過濾器,大小限制為5M,格式為jpg,gif,png
$upload->addValidator('Size', false, 5 * 1024 * 1024);
$upload->addValidator('Extension', false, 'jpg,gif,png');
if (!$upload->isValid()) {
    print '文件大小或格式不符合';
    exit();
}
//獲取上傳的文件表單,可以有多項
$fileInfo = $upload->getFileInfo();
//獲取后綴名,這里pic為上傳表單file控件的name        
$ext = $this->getExtension($fileInfo['pic']['name']);
//定義生成目錄
$dir = './upload' . date('/Y/m/d/');
//文件重新命名
do {
    $filename = date('His') . rand(100000, 999999) . '.' . $ext;
} while (file_exists($dir . $filename));
//如果目錄不存在則創建目錄
$this->makeDir($dir);
//將文件正式寫入上傳目錄
$upload->setDestination($dir);
$upload->addFilter('Rename', array('target' => $filename, 'overwrite' => true));
if (!$upload->receive()) {
    print '上傳圖片失敗';
    exit();
}
print $filename;
獲取文件擴展名的方法:
復制代碼 代碼如下:
/**
 * 獲取文件擴展名
 * 
 * @param string $fileName
 * @return string
 */
public function getExtension($fileName) {
    if (!$fileName) {
        return '';
    }
    $exts = explode(".", $fileName);
    $ext = end($exts);
    return $ext;
}
創建目錄的方法:
復制代碼 代碼如下:
/**
 * 創建目錄
 * 
 * @param string $path
 * @return boolean
 */
public function makeDir($path) {
    if (DIRECTORY_SEPARATOR == "http://") {//windows os
        $path = iconv('utf-8', 'gbk', $path);
    }
    if (!$path) {
        return false;
    }
    if (file_exists($path)) {
        return true;
    }
    if (mkdir($path, 0777, true)) {
        return true;
    }
    return false;
}
新聞熱點
疑難解答