【问题标题】:Ajax image upload form returns forbidden 403 ErrorAjax 图片上传表单返回禁止 403 错误
【发布时间】:2019-08-06 00:11:30
【问题描述】:

大家好,

我目前正在修改这个预先编写的 ajax/php 图片上传脚本,但经过将近 2 天的尝试找出它为什么会引发 403 禁止错误(您无权访问 /ajaxupload.php 在这台服务器上),我别无选择,只能在这里问大师。

我怀疑这与我的主机服务器设置有关,但在我麻烦他们之前(他们通常需要一天的时间才能得到答案),我想我可能会与你们再次核对一下,以防我失踪因为我是使用 ajax 的新手,我怀疑这是脚本出错的地方。

感谢任何可以提出我做错了什么的人。

干杯,莉亚。

HTML:上传表单:

<form action="http://www.mysite.com/ajaxupload.php" method="post" name="sleeker" id="sleeker" enctype="multipart/form-data">
    <input type="hidden" name="maxSize" value="9999999999" />
    <input type="hidden" name="maxW" value="200" />
    <input type="hidden" name="fullPath" value="http://mysite.com/uploads/" />
    <input type="hidden" name="relPath" value="../uploads/" />
    <input type="hidden" name="colorR" value="255" />
    <input type="hidden" name="colorG" value="255" />
    <input type="hidden" name="colorB" value="255" />
    <input type="hidden" name="maxH" value="300" />
    <input type="hidden" name="filename" value="filename" />
    <p><input type="file" name="filename" onchange="ajaxUpload(this.form,'http://mysite.com/uploader.php?filename=name&amp;maxSize=9999999999&amp;maxW=200&amp;fullPath=http://mysite.com/uploads/&amp;relPath=../uploads/&amp;colorR=255&amp;colorG=255&amp;colorB=255&amp;maxH=300','upload_area','File Uploading Please Wait...&lt;br /&gt;&lt;img src=\'../images/loader_light_blue.gif\' width=\'128\' height=\'15\' border=\'0\' /&gt;','&lt;img src=\'../images/error.gif\' width=\'16\' height=\'16\' border=\'0\' /&gt; Error in Upload, check settings and path info in source code.'); return false;" /></p>
</form>

JS:ajaxupload.js

function $m(theVar){
    return document.getElementById(theVar)
}
function remove(theVar){
    var theParent = theVar.parentNode;
    theParent.removeChild(theVar);
}
function addEvent(obj, evType, fn){
    if(obj.addEventListener)
        obj.addEventListener(evType, fn, true)
    if(obj.attachEvent)
        obj.attachEvent("on"+evType, fn)
}
function removeEvent(obj, type, fn){
    if(obj.detachEvent){
        obj.detachEvent('on'+type, fn);
    }else{
        obj.removeEventListener(type, fn, false);
    }
}
function isWebKit(){
    return RegExp(" AppleWebKit/").test(navigator.userAgent);
}
function ajaxUpload(form,url_action,id_element,html_show_loading,html_error_http){
    var detectWebKit = isWebKit();
    form = typeof(form)=="string"?$m(form):form;
    var erro="";
    if(form==null || typeof(form)=="undefined"){
        erro += "The form of 1st parameter does not exists.\n";
    }else if(form.nodeName.toLowerCase()!="form"){
        erro += "The form of 1st parameter its not a form.\n";
    }
    if($m(id_element)==null){
        erro += "The element of 3rd parameter does not exists.\n";
    }
    if(erro.length>0){
        alert("Error in call ajaxUpload:\n" + erro);
        return;
    }
    var iframe = document.createElement("iframe");
    iframe.setAttribute("id","ajax-temp");
    iframe.setAttribute("name","ajax-temp");
    iframe.setAttribute("width","0");
    iframe.setAttribute("height","0");
    iframe.setAttribute("border","0");
    iframe.setAttribute("style","width: 0; height: 0; border: none;");
    form.parentNode.appendChild(iframe);
    window.frames['ajax-temp'].name="ajax-temp";
    var doUpload = function(){
        removeEvent($m('ajax-temp'),"load", doUpload);
        var cross = "javascript: ";
        cross += "window.parent.$m('"+id_element+"').innerHTML = document.body.innerHTML; void(0);";
        $m(id_element).innerHTML = html_error_http;
        $m('ajax-temp').src = cross;
        if(detectWebKit){
            remove($m('ajax-temp'));
        }else{
            setTimeout(function(){ remove($m('ajax-temp'))}, 250);
        }
    }
    addEvent($m('ajax-temp'),"load", doUpload);
    form.setAttribute("target","ajax-temp");
    form.setAttribute("action",url_action);
    form.setAttribute("method","post");
    form.setAttribute("enctype","multipart/form-data");
    form.setAttribute("encoding","multipart/form-data");
    if(html_show_loading.length > 0){
        $m(id_element).innerHTML = html_show_loading;
    }
    form.submit();
}

