php 文件大小函数,PHP filesize() 函数

【摘要】

PHP即“超文本预处理器”,是一种通用开源脚本语言。PHP是在服务器端执行的脚本语言,与C语言类似,是常用的网站编程语言。PHP独特的语法混合了C、Java、Perl以及 PHP 自创的语法。下面是PHP filesize() 函数,让我们一起来看看PHP filesize() 函数的具体内容吧!

PHP filesize() 函数filesize

作用:函数返回指定文件的大小

语法:filesize(filename)

参数:

filename:必需。规定要检查的文件。

返回值:

返回文件大小的字节数,如果出错返回 FALSE 并生成一条 E_WARNING 级的错误。

filesize 示例

示例一<?php

// 输出类似:somefile.txt: 1024 bytes

$filename = 'somefile.txt';

echo $filename . ': ' . filesize($filename) . ' bytes';

?>

示例二<?php

function human_filesize($bytes, $decimals = 2) {

$sz = 'BKMGTP';

$factor = floor((strlen($bytes) - 1) / 3);

return sprintf("%.{$decimals}f", $bytes / pow(1024, $factor)) . @$sz[$factor];

}

?>

示例三<?php

/**

* Converts

38035d3f718b8ff665dbd01421e4b7fa.pngbytes into human readable file size.

*

* @param string $bytes

* @return string human readable file size (2,87 Мб)

* @author Mogilev Arseny

*/

function FileSizeConvert($bytes)

{

$bytes = floatval($bytes);

$arBytes = array(

0 => array(

"UNIT" => "TB",

"VALUE" => pow(1024, 4)

),

1 => array(

"UNIT" => "GB",

"VALUE" => pow(1024, 3)

),

2 => array(

"UNIT" => "MB",

"VALUE" => pow(1024, 2)

),

3 => array(

"UNIT" => "KB",

"VALUE" => 1024

),

4 => array(

"UNIT" => "B",

"VALUE" => 1

),

);

foreach($arBytes as $arItem)

{

if($bytes >= $arItem["VALUE"])

{

$result = $bytes / $arItem["VALUE"];

$result = str_replace(".", "," , strval(round($result, 2)))." ".$arItem["UNIT"];

break;

}

}

return $result;

}

?>

示例四<?php

/**

* Return file size (even for file > 2 Gb)

* For file size over PHP_INT_MAX (2 147 483 647), PHP filesize function loops from -PHP_INT_MAX to PHP_INT_MAX.

*

* @param string $path Path of the file

* @return mixed File size or false if error

*/

function realFileSize($path)

{

if (!file_exists($path))

return false;

$size = filesize($path);

if (!($file = fopen($path, 'rb')))

return false;

if ($size >= 0)

{//Check if it really is a small file (< 2 GB)

if (fseek($file, 0, SEEK_END) === 0)

{//It really is a small file

fclose($file);

return $size;

}

}

//Quickly jump the first 2 GB with fseek. After that fseek is not working on 32 bit php (it uses int internally)

$size = PHP_INT_MAX - 1;

if (fseek($file, PHP_INT_MAX - 1) !== 0)

{

fclose($file);

return false;

}

$length = 1024 * 1024;

while (!feof($file))

{//Read the file until end

$read = fread($file, $length);

$size = bcadd($size, $length);

}

$size = bcsub($size, $length);

$size = bcadd($size, strlen($read));

fclose($file);

return $size;

}

