亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

將所有老師講的封裝的方法都寫了一遍,包括上傳和下載封裝的方法

Original 2019-07-25 17:22:02 320
abstract:將所有老師講的封裝的方法都寫了一遍,包括上傳和下載封裝的方法<?php header('content-type:text/html;charset=utf-8'); date_default_timezone_set('Asia/shanghai'); /**  * 文件創(chuàng)建操作  * @param $f

將所有老師講的封裝的方法都寫了一遍,包括上傳和下載封裝的方法

<?php

header('content-type:text/html;charset=utf-8');
date_default_timezone_set('Asia/shanghai');

/**
 * 文件創(chuàng)建操作
 * @param $fileName   //需要創(chuàng)建的文件名
 * @return string   //提示信息
 */
function create_file($fileName)
{
    if (file_exists($fileName)) {
        return '文件已經(jīng)存在';
    } elseif (file_exists(dirname($fileName))) {
        touch($fileName);
        return '文件創(chuàng)建成功';
    } elseif (!file_exists(dirname($fileName))) {
        mkdir(dirname($fileName), 0777, true);
        touch($fileName);
        return '文件創(chuàng)建成功';
    } else {
        return '文件創(chuàng)建失敗';
    }
}

//echo create_file('text5.txt');
//echo create_file('pp/text5.txt');

/**
 * 文件刪除操作
 * @param $fileName   //需要刪除的文件名
 * @return string   //提示信息
 */
function del_file($fileName)
{
    if (!file_exists($fileName) && !is_writeable($fileName)) {
        return '文件無法刪除';
    } elseif (unlink($fileName)) {
        return '文件刪除成功';
    } else {
        return '文件刪除失敗';
    }
}

//echo del_file('text5.txt');
//echo del_file('pp/text5.txt');

/**
 * 文件復(fù)制操作
 * @param $fileName   //需要復(fù)制的文件名
 * @param $dest   //復(fù)制到的目標(biāo)目錄文件夾
 * @return string   //提示信息
 */
function copy_file($fileName,$dest)
{
    if (!file_exists($fileName) && is_writeable($fileName)) {
        return '文件無法復(fù)制';
    } elseif (file_exists($dest)) {
        $destName = $dest .'/' .basename($fileName);
        if (file_exists($destName)) {
            return '文件已經(jīng)存在';
        } else {
            copy($fileName,$destName);
            return '文件復(fù)制成功';
        }
    } elseif (!file_exists($dest)) {
        mkdir($dest,0777,true);
        $destName = $dest .'/' .basename($fileName);
        if (file_exists($destName)) {
            return '文件已經(jīng)存在';
        } else {
            copy($fileName,$destName);
            return '文件復(fù)制成功';
        }
    } else {
        return '文件復(fù)制失敗';
    }
}

//echo copy_file('text3.txt','pp2');
/**
 * 文件重命名操作
 * @param $oldName   //需要重命名的文件名
 * @param $newName   //新文件名
 * @return string   //提示信息
 */
function rename_file($oldName,$newName)
{
    $path = dirname($oldName);
    $destName = $path .'/' .$newName;
    if (!file_exists($oldName) && !is_writeable($oldName)) {
        return '文件無法重命名';
    } elseif (file_exists($destName)) {
        return '文件名已經(jīng)存在';
    } elseif (!file_exists($destName)) {
        rename($oldName,$newName);
        return '文件重命名成功';
    } else {
        return '文件重命名失敗';
    }
}

//echo rename_file('text1.txt','text5.txt');

/**
 * 文件剪切操作
 * @param $fileName   //要剪切的文件
 * @param $dest   //目標(biāo)文件目錄
 * @return string   //提示信息
 */
function cut_file($fileName,$dest)
{
    $destName = $dest .'/' .basename($fileName);
    if (!is_file($fileName)) {
        return '文件無法剪切';
    } elseif (!is_dir($dest)) {
        mkdir($dest,0777,true);
        if (is_file($destName)) {
            return '文件已經(jīng)存在';
        } else {
            rename($fileName,$destName);
            return '文件剪切成功';
        }
    } elseif (is_dir($dest)) {
        if (is_file($destName)) {
            return '文件已經(jīng)存在';
        } else {
            rename($fileName,$destName);
            return '文件剪切成功';
        }
    } else {
        return '文件剪切失敗';
    }
}

//echo cut_file('text3.txt','pp3');

/**
 * 文件信息查詢操作
 * @param $fileName   //需要查詢的文件名
 * @return array|string   //文件信息
 */
function file_get_info($fileName)
{
    if (!is_file($fileName) && is_writeable($fileName)){
        return '文件無法查詢信息';
    } else {
        return [
            'type' => filetype($fileName),
            'ctime' => date('Y-m-d H:i:s',filectime($fileName)),
            'mtime' => date('Y-m-d h:i:s',filemtime($fileName)),
            'atime' => date('Y-m-d H:i:s',fileatime($fileName)),
            'size' => trans_byte(filesize($fileName))
        ];
    }
}

//var_dump(file_get_info('text3.txt'));

/**
 * 字節(jié)轉(zhuǎn)換操作
 * @param $byte   //字節(jié)大小
 * @param int $precision   //保留小數(shù)位數(shù)
 * @return string
 */