PHP:ajaxupload.php

<?php
    function uploadImage($fileName, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH = null){
        $folder = $relPath;
        $maxlimit = $maxSize;
        $allowed_ext = "jpg,jpeg,gif,png,bmp";
        $match = "";
        $filesize = $_FILES[$fileName]['size'];
        if($filesize > 0){  
            $filename = strtolower($_FILES[$fileName]['name']);
            $filename = preg_replace('/\s/', '_', $filename);
            if($filesize < 1){ 
                $errorList[] = "File size is empty.";
            }
            if($filesize > $maxlimit){ 
                $errorList[] = "File size is too big.";
            }
            if(count($errorList)<1){
                $file_ext = preg_split("/\./",$filename);
                $allowed_ext = preg_split("/\,/",$allowed_ext);
                foreach($allowed_ext as $ext){
                    if($ext==end($file_ext)){
                        $match = "1"; // File is allowed
                        $NUM = time();
                        $front_name = substr($file_ext[0], 0, 15);
                        $newfilename = $front_name."_".$NUM.".".end($file_ext);
                        $filetype = end($file_ext);
                        $save = $folder.$newfilename;
                        if(!file_exists($save)){
                            list($width_orig, $height_orig) = getimagesize($_FILES[$fileName]['tmp_name']);
                            if($maxH == null){
                                if($width_orig < $maxW){
                                    $fwidth = $width_orig;
                                }else{
                                    $fwidth = $maxW;
                                }
                                $ratio_orig = $width_orig/$height_orig;
                                $fheight = $fwidth/$ratio_orig;

                                $blank_height = $fheight;
                                $top_offset = 0;

                            }else{
                                if($width_orig <= $maxW && $height_orig <= $maxH){
                                    $fheight = $height_orig;
                                    $fwidth = $width_orig;
                                }else{
                                    if($width_orig > $maxW){
                                        $ratio = ($width_orig / $maxW);
                                        $fwidth = $maxW;
                                        $fheight = ($height_orig / $ratio);
                                        if($fheight > $maxH){
                                            $ratio = ($fheight / $maxH);
                                            $fheight = $maxH;
                                            $fwidth = ($fwidth / $ratio);
                                        }
                                    }
                                    if($height_orig > $maxH){
                                        $ratio = ($height_orig / $maxH);
                                        $fheight = $maxH;
                                        $fwidth = ($width_orig / $ratio);
                                        if($fwidth > $maxW){
                                            $ratio = ($fwidth / $maxW);
                                            $fwidth = $maxW;
                                            $fheight = ($fheight / $ratio);
                                        }
                                    }
                                }
                                if($fheight == 0 || $fwidth == 0 || $height_orig == 0 || $width_orig == 0){
                                    die("FATAL ERROR REPORT ERROR CODE [add-pic-line-67-orig] to <a href='http://www.atwebresults.com'>AT WEB RESULTS</a>");
                                }
                                if($fheight < 45){
                                    $blank_height = 45;
                                    $top_offset = round(($blank_height - $fheight)/2);
                                }else{
                                    $blank_height = $fheight;
                                }
                            }
                            $image_p = imagecreatetruecolor($fwidth, $blank_height);
                            $white = imagecolorallocate($image_p, $colorR, $colorG, $colorB);
                            imagefill($image_p, 0, 0, $white);
                            switch($filetype){
                                case "gif":
                                    $image = @imagecreatefromgif($_FILES[$fileName]['tmp_name']);
                                break;
                                case "jpg":
                                    $image = @imagecreatefromjpeg($_FILES[$fileName]['tmp_name']);
                                break;
                                case "jpeg":
                                    $image = @imagecreatefromjpeg($_FILES[$fileName]['tmp_name']);
                                break;
                                case "png":
                                    $image = @imagecreatefrompng($_FILES[$fileName]['tmp_name']);
                                break;
                            }
                            @imagecopyresampled($image_p, $image, 0, $top_offset, 0, 0, $fwidth, $fheight, $width_orig, $height_orig);
                            switch($filetype){
                                case "gif":
                                    if(!@imagegif($image_p, $save)){
                                        $errorList[]= "PERMISSION DENIED [GIF]";
                                    }
                                break;
                                case "jpg":
                                    if(!@imagejpeg($image_p, $save, 100)){
                                        $errorList[]= "PERMISSION DENIED [JPG]";
                                    }
                                break;
                                case "jpeg":
                                    if(!@imagejpeg($image_p, $save, 100)){
                                        $errorList[]= "PERMISSION DENIED [JPEG]";
                                    }
                                break;
                                case "png":
                                    if(!@imagepng($image_p, $save, 0)){
                                        $errorList[]= "PERMISSION DENIED [PNG]";
                                    }
                                break;
                            }
                            @imagedestroy($filename);
                        }else{
                            $errorList[]= "CANNOT MAKE IMAGE IT ALREADY EXISTS";
                        }   
                    }
                }       
            }
        }else{
            $errorList[]= "NO FILE SELECTED";
        }
        if(!$match){
            $errorList[]= "File type isn't allowed: $filename";
        }
        if(sizeof($errorList) == 0){
            return $fullPath.$newfilename;
        }else{
            $eMessage = array();
            for ($x=0; $x<sizeof($errorList); $x++){
                $eMessage[] = $errorList[$x];
            }
            return $eMessage;
        }
    }

    $filename = strip_tags($_REQUEST['filename']);
    $maxSize = strip_tags($_REQUEST['maxSize']);
    $maxW = strip_tags($_REQUEST['maxW']);
    $fullPath = strip_tags($_REQUEST['fullPath']);
    $relPath = strip_tags($_REQUEST['relPath']);
    $colorR = strip_tags($_REQUEST['colorR']);
    $colorG = strip_tags($_REQUEST['colorG']);
    $colorB = strip_tags($_REQUEST['colorB']);
    $maxH = strip_tags($_REQUEST['maxH']);
    $filesize_image = $_FILES[$filename]['size'];
    if($filesize_image > 0){
        $upload_image = uploadImage($filename, $maxSize, $maxW, $fullPath, $relPath, $colorR, $colorG, $colorB, $maxH);
        if(is_array($upload_image)){
            foreach($upload_image as $key => $value) {
                if($value == "-ERROR-") {
                    unset($upload_image[$key]);
                }
            }
            $document = array_values($upload_image);
            for ($x=0; $x<sizeof($document); $x++){
                $errorList[] = $document[$x];
            }
            $imgUploaded = false;
        }else{
            $imgUploaded = true;
        }
    }else{
        $imgUploaded = false;
        $errorList[] = "File Size Empty";
    }
