Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/238.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 文件导入安全问题_Php_Security_File Upload - Fatal编程技术网

Php 文件导入安全问题

Php 文件导入安全问题,php,security,file-upload,Php,Security,File Upload,我有一个问题 当我们向web用户提供将数据导入mysql表的选项时,这是否安全 比如说 <form method="post" action="import.php" enctype="multipart/form-data"> <input id="file1" name="file1" type="file"> <input type="submit" name="button" id="button" value="Submit" >

我有一个问题

当我们向web用户提供将数据导入mysql表的选项时,这是否安全

比如说

    <form method="post" action="import.php" enctype="multipart/form-data">

    <input id="file1" name="file1" type="file">

 <input type="submit" name="button" id="button" value="Submit" >
    </form> 

在import.php中,我们有以下代码

        <?php
       $theFile = $_FILES['file1'];
         $tmp_name1 = $theFile['tmp_name'];
        $row = 1;
        if (($handle = fopen($tmp_name1, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
         $num = count($data);

        // SQL insert statement
        }
        fclose($handle);
        }

该代码中的任何内容都不会执行文件,因此该部分不会出现问题。至于文件提取后是否会出现问题,这是另一个问题。

只要您不执行任何文件,并且不将其移动到可从外部访问的位置(即网站中的文件夹),则无论文件包含什么,都不会有安全问题

你只需要非常小心,当你对文件做你想做的事情时,不要相信任何以这种方式出现的文件。例如,永远不要执行它们。切勿将它们放在可以执行它们的位置(例如,不安全下载目录中的
.php
文件)

否则,这实际上取决于您想对文件做什么。在标准的Linux/Apache/PHP设置中没有快速简单的病毒检查解决方案


要全面了解如何使文件上传尽可能安全,请查看对它的回复,尤其是bobince回复中的链接

不安全。至少您需要验证该文件是否确实是上载的文件,而不是服务器上已有的文件,如/etc/passwd。为此,您需要使用
is\u uploaded\u file()

例如:

<?php
if (is_uploaded_file($_FILES['file1']['tmp_name'])) {
    $tmp_name1 = $_FILES['file1']['tmp_name'];
    if (($handle = fopen($tmp_name1, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            $num = count($data);

            // SQL insert statement
        }
    fclose($handle);
    }
}

您还应该重命名上载到服务器的任何文件,因为保持文件名不变可能会导致远程文件攻击,即有人在您的服务器上执行该文件

最后,如果文件上传只接受特定的文件类型,比如图像,那么您应该明确检查以确保文件实际上是图像。至少要检查文件扩展名,以确保它是.png、.gif、.jpg等。如果它是.exe,则立即拒绝它,因为它显然不是图像,因此对您没有任何用处

<?php
    if (is_uploaded_file($_FILES['file1']['tmp_name'])) {

        $allowedExtensions = array("txt","csv","htm","html","xml","css","doc","xls","rtf","ppt","pdf","swf","flv","avi","wmv","mov","jpg","jpeg","gif","png"); 
        if (!in_array(end(explode(".", strtolower($_FILES['file1']['name']))), $allowedExtensions)) { 
            // Bad file type. Error!  
        }
        else {
            $tmp_name1 = $_FILES['file1']['tmp_name'];
            if (($handle = fopen($tmp_name1, "r")) !== FALSE) {
                while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
                    $num = count($data);

                    // SQL insert statement
                }
                fclose($handle);
            }
        }
    }
?>