php - 我怎样才能有效地添加图像处理得到这个脚本

标签 php mysql forms upload

希望有人可以帮助我。整晚都在浏览,我尝试的任何方法似乎都不起作用,但我对 php 很陌生,所以速度很慢。我需要上传 6 张图片,这效果很好。但后来我意识到您不仅可以上传图像,还可以上传所有其他文件类型。我试图将其限制为每个图像小于 100kb。嘿嘿嘿嘿嘿嘿嘿嘿嘿嘿!!!请!

function findexts ($filename) { $filename = strtolower('$filename') ; 
$exts = preg_split("[/\\.]", $filename) ;
 $n = count($exts)-1; 
 $exts = $exts[$n];
  return $exts;
   }

$ext = findexts ($_FILES['images']['name']) ; 
$ran = rand ();
$ran2 = $ran.".";

while(list($key,$value) = each($_FILES['images']['name']))
        {
            if(!empty($value))
            {
                $filename = $ran.$value;
                    $filename=str_replace(" "," _ ",$filename);// Add _ inplace of blank space in file name, you can remove this line

                    $add = "media/".$ran."$filename";
                $insert_query = "INSERT INTO ....VALUES ...";
                       //echo $_FILES['images']['type'][$key];
                 // echo "<br>";
                    copy($_FILES['images']['tmp_name'][$key], $add);
                    chmod("$add",0777);

        mysql_query($insert_query);

            }

        }

最佳答案

在此处查看您的两个问题的答案:

https://stackoverflow.com/a/9153419/723855

将此函数添加到您的脚本中(从链接修改):

function acceptFileUpload($thefile){
    if(isset($_FILES[$thefile])) {
        $errors     = array();
        $maxsize    = 2097152;
        $acceptable = array(
            'application/pdf',
            'image/jpeg',
            'image/jpg',
            'image/gif',
            'image/png'
        );

        if(($_FILES[$thefile]['size'] >= $maxsize) || ($_FILES[$thefile]["size"] == 0)) {
            $errors[] = 'File too large. File must be less than 2 megabytes.';
        }

        if(!in_array($_FILES[$thefile]['type'], $acceptable)) && (!empty($_FILES[$thefile]["type"]))) {
            $errors[] = 'Invalid file type. Only PDF, JPG, GIF and PNG types are accepted.';
        }

        if(count($errors) !== 0) {
            return true;
        } else {
            foreach($errors as $error) {
                echo '<script>alert("'.$error.'");</script>';
                return false;
            }

            die(); //Ensure no more processing is done
        }
    }
}

然后在脚本中更改 while 循环以使用此函数检查有效文件:

while(list($key,$value) = each($_FILES['images']['name']))
    {
        if(!empty($value))
        {
            if(acceptFileUpload('images'))
            {
            $filename = $ran.$value;
                $filename=str_replace(" "," _ ",$filename);// Add _ inplace of blank space in file name, you can remove this line

                $add = "media/".$ran."$filename";
            $insert_query = "INSERT INTO ....VALUES ...";
                   //echo $_FILES['images']['type'][$key];
             // echo "<br>";
                copy($_FILES['images']['tmp_name'][$key], $add);
                chmod("$add",0777);

    mysql_query($insert_query);
            }
        }

    }

我可能没有正确传递给acceptFileUpload() 的参数。

关于php - 我怎样才能有效地添加图像处理得到这个脚本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10175226/

相关文章:

mysql - 定位文本位置,提取文本并插入 MySQL 中的新列

mysql - Spark框架中使用Scala IDE从Mysql数据库加载数据

c# - 从 Input 控件获取数据到代码隐藏

javascript - 如何在页面访问时自动提交 Rails 表单

PHP 错误 : php_network_getaddresses: getaddrinfo failed: (while getting information from other site.)

java - Sugar ORM 如何将数据添加到表中的一行

php - 脚本太慢如何避免多次运行相同的查询?

python - Django 表单自定义 : change field on other field change

php - Symfony2 可重用的代码片段函数

javascript - 无法检查从 json 编码的 php 数组准备的 javascript json 中的对象值