-1

I have a very simple function to read URL image file and save it on server.

function o99_download_shot_binary(){

        $url = esc_url_raw( o99_get_content_link() ); // function returns http:// url
        $img = $uploads['basedir'] . '/tmp/' . uniqid() . '.jpeg'; // name and location
        file_put_contents($img, file_get_contents($url)); 
    }

IT works ok, but sometimes , o99_get_content_link() returns an invalid URL, so an empty image is saved , so I made a simple check with if statement and separated the one liner:

function o99_download_shot_binary(){

    $url = esc_url_raw( o99_get_content_link() ); // function returns http:// url
    $img = $uploads['basedir'] . '/tmp/' . uniqid() . '.jpeg'; // name and location
    $file = file_get_contents($url);
     if (file_exists($file)) {
        file_put_contents($img, $file); 
     }
}

But doing so, NO image is saved .

So I tried to change the if statement to

if ( $file) { file_put_contents($img, $file); }

..and now it works .

As everything is OK now, you might be wondering why did I posted this question , well, since I want also to UNDERSTAND what I am doing - My Question is simply :

why does the file_exists($file) check fails ?

I assumed to myself (maybe wrongly) that it is because the file_exists() only checks LOCAL filesystem. If this is the case , then is there a dedicated function to check the existence over URL ? or any other better solutions for future references ?

EDIT I (as per comments request )

Var_dump() is a binary string :

string(51238) "����JFIF��������BZ�|su��{��3�_�R��[J��z���K�^��g�/��9���|*�F��W~�>tχ�>��� �&��?�~0�f�����[��i���R��_���g~�_��Ɵ�#����.5�x+�_ u��-�ٿ�L����ƺV��  ... ... ... 

But this is predictable, like I said in original question, the file IS being saved OK, it is just a question of WHY the file_exists() check fails, and if there is a better way to perform that check on a binary remote file from URL...

Obmerk Kronen
  • 15,619
  • 16
  • 66
  • 105
  • Can you add the output of `var_dump($file)` right after `$file = file_get_contents($url);` ? – hek2mgl Mar 08 '13 at 02:25
  • 2
    `file_exists()` is expecting a path like `/home/user/foo.txt` or `c:\foo.txt`. You are supplying it with the contents of the file, not the local path to the file... indeed, the file is not yet stored on the local system. – Jeremy Holovacs Mar 08 '13 at 02:33
  • Like I wrote in the original question, this is what I suspected . But is there some other practice I should ideally perform, or the last check I made is enough for remote files ? – Obmerk Kronen Mar 08 '13 at 02:36
  • Well you could always just see if `$file` has data in it, I suppose? If it does, *something* came back. – Jeremy Holovacs Mar 08 '13 at 02:37
  • yes, but this "SOMETHING" can be anything , even just a header or an error .. – Obmerk Kronen Mar 08 '13 at 02:39
  • 1
    @ObmerkKronen this is certainly true. It depends on how seriously you need to take it. If this is important data, you will probably want to wrap some error-checking around this. The statements are fairly agnostic to what comes back, they are expecting you to know what to do with them. – Jeremy Holovacs Mar 08 '13 at 02:42
  • @ObmerkKronen Get some sleep and read your question again, tomorrow. – hek2mgl Mar 08 '13 at 02:50
  • @hek2mgl - Thanks again (this time for voting down AND deleting your answer and my comments ), and I WILL try to apply and address your recommendations on my sleep regime . But it is hardly the way to go . I really DO appreciate any help that is given, but taking things personally like that is ,IMHO, just a bit below the scope of this community. I asked a simple question, and as far as you tried to help, it provided no real answer to the question ITSELF which is WHY the function fails. – Obmerk Kronen Mar 08 '13 at 02:57
  • @hek2mgl - ..Voting down a perfectly good question and deleting answers to it harms the whole COMMUNITY as it harms future references and future similar questions. It is not supposed to be a personal thing. I do not need to read my own question again. I know what I asked, but unfortunately I can not know, nor be responsible for what YOU understood out of it . And in any rate, your answer HAD value, because in other cases , it might be the solution for future users .. – Obmerk Kronen Mar 08 '13 at 03:00
  • Hey take it not personal. I just meant, after some sleep things will get more clear. (It was meant as a good advice, I would gave myself in such a situation too). Undeleted answer. Think it is not useful as you not understand it – hek2mgl Mar 08 '13 at 03:00
  • 1
    ??? What is the mystery? You asked PHP to look for a file that didn't exist, because you hadn't saved it yet, and you had given it a bogus path name. Of course it said it didn't exist. – Jeremy Holovacs Mar 08 '13 at 03:01
  • @JeremyHolovacs Maybe you can find a way to show her(?) the simplicity :) – hek2mgl Mar 08 '13 at 03:02
  • @ObmerkKronen Again, you just used the wrong variable as arguments to the wrong functions.. Nothing magic.. Just another hopefully useful tip from my side as you asked this. file_exists() will work with remote files as ftp or http. You'll just have to pass an url to it and not the files contents of something – hek2mgl Mar 08 '13 at 03:05
  • @Jeremy Holovacs I understood that , and I also suspected that as i wrote in the Original question. Funny though that NO ONE mentioned or confirmed it as a certainty in a definitive ANSWER. hek2mgl ´s answer about the cache seems to me totally irrelevant, and frankly your first comment was the only reasonable answer here (which you should have posted as an answer, because it is the only right one ),. – Obmerk Kronen Mar 08 '13 at 03:06
  • 1
    @ObmerkKronen, well for everyone's viewing pleasure I have done as you suggested. :) – Jeremy Holovacs Mar 08 '13 at 03:11
  • @Jeremy Holovacs ..and I have accepted. Things can be much simpler sometimes ..(file_exists() simply can not check for remote URL file, only LOCAL file... – Obmerk Kronen Mar 08 '13 at 03:13

2 Answers2

2

$file = file_get_contents($url) means that $file contains a string, the contents of the file. Unless the file's contents happens to be a path to a file that exists, you won't save anything.

You want to check if( file_exists($url)) and keep the one-liner you had before.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • Thanks for the answer, but even though I understand the logic of your answer - it seems to fail just the same .. – Obmerk Kronen Mar 08 '13 at 02:25
  • yes, Like I said in the original question, this is what I suspected , so the rest of the question is : what else can I do except what I already did ? Is there a better alternative ? practice ? – Obmerk Kronen Mar 08 '13 at 02:34
  • 1
    I think what you have may be the best option. The alternative would be using socket functions or an extension like cURL to check the actual HTTP response. – Niet the Dark Absol Mar 08 '13 at 02:35
  • Thanks ..You are right . ( I wonder why it was just voted down, and a long answer with comments was just deleted.) – Obmerk Kronen Mar 08 '13 at 02:52
1

file_exists() expects a path, like /home/user/foo.txt, or C:\foo.txt. You are supplying it with the contents of the file, not the local path to the file, so it will not return a true value (unless, of course, the contents of the file are a valid file path to an existing file... not to muddy the waters but it had to be said :) ).

Jeremy Holovacs
  • 22,480
  • 33
  • 117
  • 254