Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ajax/6.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上传安全-防止用户上传无限文件-使用ajax上传表单_Php_Ajax_Forms_Security_Upload - Fatal编程技术网

PHP上传安全-防止用户上传无限文件-使用ajax上传表单

PHP上传安全-防止用户上传无限文件-使用ajax上传表单,php,ajax,forms,security,upload,Php,Ajax,Forms,Security,Upload,编辑2:我注意到用户可以上传无限的文件,可以占用所有的磁盘空间,如何防止? 编辑:因为没有人回答这个问题,有没有一个来源我可以阅读得到我的答案??? 我有一张联系表。有三种输入。我使用jQuery插件上传文件。插件添加另一个表单元素并通过ajax上传文件。 我是一个初学者,但这段代码是为客户和真正的工作,所以我想确保它的安全 在我看来: <form action="" method="post" enctype="multipart/form-data" > <input


编辑2:我注意到用户可以上传无限的文件,可以占用所有的磁盘空间,如何防止?
编辑:因为没有人回答这个问题,有没有一个来源我可以阅读得到我的答案???

我有一张联系表。有三种输入。我使用jQuery插件上传文件。插件添加另一个表单元素并通过ajax上传文件。
我是一个初学者,但这段代码是为客户和真正的工作,所以我想确保它的安全

在我看来:

<form action="" method="post" enctype="multipart/form-data" >
<input type="text" name="name"  />
<input type="number" name="phone" />
<textarea name="enquiry" rows="10" ></textarea>
<div id="upload-div">
<div id="extraupload">Upload</div>
<input type="hidden" name="count" value="0" id="count"/>
<input type="submit" />
将文件名更改为唯一哈希后,我将其保存在tmp文件夹中

然后,当提交主表单时,会发生以下情况:

//validation method: if that file exists in tmp folder  
if(isset($this->request->post['count']) && is_numeric($this->request->post['count'])) {
    for($i=1; $i<=$this->request->post['count']; $i++ ) {
        if(isset($this->request->post['file_'.$i])){
            if(!file_exists('image/tmp/'.$this->request->post['file_'.$i])){
                //throw error
            }
        } else{
            //throw error
        }               
    }
}
// hidden input count can only be integer
if(isset($this->request->post['count']) && !is_numeric($this->request->post['count'])) {
    //throw error
}
//验证方法:如果tmp文件夹中存在该文件
if(设置($this->request->post['count'])和&is_数值($this->request->post['count'])){
对于($i=1;$irequest->post['count'];$i++){
if(设置($this->request->post['file.'$i])){
如果(!file_存在('image/tmp/.$this->request->post['file_.$i])){
//抛出错误
}
}否则{
//抛出错误
}               
}
}
//隐藏输入计数只能是整数
如果(设置($this->request->post['count'])&&!是数值($this->request->post['count'])){
//抛出错误
}
然后邮寄文件并将文件名保存在数据库中(我没有包括数据库部分,因为我有点确定它是可以的)

//每次提交都会删除tmp文件夹中早于1天的文件
$oldFiles=glob($tmp_dir.“*”);
$now=时间();
foreach($oldFiles作为$oldFile){
如果(是_文件($oldFile)){
如果($now-filemtime($oldFile)>=60*60*24){
取消链接($oldFile);
}
}
}
$mail=新邮件();
//已删除邮件设置和详细信息
//如果有任何文件上传
如果($this->request->post['count']!=0){
//每个表单提交的唯一目录
$dir_path='image/submitted/'.uniqid();
mkdir($dir_path,0764,true);
//对于所有隐藏输入,将文件从tmp文件夹移动到$dir_路径
对于($i=1;$i请求->发布['count'];$i++){
$file=$this->request->post['file.'$i];
重命名('image/tmp'.$file,$dir_path./'.$file);
$mail->AddAttachment($dir_path.'/'.$file);
}
}
$mail->send();
现在我的问题是:这样安全吗?特别是当我用文件名附加隐藏输入,并从隐藏输入计数中获取上载文件的数量??
此代码已经运行,但我认为这可能是一个安全问题。
非常感谢你的耐心,也很抱歉我的英语不好!

ps:我使用opencart有一个普遍的误解,即在AJAX应用程序中更安全,因为人们认为用户在没有呈现的用户界面(基于AJAX的网页)的情况下无法访问服务器端脚本。基于XML HTTP请求的web应用程序模糊了服务器端脚本,这种模糊性给网站开发人员和所有者带来了错误的安全感——模糊性不是安全性。由于XML HTTP请求使用与web上所有其他应用程序(HTTP)相同的协议运行,从技术上讲,基于AJAX的web应用程序容易受到与“普通”应用程序相同的黑客攻击方法的攻击。

谢谢您的回答,那么我该如何确保它的安全?当我加入编辑一个用户可以上传无限的文件!我建议你找到线索,希望这一条能帮助你
if ($_FILES['file']['type']=='image/jpeg' || $_FILES['file']['type']=='image/pjpeg') { 
    $ext = '.jpg';
}
elseif ($_FILES['file']['type']=='image/png') { 
    $ext = '.png'; 
}
elseif ($_FILES['file']['type']=='application/pdf') { 
    $ext = '.pdf'; 
}
else {
    echo json_encode('Only images and pdf files are allowed!');
    die();
}
$fileName = md5(uniqid());
$fileName = $fileName.$ext;
move_uploaded_file($_FILES["file"]["tmp_name"], 'image/tmp'.$fileName); 
$result = array('status'=> 'success','files' => $fileName);
echo json_encode($result);
//validation method: if that file exists in tmp folder  
if(isset($this->request->post['count']) && is_numeric($this->request->post['count'])) {
    for($i=1; $i<=$this->request->post['count']; $i++ ) {
        if(isset($this->request->post['file_'.$i])){
            if(!file_exists('image/tmp/'.$this->request->post['file_'.$i])){
                //throw error
            }
        } else{
            //throw error
        }               
    }
}
// hidden input count can only be integer
if(isset($this->request->post['count']) && !is_numeric($this->request->post['count'])) {
    //throw error
}
//by every submition delete files in tmp folder older than 1 day
$oldFiles = glob($tmp_dir."*");
$now   = time();

foreach ($oldFiles as $oldFile) {
    if (is_file($oldFile)) {
        if ($now - filemtime($oldFile) >= 60 * 60 * 24) { 
            unlink($oldFile);
        }
    }
}

$mail = new Mail();
//Mail Setting and details deleted

//if there's any file uploaded
if($this->request->post['count'] != 0) {
    //unique directory for every form submition
    $dir_path = 'image/submitted/'.uniqid();
    mkdir($dir_path, 0764, true);               

    //for all hidden inputs move file from tmp folder to $dir_path
    for ($i=1; $i <= $this->request->post['count']; $i++) {
        $file = $this->request->post['file_'.$i];
        rename('image/tmp'.$file, $dir_path.'/'.$file);
        $mail->AddAttachment($dir_path.'/'.$file);
    }
}
$mail->send();