0

Please consider the following code:

$imagePath = "https://s22.postimg.org/3k3o9ly8t/testigo.jpg";
$imagedata = get that image data through curl and store in this variable;
echo strlen($imagedata); // outputs 4699

if(strlen($imagedata) == 4699 ) {
    echo "length is 4699";
}

The above if-condition never becomes true even though the strlen value is 4600. It seems very strange; am I missing anything? I've already tried mb_strlen, but to no avail.

Update: It seems to work for certain images, but not for the following image.


Code:

$strImageURL = "https://s22.postimg.org/3k3o9ly8t/testigo.jpg";

$strImageData = getData($strImageURL);

echo "<br />" . strlen($strImageData);

if(strlen($strImageData) === 4699) {
    echo "true";
}

function getData($strSubmitURL)
{

    $strData = null;

    $ch = curl_init();

    //return parameter
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
    curl_setopt($ch, CURLOPT_TIMEOUT, 140);

    //site name
    curl_setopt($ch, CURLOPT_URL,$strSubmitURL);


    // don' verify ssl host
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER,false);
    $strData = curl_exec ($ch);

    if (!$strData) {
        //die ("cURL error: " . curl_error ($ch) . "\n");
        return '';
    }
    curl_close ($ch);

    unset($ch);

    return $strData;
}
Community
  • 1
  • 1
  • 7
    Given the situation you described, this basically can not be incorrect.. and there's probably a failure somewhere else in your code.. – Evert Sep 10 '13 at 18:43
  • If you are attempting to determine the file size this would be the wrong way to go. For safety use if(strlen($imagedata) === 4) (notice the triple =) – Rottingham Sep 10 '13 at 18:47
  • [This](http://stackoverflow.com/questions/2602612/php-remote-file-size-without-downloading-file) is actually more interesting ... – HamZa Sep 10 '13 at 18:48
  • @Rottingham I already tried that. But still the same. – user2001842 Sep 10 '13 at 18:52
  • @Evert Correct, but that is really not working. Even I simplified things by having a separate test file. I know this was working for me.. used countless of times. Not sure what is going on. May be something to do with the way host configured any php aspect? I will post the exact code in a minute. – user2001842 Sep 10 '13 at 18:54
  • 1
    @user2001842 If you're checking to see an exterior file that you probably may not have control over, have you thought about the fact that the image may have changed in size since? It's a possible scenario if that's the case. – Funk Forty Niner Sep 10 '13 at 18:56
  • I would put in a var dump at each point to see whats really going on rather than speculating. Please see my answer with some working code (not using curl though). – Joeme Sep 10 '13 at 19:02
  • @Joeme Image size not changed.. because the previous line still showing the same length. Also, it seems working for certain images.. Please try with the below image. http://i44.tinypic.com/65mmc7.jpg – user2001842 Sep 10 '13 at 19:06
  • That "image" url gives me a web page with a photo not available message, not an image. Can you post up more of your code then I can test with curl? Any reason for using curl and not `file_get_contents`? have you tried my code below? – Joeme Sep 10 '13 at 19:10
  • Just to note that if you try to get that image (which does not exist) it actually reads `4669 bytes` (of html code) – Ivan Hušnjak Sep 10 '13 at 19:12
  • @Joeme Probably because you need to be "signed in" to `tinypic.com` and/or have a credited API to boot. – Funk Forty Niner Sep 10 '13 at 19:15
  • Are you perhaps trying to find out if image exists/is available? Cause checking the length of server response body might not be a good way of doing that, better check for HTTP headers – Ivan Hušnjak Sep 10 '13 at 19:16
  • @Joeme apologize. Please try this URL: http://s22.postimg.org/3k3o9ly8t/testigo.jpg I didn't try your code, but used a different image and working good. Please try this image. I am posting the code now. – user2001842 Sep 10 '13 at 19:17
  • I would still like to know why it didn't work, the compare should do. I put in a var dump on mine and it works fine: `$strlen = strlen($imagedata); // outputs 1020` `var_dump($strlen);` `if($strlen == 1020 ) { ...` – Joeme Sep 10 '13 at 19:23
  • That works for me too, 4669 (the image is the unavailable image, but its and image nonetheless). I've added some code to my answer to show you. – Joeme Sep 10 '13 at 19:25
  • @joeme I just added the code. Please try it. I tried file_get_contents.. that too not working. Very strange. All other images are working. only this is giving trouble. If you have any trouble in that image link, please copy the image to your pc and then try that php code. I am spending 2 hours with this, not sure whats going on. – user2001842 Sep 10 '13 at 19:29
  • ok if this is it then :-| when i put in a var dump, it gives `["strImageData"]=> string(4669) ` Also, your 5th line down echo's `
    4669` However.... your code has: `if(strlen($strImageData) == 4699) {` Four Six _NINE_ Nine... maybe, change the _NINE_ to a Six !!
    – Joeme Sep 10 '13 at 19:37
  • And now it returns `true ` for me, change the nine to a six dude. – Joeme Sep 10 '13 at 19:40
  • @joeme Thank you! what a silly mistake I made :( – user2001842 Sep 10 '13 at 20:00
  • No problem!! This is why I always find `var_dump` helpful. Please accept my answer below if you have found it works now. – Joeme Sep 10 '13 at 20:53

2 Answers2

3

This works for me:

$url = "http://static.bbci.co.uk/frameworks/barlesque/2.51.4/orb/4/img/bbc-blocks-dark.png";
$imagedata = file_get_contents($url);
//echo strlen($imagedata); // outputs 1020

if(strlen($imagedata) == 1020 ) {

    echo "length is 1020";

}

And as further troubleshooting, I would try a var_dump(get_defined_vars()); at the end of your code and inside the if statement to see whats going on.

//Edit/Update: Using your url, and also putting in a var dump twice:

$url = "http://s22.postimg.org/3k3o9ly8t/testigo.jpg";
$imagedata = file_get_contents($url);
$strlen =  strlen($imagedata); // outputs 4669
var_dump($strlen);

if($strlen == 4669 ) {

    echo "length is 4669 \n";
    var_dump($strlen);
}

returns:

PhpMate running PHP 5.3.15 with (/usr/bin/php)
 >>> untitled

int(4669)
length is 4669 
int(4669)
Joeme
  • 505
  • 1
  • 5
  • 12
-1

strlen() is used to count the bytes that a string equates an one character does not necessarily equal 1 byte in UTF-8

Another issue could be type-casting since PHP has such loose rules about this. Would this work for you?

if((int)strlen($imagedata) == 4600 ) {

    echo "length is 4600";

}

or

if(strlen($imagedata) == '4600' ) {

    echo "length is 4600";

}
MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
  • 3
    casting to `(int)` or using string notation makes no difference to php since `strlen()` can only return integer values – Ivan Hušnjak Sep 10 '13 at 18:51