duanchuang1935 2017-05-09 18:34
浏览 55

在ajax调用的情况下,file_exists($ url)不起作用

I have a file called 'test.php' inside the file i have the following code

 <!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>

My 'images.php' file which executes when ajax called conteains the following code.

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

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

inside the 'images.php' file_exists($url) gives me always wrong answer(Does not exist).. I am sure $url really exits in the given url.. But why it does not work. How i can make it work ... Please help me.. Thanks..

  • 写回答

3条回答 默认 最新

  • duanpuluan0480 2017-05-09 18:46
    关注

    file_exists can't be used with http:// URLs. From the documentation:

    As of PHP 5.0.0, this function can also be used with some URL wrappers. Refer to Supported Protocols and Wrappers to determine which wrappers support stat() family of functionality.

    If you follow that link and then follow the http:// link, it says:

    Supports stat() No

    Instead, you can simply try to open the URL. If it succeeds then the URL exists.

    <?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";
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题