?>
<?php
    if($imgUploaded){
        echo '<img src="../images/success.gif" width="16" height="16" border="0" style="marin-bottom: -4px;" /> Success!<br /><img src="'.$upload_image.'" border="0" />';
    }else{
        echo '<img src="../images/error.gif" width="16" height="16px" border="0" style="marin-bottom: -3px;" /> Error(s) Found: ';
        foreach($errorList as $value){
                echo $value.', ';
        }
    }
?>

【问题讨论】:

  • 上传脚本需要目标目录的写权限。你仔细检查了吗?
  • 您可以手动浏览到 PHP 文件对吗?
  • @rAyt 是的,上传目录是CHMOD 777,出现同样的问题。 @Chacha102 是的,我可以手动浏览文件。
  • 我这里什么都没有,但我爱上了使用 Flash 上传文件。

标签: php html ajax


【解决方案1】:

确保您的上传目录的文件权限设置为 777 并且在正确的组中。在 Linux 中,这很容易完成

chmod -R 777 /path/to/uploads

要检查他们已经拥有的权限,只需调用它

ls -al

在上面的目录中上传。

大多数 403 Forbidden 是由权限问题引起的。希望这会有所帮助。

【讨论】:

  • 感谢您的回答。是的,文件夹权限设置为777,同样的错误返回。
  • 你完全疯了吗?在网络服务器上的任何目录上设置 777 权限要求被黑客入侵。如果用户在共享主机上托管怎么办?需要一个易受攻击的用户才能让黑客完全访问这个 777 目录。有关更深入的解释,请参阅本文。 askubuntu.com/questions/20105/…