function trans_byte($byte,$precision = 2)
{
    $KB = 1024;
    $MB = 1024 * $KB;
    $GB = 1024 * $MB;
    $TB = 1024 * $GB;
    if ($byte < $KB) {
        return $byte .'B';
    } elseif ($byte < $MB) {
        return round($byte / $KB, $precision) . 'KB';
    } elseif ($byte < $GB) {
        return round($byte / $MB, $precision) . 'MB';
    } elseif ($byte < $TB) {
        return round($byte / $GB, $precision) . 'GB';
    } else {
        return round($byte / $TB, $precision) . 'TB';
    }
}

/**
 * 文件內(nèi)容讀取操作
 * @param $fileName   //需要讀取的文件名
 * @return bool|false|string   //文件內(nèi)容或者提示信息
 */
function read_file($fileName)
{
    if (is_file($fileName) && is_writeable($fileName)) {
        return file_get_contents($fileName);
    } else {
        return '文件無法讀取';
    }
}

//echo read_file('text3.txt');

/**
 * 文件內(nèi)容讀取操作(數(shù)組返回)
 * @param $fileName   //需要讀取的文件名
 * @param bool $skip_empty_lines   //是否有空行
 * @return array|bool|string   //文件內(nèi)容或者提示信息
 */
function read_file_array($fileName,$skip_empty_lines = false)
{
    if (is_file($fileName) && is_writeable($fileName)) {
        if ($skip_empty_lines = true) {
            return file($fileName,FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
        } else {
            return file($fileName);
        }
    } else {
        return '文件無法讀取';
    }
}

//var_dump(read_file_array('text3.txt'));

/**
 * 文件寫入操作
 * @param $fileName   //寫入的目標(biāo)文件
 * @param $data   //寫入的數(shù)據(jù)
 * @param bool $clear   //是否清空目標(biāo)文件
 * @return string   //提示信息
 */
function write_file($fileName,$data,$clear = false)
{
    $dirname = dirname($fileName);
    if (!is_dir($dirname)) {
        mkdir($dirname,0777,true);
    }
    if (is_array($data) || is_object($data)) {
        $data = serialize($data);
    }
    if ($clear == false) {
        if (is_file($fileName) && is_readable($fileName)) {
            if (filesize($fileName) > 0) {
                $srcData = file_get_contents($fileName);
                echo $srcData;
                $data = $srcData .$data;
            }
        }
    }

    if (file_put_contents($fileName,$data)) {
        return '文件寫入成功';
    }
    return '文件寫入失敗';
}

$data = [
    'name' => '滅絕',
    'age' => 18
];

//var_dump(write_file('text3.txt',$data));

/**
 * 文件下載操作
 * @param $fileName   //需要下載的文件名
 */
function down_file($fileName)
{
    header('Accept-Length:' .filesize($fileName));
    header('Content-Disposition:attachment;filename=' .basename($fileName));
    readfile($fileName);
}

//down_file('text3.txt');

/**
 * 單文件上傳操作
 * @param $fileInfo   //上傳的文件信息
 * @param string $uploadPath   //上傳的指定目錄
 * @param array $allowExt   //上傳的文件類型
 * @param int $maxSize   //上傳文件最大值
 * @return string   //提示信息
 */
function upload_file($fileInfo,$uploadPath = './upload',$allowExt = ['png', 'jpg', 'jpeg', 'gif', 'txt', 'html'],$maxSize = 1000000)
{
    if ($fileInfo['error'] === 0) {
        $ext = strtolower(pathinfo($fileInfo['name'],PATHINFO_EXTENSION));
        if (!in_array($ext,$allowExt)) {
            return '非法文件類型';
        }
        if ($fileInfo['size'] > $maxSize) {
            return '超出文件上傳最大值!';
        }
        if (!is_uploaded_file($fileInfo['tmp_name'])) {
            return '非法上傳';
        }
        if (!is_dir($uploadPath)) {
            mkdir($uploadPath,0777,true);
        }
        $fileName = md5(uniqid(microtime(true),true)) .'.' .$ext;
        $dest = $uploadPath .'/' .$fileName ;
        if (!move_uploaded_file($fileInfo['tmp_name'],$dest)) {
            return '文件上傳失敗';
        }
        return '文件上傳成功';
    } else {
        switch ($fileInfo['error']) {
            case 1:
                $res = '上傳的文件超過了 php.ini 中 upload_max_filesize 選項限制的值!';
                break;
            case 2:
                $res = '上傳文件的大小超過了 HTML 表單中 MAX_FILE_SIZE 選項指定的值!';
                break;
            case 3:
                $res = '文件只有部分被上傳!';
                break;
            case 4:
                $res = '沒有文件被上傳!';
                break;
            case 6:
                $res = '找不到臨時文件夾';
                break;
            case 7:
                $res = '文件寫入失敗';
                break;
        }
        return $res;
    }
}


Correcting teacher:查無此人Correction time:2019-07-27 11:19:58
Teacher's summary:完成的不錯。文件上傳,后綴名要判斷,文件大小要判斷。以免別人上傳故意上傳垃圾文件。繼續(xù)加油。

Release Notes

Popular Entries