【问题标题】:file_exists($url) does not work in case of ajax callfile_exists($url) 在 ajax 调用的情况下不起作用
【发布时间】:2017-10-08 05:09:37
【问题描述】:

我在文件中有一个名为“test.php”的文件,我有以下代码

 <!DOCTYPE html>
    <html>
    <head>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script>
    $(document).ready(function(){
        $("#btn").click(function(){
            jQuery.ajax({
                url : 'http://example.com/TEST/images.php',
                type : 'POST',
                data : {
                     name : 'black-ring1.gif'
                },
                success : function(data, status) {
                    if(data){
                       alert(data);
                    }
                }


            });
        });
    });
    </script>
    </head>
    <body>

    <p id="btn">Button</p>
    </body>
    </html>

调用 ajax 时执行的我的“images.php”文件包含以下代码。

<?php
$image_name = $_POST['name'];
$src = "http://example.com/images/icons/" . $image_name;

if(file_exists($src)){
    echo "Exists";
}else{
    echo "Does not exist";
}

在 'images.php' file_exists($url) 里面总是给我错误的答案(不存在).. 我确信 $url 确实存在于给定的 url.. 但为什么它不起作用。我怎样才能使它工作...请帮助我..谢谢..

【问题讨论】:

  • file_exists() 检查文件。 URL 不是文件。
  • 在这种情况下您可能可以使用file_get_contents(),或者可以利用 curl,因为它们都允许使用 URL。 file_get_contents() === false 出错。如果您对文件具有本地访问权限,则应使用本地文件路径和基本文件操作,因为从长远来看,它们的性能更好。

标签: php jquery ajax


【解决方案1】:

file_exists 不能与http:// URL 一起使用。来自documentation

从 PHP 5.0.0 开始,这个函数也可以与一些 URL 包装器一起使用。请参阅 Supported Protocols and Wrappers 以确定哪些包装器支持 stat() 系列功能。

如果您点击该链接,然后点击 http:// 链接,它会显示:

支持 stat() 否

相反,您可以简单地尝试打开 URL。如果成功,则 URL 存在。

<?php
$image_name = $_POST['name'];
$src = "http://example.com/images/icons/" . $image_name;

if($fh = fopen($src)){
    echo "Exists";
    close($fh);
}else{
    echo "Does not exist";
}

【讨论】:

    【解决方案2】:

    首先您需要验证您是否传递了名称

    (!empty($_POST["name"])) {
      $image_name = $_POST['name'];
      $src = "http://example.com/images/icons/" . $image_name;
    }else{  
        echo "no, name";
    }
    

    然后你可以使用 CURL 来检查 url 是否存在

    if(is_url_exist($src)){
        echo "Exists";
    }else{
        echo "Does not exist";
    }
    

    此代码来自此SO

    function is_url_exist($url){
        $ch = curl_init($url);    
        curl_setopt($ch, CURLOPT_NOBODY, true);
        curl_exec($ch);
        $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    
        if($code == 200){
           $status = true;
        }else{
          $status = false;
        }
        curl_close($ch);
       return $status;
    }
    

    你也可以试试这个SO

    if (getimagesize($src) !== false) {
            echo "Exists";
        }else{
            echo "Does not exist";
        }
    

    【讨论】:

      【解决方案3】:

      file_exists() 检查文件。 URL 不是文件。如果您要查找的图像文件位于本地服务器上,则只需使用完整路径名即可:

      if (file_exists('/path/to/images/foo.png')) {
          ...
      }
      

      如果文件在远程服务器上,并且您想测试引用它的 URL 是否有效,您可以使用 CURL 对其执行 HEAD 请求:

      $curl = curl_init($url);
      curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'HEAD');
      $result = curl_exec($curl);
      $info = curl_getinfo($curl);
      curl_close($curl);
      if ($info['http_code'] == 200) {
          echo "image exists\n";
      }
      

      除非您确实需要图像内容,否则我不建议您执行 file_get_contents($url) 之类的操作,因为这实际上会下载完整的图像然后将其丢弃,从而浪费您和远程服务器的带宽。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-08-23
        • 1970-01-01
        • 2013-12-09
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多