mysql

wvyml7n5  于 2021-06-17  发布在  Mysql
关注(0)|答案(3)|浏览(301)

我是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';
}
}

我将感激任何帮助。

slhcrj9b

slhcrj9b1#

首先,获取文件大小(kb),如下所示:

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

然后可以将字节转换为MB,如下所示: $fileSizeInMB = ($fileSize)/(1024*1024) ;
然后您可以检查$filesizeinmb是否大于3。

vm0i2vca

vm0i2vca2#

您可以修改php.ini文件以设置允许上载的最大文件大小。
上载\u最大\u文件大小=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';
    }
    }

    ?>
hgqdbh6s

hgqdbh6s3#

$\u文件是php中的一个全局变量和一个数组元素,包含名为 size ,这是您尝试上载的文件的大小,您可以在此关键元素上添加任何验证。
裁判:http://php.net/manual/en/reserved.variables.files.php

相关问题