【问题标题】:How to validate file size to upload php mysql如何验证文件大小以上传 php mysql
【发布时间】:2019-05-24 10:08:26
【问题描述】:

我是 php + mysql 的新手,我有这个脚本可以将文件上传到服务器并将数据插入数据库。一切正常,只是我不知道如何将文件大小限制为 3MB。代码如下:

// limit file types
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp' , 'pdf' , 'doc' , 'ppt'); // valid extensions
$path = 'uploads/'; // upload directory

if(!empty($_POST['name']) || !empty($_POST['email']) || $_FILES['image'])
{
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];

// get uploaded file extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

// rename file to prevent rewrite existing files
$final_image = rand(1000,1000000).$img;

// check's valid format
if(in_array($ext, $valid_extensions)) 
{ 
$path = $path.strtolower($final_image); 

if(move_uploaded_file($tmp,$path)) 
{
echo "File uploaded succesfully";
$name = $_POST['name'];
$email = $_POST['email'];

//include database configuration file
include_once 'db.php';

//insert form data in the database
$insert = $db->query("INSERT uploading (name,email,file_name) VALUES ('".$name."','".$email."','".$path."')");

}
} 
else 
{
echo 'File not uploaded, try again';
}
}

我会感谢任何帮助。

【问题讨论】:

  • 您可以使用 $_FILES['image']['size'] 或 filesize($path)。

标签: php mysql file-upload


【解决方案1】:

首先,以 KB 为单位获取文件大小:

$fileSize = $_FILES['image']['size'];

然后您可以将字节转换为 MB:

$fileSizeInMB = ($fileSize)/(1024*1024);

然后你可以检查一下这个 $fileSizeInMB 是否大于 3。

【讨论】:

    【解决方案2】:

    您可以修改 php.ini 文件以设置允许上传的最大文件大小。

    upload_max_filesize = 40M

    或者你可以在你的脚本中设置它

    $fileSize = $_FILES['image']['size'];
    

    比使用 if 语句

    if ($fileSize < 3000000) {
                echo "this image cannot be uploaded";
            }
    

    插入到您的脚本中。

       <?php
    
    
        $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp' , 'pdf' , 'doc' , 'ppt'); // valid extensions
        $path = 'uploads/'; // upload directory
    
        if(!empty($_POST['name']) || !empty($_POST['email']) || $_FILES['image'] )
        {
    
    
        $img = $_FILES['image']['name'];
        $tmp = $_FILES['image']['tmp_name'];
        $fileSize = $_FILES['image']['size']
    
        // get uploaded file extension
        $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
    
        // rename file to prevent rewrite existing files
        $final_image = rand(1000,1000000).$img;
    
    
    
        // check's valid format
        if(in_array($ext, $valid_extensions))
        {
        $path = $path.strtolower($final_image);
    
    // if condition to make sure filesize is less than 3000000 bytes, 3MB
        if($fileSize < 3000000){ //php uses bytes so 3000000 is 3MB 
    
    
        if(move_uploaded_file($tmp,$path))
        {
        echo "File uploaded succesfully";
        $name = $_POST['name'];
        $email = $_POST['email'];
    
        //include database configuration file
        include_once 'db.php';
    
        //insert form data in the database
        $insert = $db->query("INSERT uploading (name,email,file_name) VALUES ('".$name."','".$email."','".$path."')");
        }
        } else {
    
        echo 'Cannot upload file too large' ;
        }
        }
    
        else
        {
        echo 'File not uploaded, try again';
        }
        }
    
        ?>
    

    【讨论】:

    • 不知道在脚本里放哪里,能举个例子吗?
    • 感谢您的回复。只有两件事:$fileSize = $_FILES['image']['size'];
    • 抱歉之前的评论...只有两件事: 1. 您忘记了 $filesize 末尾的分号 $fileSize = $_FILES['image']['size']; 2.上传大于6MB的文件时,不出现错误信息。该消息仅针对 3MB 到 6MB 的文件显示。我已将 php.ini 中的 upload_max_filesize = 40M 修改为 40MB,但仍然没有出现该消息。我能做什么?
    • 我只是在upload_max_filesize= 40M中放了一个值作为例子,你需要将它设置为你的用户特定设置,所以如果设置为3MB,它将是upload_max_filesize = 3M
    【解决方案3】:

    $_FILES 是 PHP 中的全局变量和数组元素,包含名为

    的键

    size, 这是您尝试上传的文件的大小,您可以在此关键元素上添加任何验证。

    参考:http://php.net/manual/en/reserved.variables.files.php

    【讨论】:

      猜你喜欢
      • 2011-06-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-12
      • 2012-04-12
      相关资源
      最近更新 更多