【解决方案2】:
<input type="hidden" name="fullPath" value="http://mysite.com/uploads/" />

POST 中的“http://”值导致服务器限制访问。只需从上面的值中删除“http://”并将其添加到 php 方面,就可以了。

【讨论】:

  • 我遇到了完全相同的问题,这有帮助。
【解决方案3】:

403 禁止错误(您无权访问此服务器上的/ajaxupload.php)

从上面的错误中,您需要检查您的文档根文件夹中的域/url。 检查所有权和读写权限。有时您的网络服务器以普通用户身份运行

例如:www-user

也许您创建了文件,例如。 ajaxupload.php 作为 root 用户(如果它在 unix/linux 环境中)。 文件或文件夹不允许访问。检查 ajaxupload.php 文件和文档根文件夹的所有权和权限。

如果你只是在 linux/unix 环境中进行测试的一种方法

chmod 755 -R /your/doc/root/folder

并尝试触发您的页面。

确定你已经在你的 apache conf 中为你的 doc 根文件夹配置了这个

<Directory "/your/doc/root/folder">
        Options +Indexes FollowSymLinks +ExecCGI
        AllowOverride AuthConfig FileInfo
                Order allow,deny
        Allow from all
</Directory>

如果无法访问 httpd/apache 配置文件,您可以随时使用 .htaccess 文件。

在 /your/doc/root/folder/.htaccess 中创建 .htaccess 文件

在这个文件里面添加如下:

Options +Indexes FollowSymLinks +ExecCGI
Order allow,deny
Allow from all

有关您的系统的更多详细信息将有很大帮助。

【讨论】:

  • 感谢您的回答。我在 unix/linux 环境中运行。未创建的文件是通过 FTP 上传的。所有 CHMOD/权限对于我的服务器配置都是正确的。无论 CHMOD 如何,我仍然会抛出错误 403。再次感谢
  • 如果你已经做了 chmod 755 -R /your/doc/root/folder,另一件要检查的是你的 apache 配置,确保你的 conf 中有这样的东西: Options +Indexes FollowSymLinks +ExecCGI AllowOverride AuthConfig FileInfo Order allow,deny Allow from all
  • 是的,我已经为 /your/doc/root/folder 设置了 CHMOD 755,但仍然抛出相同的错误。另外,我的主机不允许访问 apache 配置。再次感谢
  • 谢谢,但它仍然返回相同的错误。 ggrr,我放弃了,并向主机支持发送电子邮件。感谢您的帮助:)
【解决方案4】:

问题可能是因为您使用的是 www.mysite.com 和 mysite.com...它们可能被视为两个不同的域,并且您在技术上根据您的请求执行跨站点脚本。确保所有调用都使用相同的基本 url...

【讨论】:

    【解决方案5】:

    在onchange代码的表单动作中,在指定ajaxupload.php之前添加http://localhost/

    【讨论】:

      【解决方案6】:

      此错误有时与您服务器上的 mod_security 配置有关。 您可以要求您的主机管理员禁用它。如果可行,请确保您在 mod_security 上的配置与您的代码兼容。

      【讨论】:

        猜你喜欢
        • 2017-12-15
        • 2019-02-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-11-02
        • 2017-03-23
        • 1970-01-01
        • 2012-05-20
        相关资源
        最近更新 更多