以上就是PHP filesize() 函数的内容,对PHP感兴趣的小伙伴们可以关注考必过,获取更多PHP前沿资讯和最新技术。最新消息小编会第一时间发布,助力大家考试,加油!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是一个简单的 PHP 文件上传函数的封装示例: ``` function uploadFile($file, $targetDir) { $targetFile = $targetDir . basename($file["name"]); $uploadOk = 1; $imageFileType = strtolower(pathinfo($targetFile,PATHINFO_EXTENSION)); // 检查文件是否已经存在 if (file_exists($targetFile)) { echo "抱歉,文件已经存在。"; $uploadOk = 0; } // 检查文件大小 if ($file["size"] > 500000) { echo "抱歉,文件太大。"; $uploadOk = 0; } // 允许上传的文件格式 if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) { echo "抱歉,只允许上传 JPG, JPEG, PNG 和 GIF 格式的文件。"; $uploadOk = 0; } // 检查 $uploadOk 是否为 0 if ($uploadOk == 0) { echo "抱歉,文件未上传。"; // 如果一切都正常,尝试上传文件 } else { if (move_uploaded_file($file["tmp_name"], $targetFile)) { echo "文件 ". basename( $file["name"]). " 已经上传。"; } else { echo "抱歉,文件上传失败。"; } } } ``` 你可以将这个函数保存在一个单独的 PHP 文件中,然后在你的项目中包含它,以便在需要上传文件时调用它。 ### 回答2: PHP是一种流行的服务器端脚本语言,用于开发Web应用程序。在Web应用程序中,文件上传是常见的功能。下面是一个简单的封装了文件上传功能的PHP函数: ```php function uploadFile($file, $targetDir) { // 检查目标文件夹是否存在,如果不存在则创建 if (!file_exists($targetDir)) { mkdir($targetDir, 0777, true); } // 获取文件名和文件路径 $fileName = $file['name']; $filePath = $targetDir . '/' . $fileName; // 移动上传的文件到目标文件夹 if (move_uploaded_file($file['tmp_name'], $filePath)) { return "文件上传成功"; } else { return "文件上传失败"; } } ``` 这个函数接受两个参数:`$file`和`$targetDir`。`$file`是包含上传文件信息的数组,通常是`$_FILES['file']`,其中`'file'`是HTML表单中文件上传字段的名称。`$targetDir`是要保存上传文件的目标文件夹路径。 在函数内部,我们首先检查目标文件夹是否存在,如果不存在则创建。然后获取上传文件的原始文件名和目标文件路径。最后,我们使用`move_uploaded_file`函数将上传的临时文件移动到目标文件夹。如果移动成功,函数返回"文件上传成功",否则返回"文件上传失败"。 这个函数只是一个简单的文件上传功能示例,实际使用中可能还需要进行文件类型和大小的验证,以及对上传文件名的处理。此外,为了安全考虑,我们可能还需要对上传文件进行进一步的检查和过滤,以防止恶意文件上传和执行。 ### 回答3: PHP文件上传函数的封装包括以下几个步骤: 1. 创建一个函数,使用`function`关键字,命名为`uploadFile`,并接受一个参数`$file`,代表文件的相关信息。 2. 在函数内部,首先判断文件是否成功上传,使用`isset`函数判断`$file`是否存在以及`$file['error']`是否等于`UPLOAD_ERR_OK`。 3. 如果文件上传成功,继续对文件进行处理。可以先定义一个数组来保存可能遇到的错误信息,如`$errors = array()`。 4. 判断文件大小是否符合要求,可以通过`$file['size']`与指定的最大文件大小进行比较。如果超过最大值,将错误信息添加到`$errors`数组中。 5. 判断文件的类型是否符合要求,可以通过`$file['type']`与指定的允许类型进行比较。如果不在允许的类型范围内,将错误信息添加到`$errors`数组中。 6. 如果上述验证都通过,可以将文件移动到指定的上传目录。可以使用`move_uploaded_file`函数来实现文件的移动,其中第一个参数为`$file['tmp_name']`表示临时存储的路径,第二个参数为指定的上传目录和文件名。 7. 最后,函数应该返回一个数组,包含两个元素。一个是表示操作是否成功的`success`,可以通过检查`$errors`数组是否为空来判断。另一个是保存错误信息的`errors`数组,如果有错误的话。 整个函数的代码如下: ``` function uploadFile($file) { if (isset($file) && $file['error'] === UPLOAD_ERR_OK) { $errors = array(); // 验证文件大小 $maxSize = 10 * 1024 * 1024; // 假设最大文件大小为10MB if ($file['size'] > $maxSize) { $errors[] = '文件大小超过限制'; } // 验证文件类型 $allowedTypes = array('image/jpeg', 'image/png'); // 假设只允许上传JPEG和PNG图片 if (!in_array($file['type'], $allowedTypes)) { $errors[] = '文件类型不被允许'; } // 将文件移动到上传目录 $uploadDirectory = 'uploads/'; // 假设上传目录为当前目录下的uploads文件夹 $destination = $uploadDirectory . $file['name']; move_uploaded_file($file['tmp_name'], $destination); return array( 'success' => empty($errors), 'errors' => $errors ); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值