0

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

Sadhon
  • 674
  • 7
  • 11
  • `file_exists()` checks files. A URL is not a file. – Alex Howansky May 09 '17 at 18:36
  • You might be able to use `file_get_contents()` in this situation or maybe take advantage of curl as they both allow use of URLs. `file_get_contents() === false` on error. If you have local access to the file you should use a local file path and basic file operations as they are better performance in the long run. – Mic1780 May 09 '17 at 18:39

3 Answers3

0

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";
}
Barmar
  • 741,623
  • 53
  • 500
  • 612
0

First you need to validate that you pass the name

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

then you can use CURL to check if the url exist

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

this code is from this 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;
}

you can also try from this SO

if (getimagesize($src) !== false) {
        echo "Exists";
    }else{
        echo "Does not exist";
    }
Community
  • 1
  • 1
Ram Segev
  • 2,563
  • 2
  • 12
  • 24
0

file_exists() checks files. A URL is not a file. If the image file you're looking for is on your local server, then you can just use the full path name to it:

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

If the file is on a remote server, and you want to test that the URL referencing it is valid, you might use CURL to perform a HEAD request against it:

$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";
}

I would not recommend doing something like file_get_contents($url) unless you actually need the image contents, because this will actually download the full image and then just throw it away, wasting bandwidth for both you and the remote server.

Alex Howansky
  • 50,515
  • 8
  • 78
  • 98