【问题标题】:How To Set File Size?如何设置文件大小?
【发布时间】:2021-10-06 18:20:00
【问题描述】:

带有此消息 "大小最大为 2 MB"

如何在此操作中插入尺寸验证? 我将制作 2 个上传文件,所以这是我的代码...

<?php 
include 'koneksi.php';
$nama  = $_POST['nama'];
$kelas = $_POST['kelas'];
$alamat = $_POST['alamat'];

$rand1 = rand();
$rand2 = rand();

$allowed = array('pdf');

$filename1 = $_FILES['file1']['name'];
$filename2 = $_FILES['file2']['name'];

$ekstensi1 = pathinfo($filename1, PATHINFO_EXTENSION);
$ekstensi2 = pathinfo($filename2, PATHINFO_EXTENSION);

if($filename1 != "" && in_array($ekstensi1,$allowed)){
    move_uploaded_file($_FILES['file1']['tmp_name'], 'pdf1/'.$rand1.'_'.$filename1);
    $nama_file1 = $rand1.'_'.$filename1;
}else{
    $nama_file1 = "";
}

if($filename2 != "" && in_array($ekstensi2,$allowed)){
    move_uploaded_file($_FILES['file2']['tmp_name'], 'pdf2/'.$rand2.'_'.$filename2);
    $nama_file2 = $rand2.'_'.$filename2;
}else{
    $nama_file2 = "";
}
mysqli_query($koneksi, "insert into siswa values (NULL,'$nama','$kelas','$alamat','$nama_file1','$nama_file2')");
header("location:index.php");

感谢您的帮助

【问题讨论】:

  • $_FILES['file_1']['size'] 将为您提供上传文件的大小。
  • 警告:您对SQL Injections 持开放态度,应该使用参数化的prepared statements,而不是手动构建查询。它们由PDOMySQLi 提供。永远不要相信任何形式的输入!即使您的查询仅由受信任的用户执行,you are still in risk of corrupting your dataEscaping is not enough!
  • @Someone_who_likes_SE 你想分享完整的代码吗,先生:D
  • 如果我的输入是'); DROP TABLE siswa; --怎么办?

标签: php file upload size


【解决方案1】:

如果 (!isset($_GET['id'])) {

        $klasör = "../uploads/post/image";
        $inputname= $_FILES['resim'];
        $tmp_name = $inputname['tmp_name'];
        $name  = $inputname['name'];
        $size  = $inputname['size'];
        $tip  = $inputname['type'];
        $uzantı = substr($name,-4,4);
        $sayı_uret1= rand(10000,50000);
        $sayı_uret2= rand(10000,50000);
        $resim_ad = $sayı_uret1.$sayı_uret2.$uzantı;
        if (strlen($name)==0) {
            echo "<script> Notiflix.Report.Failure( 'Error', 'Empty File Cannot Be Uploaded ', 'Ok', function(){ window.location.href = window.location.href;}); </script>";
            exit();
        }else if ($size> (1024*1024*3)) {
            echo "<script>Notiflix.Report.Failure( 'Error', 'File Size Too Large Cannot Be Uploaded ', 'Ok', function(){ window.location.href = window.location.href;}); </script>";   
            exit();
        }  .......

【讨论】:

    【解决方案2】:

    验证文件大小:

    $_FILES['yourfile']['size'];
    

    假设你有一个 HTML 表单:

    <form action=
                 "process.php" 
          method=
                 "post"
         enctype="multipart/form-data">
    <label>
      Your File:
      <input type="file" name="yourfile" />
    </label>
    <input type="submit" value="send" />
    </form>
    选择文件后您会看到如下内容:

    请注意,如果您选择 2GB 文件,它仍然会到达那里。

    验证

    那么process.php是这样的:

    <?php
    $size = $_FILES['yourfile']['size'];
    $twoMB = 2*1024*1024; // two megabytes is 2*1024*1024 bytes
    if ($size < $twoMB) {
        // yay file is under 2 MB
    } else {
        echo 'oops you need to select a smaller file';
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-17
      • 2016-09-29
      • 1970-01-01
      • 2021-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多