0

I'm working on Wordpress at the moment and as simple as it's sounds i'm trying to check if the image is there in the directory if not then it will show a standard no-image.

My problem is that the file_exists is returning false although the url is correct inside it, and it's accessible via browser so it's not a permission issue, and maybe i'm doing it wrong, here's the code;

$img = 'no_img.jpg';
if($val->has_prop('user_image')){
$img_tmp = $val->get( 'user_image' );
        if(file_exists($upload_dir['baseurl'].'/users/'.$img_tmp)){
$img = $val->get( 'user_image' );
    }
}

if i do var_dump($upload_dir['baseurl'].'/users/'.$img_tmp); it will show the exact direct URL to the file, and it's correct one, but when it enters the file_exists it returns $img = 'no_img.jpg' although the file exists in the directory.... What am i doing wrong??

I tried also to add clearstatcache(); before the file_exists but didn't work also. Any ideas?

Liza
  • 298
  • 3
  • 13
  • What's in `$upload_dir["baseurl"]`? – Maerlyn Dec 12 '13 at 09:12
  • 6
    `file_exists` works with path of file, not url. – Narek Dec 12 '13 at 09:14
  • @Maerlyn http://codex.wordpress.org/Function_Reference/wp_upload_dir , it return the URL of the upload directory in WP – Liza Dec 12 '13 at 09:14
  • 1
    is "direct URI" actually relative or absolute? You might be assuming the wrong *working directory* – Aurelia Dec 12 '13 at 09:14
  • When i did that `var_dump` it will return `http://www.domain.com/wp-content/uploads/users/file.jpg`, it's a full path to the file image... – Liza Dec 12 '13 at 09:15
  • 1
    You should try to use [getimagesize](http://stackoverflow.com/questions/14970322/check-if-image-exists-php). – Rahil Wazir Dec 12 '13 at 09:15
  • 2
    @Liza, no it's **url** of file full path will be somethisg like `/home/user/workspace/folder/users/imp.png` OR `C://path/to/folder/img.png` if you use Windows – Narek Dec 12 '13 at 09:17
  • 1
    @Liza you must use a path, not a URI, like `wp-content/uploads/users/file.jpg` if you're using file_exists from the WP root – Aurelia Dec 12 '13 at 09:18
  • 1
    you can use `$fullPath=$upload_dir['baseurl'].'/users/'.$img_tmp; if(@fopen($fullPath,"r"))` – Nouphal.M Dec 12 '13 at 09:23
  • 1
    Check `@file_get_contents($upload_dir['baseurl'].'/users/'.$img_tmp, null, null, 0, 1);` to see if the path is correct and the file actually exists. If it returns anything, it is OK. – davidkonrad Dec 12 '13 at 09:26
  • Thank you all for the comments, i changed to @Nouphal.M method of `@fopen` – Liza Dec 12 '13 at 09:28
  • This question appears to be off-topic because it is about Wordpress – SomeKittens Mar 01 '14 at 00:30

4 Answers4

3

Try to use:

if( getimagesize($upload_dir['baseurl'].'/users/'.$img_tmp) !== false ){
    $img = $val->get( 'user_image' );
}
else {
    $img = $val->get( 'no_image' );
}

Also refer to the doc getimagesize

Rahil Wazir
  • 10,007
  • 11
  • 42
  • 64
1

Or as mentioned in comments, try sniff instead :

if (@file_get_contents($upload_dir['baseurl'].'/users/'.$img_tmp, null, null, 0, 1)) {
    //ok
}
davidkonrad
  • 83,997
  • 17
  • 205
  • 265
  • That seems like a bad practice. Reminds me of fixing WP RSS feeds with linebreaks at the top (due to bad plugins) with CURLand trim (that actually happened) – Aurelia Dec 12 '13 at 09:36
1

you can use

$fullPath=$upload_dir['baseurl'].'/users/'.$img_tmp; 

if(@fopen($fullPath,"r")){
     ........
 }
Nouphal.M
  • 6,304
  • 1
  • 17
  • 28
1

You need to use $upload_dir['basedir'] instead of baseurl if you used $upload_dir = wp_upload_dir(); to determine the upload path of your WP installation.

or you can use file_get_contents, cURL, or fopen to sniff if image exists for the url.

Sovit Tamrakar
  • 331
  • 1
  • 3