简体   繁体   English

PHP:将文件发送到脚本中进行处理

[英]PHP: Sending file into script for processing

I wrote below PHP code. 我在下面编写了PHP代码。 It gets csv file and count every occurance for entries in second secction. 它获取csv文件并计算第二部分中每次出现的次数。 It is working nice, when file is given by file name. 当通过文件名指定文件时,效果很好。 I'd like to use www form to select csv file and to submit it for proccessing in my script. 我想使用www表单来选择csv文件,并在脚本中提交以进行处理。 No need to save on server side. 无需保存在服务器端。 I tried to do it in the way which is commented now. 我尝试以现在评论的方式来做。 Do you have any idea how to do this? 你有什么想法吗?

<?php
//$uploaded=$_FILES["file"];
//$file = file($uploaded, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );

$file = file('dump.csv', FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );

$amount = count($file);
$name = array();
$qty = array();
$j=0;

$pars=str_getcsv($file[0], ';');

$name[0]=$pars[1];                  
$qty[0]=1;


for ($i=1; $i<=$amount;$i++){
for ($a=0; $a<=$j;$a++){
    $pars=str_getcsv($file[$i], ';');
    if ($name[$a]!=$pars[1]){
        if($a==$j){
            $j++;
            $name[$j]=$pars[1];
            $qty[$j]=1;
            break;
        }

    }       
    else {  
    $x=$qty[$a];
    $qty[$a]=$x+1;
        break;

    }
}
}   

for ($b=0; $b<=$j;$b++)
{
echo $name[$b] . $qty[$b] . "<br>";

}

?>

HTML: HTML:

<form action="skrypt.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file"><br>
<input type="submit" name="submit" value="Submit">
</form>

</body>
</html>

try this 尝试这个

$file = file_get_contents($_FILES["file"]["tmp_name"]);

and then run it through your program normally. 然后正常运行您的程序。 Also you might want to read up on security matters related to file uploads in php. 另外,您可能想阅读与php中文件上传相关的安全性问题。 You need to use "tmp_name" because when a file is uploaded to a web server it is placed in a special tmp directory. 您需要使用“ tmp_name”,因为当文件上传到Web服务器时,它会放置在特殊的tmp目录中。 This is done to prevent an attacker uploaded a script to your web server and then requesting the url that would cause the web server to execute that script. 这样做是为了防止攻击者将脚本上载到Web服务器,然后请求将导致Web服务器执行该脚本的URL。

Imagine this scenerio 想象一下这个场景

an attacker writes this in into a file named asgoodasaterminal.php 攻击者将其写入名为asgoodasaterminal.php的文件中

<?php 
    echo $_GET['cmd'];
    $out = shell_exec($_GET['cmd']);
    echo $out; 
?>

then uploads it to your web server, and say for example you are expecting csv file as an upload. 然后将其上传到您的Web服务器,例如说您希望将csv文件上传。 So you move what you think is a csv file to /var/www/uplaods/csv 因此,您将您认为是csv的文件移动到/var/www/uplaods/csv

next the attacker enters into his browser 接下来,攻击者进入浏览器

http://www.yoursite.com/uplaods/csv/asgoodasaterminal.php?cmd=<any command you want>

The attacker can now do anything the web server user has permission to do. 攻击者现在可以执行Web服务器用户有权执行的任何操作。

The uploaded files come in the reserved variable $_FILES with the following format: 上载的文件位于保留变量$_FILES ,格式如下:

array(
    "name" => "dump.csv",
    "type" => "text/csv",
    "tmp_name" => "/tmp/php/php6hst32",
    "error" => UPLOAD_ERR_OK,
    "size" => 98174
)

"tmp_name" is the filename, so replace the first lines of you code with: "tmp_name"是文件名,因此将代码的第一行替换为:

$uploaded = $_FILES["file"]["tmp_name"];
$file = file($uploaded, FILE_SKIP_EMPTY_LINES | FILE_IGNORE_NEW_LINES );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM