Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP GD get image dimensions

Options
  • 17-01-2007 4:14pm
    #1
    Registered Users Posts: 673 ✭✭✭


    Hey,

    Im using the following script on every page of my site:

    list($width, $height) = getimagesize("../phpBB2/images/avatars/gallery/".$userdata."");

    if ($width > 85) {
    $width = 85;
    }
    if ($height > 85) {
    $height = 85;
    }

    This is inserted into each page with an includes function.

    If i open the homepage (index.php) and echo the $width its comes up blank but if i open any of the subdomains (mysite/page2.php) it echo's out the correct $width value. If i try this on my testing server its says "failed to open stream: No such file or directory in" which is referring to the image file.

    I have tried inserting the actual url of the image but that gives back a blank result for $width.

    Anyone know what im doing wrong here?

    Thanks


Comments

  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    I think it's the "../phpBB2" which probably causing the problem. "../" means "The directory below this one"

    Let's say your root directory looks like this:

    /index.php
    /phpBB2/
    /mysite/

    So the "directory below this one", changes, depending on which directory you are in. The root directory is "/"
    So if you try to call
    getimagesize("../phpBB2/images/avatars/gallery/".$userdata."");
    from index.php, you will get an error. First of all because there is no directory below the root directory, but even if there were, there would be no phpBB2 directory in there.

    Calling from any page in the /mysite/ directory will work. This is because the directory below the mysite directory is the root directory, and the phpBB2 directory does exist in the root directory.

    Also remember that include(); includes the file as it were part of the file that called it. So it doesn't matter that the includes file may be in an /includes/ directory, when called from index.php, it will act as if it were in the root directory.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    I thought it had something to do with the ../ but when i put in the whole url of the image e.g. http://www.mysite.com/phpbb2/avatars/1.jpg the $width variable comes up blank. This is where im getting relly confused.


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    Technically "../" means "The directory above this one"

    There may be two issues at stake.......the first is the location of the file (and whether it's included in an include file, as seamus was spot-on on that aspect) but when trying the whole URL you could be running into this issue (a separate one that's giving you the same result

    http://bugs.php.net/bug.php?id=21912

    Try it with just "/phpbb2/avatars/1.jpg" first of all, and let us know how you get on.


  • Registered Users Posts: 673 ✭✭✭Bananna man


    Its definetly to do with the ../ root. On my testing server i am getting the following error:

    When i put in ../ i get the following when i view it from the index.php file.

    Warning: getimagesize(../phpBB2/images/avatars/gallery/gaming/101.bmp): failed to open stream: No such file or directory in d:\website\ps3-leagues.co.uk\ps3-includes\user-login.php on line 30

    But i am also echoing the image again later in the script with exactly the same prefix:

    <div align='center'><img src='../phpBB2/images/avatars/gallery/".$userdata."' width='".$width."' height='".$height."' border='0'>

    and this works.

    When i view it from the subdomains it works fine.

    If i try ./ it does the opposite and works fine on the index.php file but not on the subdomains.

    If i try / on its own it doesnt work on index.php or the subdomains.

    If i try putting in the whole im url on my testing server this works php version 4.3.10 but if i try it on my remote server php version 4.4.4 it doesnt work.

    Hmmmmmm :confused:


  • Registered Users Posts: 68,317 ✭✭✭✭seamus


    Liam Byrne wrote:
    Technically "../" means "The directory above this one"
    Heh. Personal foible of mine. I always see directory trees in my mind upside down. :)
    If i try ./ it does the opposite and works fine on the index.php file but not on the subdomains.
    "./" means "this directory". Which is why it works for index.php and not the other.

    Although I say "/" means the root directory, depending on the setup of the machine, "/" may be the document root of the Web Server, or it may be something else. That is, the root of your web server may be something like

    /users/joebloggs/httpd/wwwroot/

    Try
    <?php
    echo $_SERVER;
    ?>

    And this will tell you the root directory of your web server. Then use this in place of "../" or "./".
    If i try putting in the whole im url on my testing server this works php version 4.3.10 but if i try it on my remote server php version 4.4.4 it doesnt work.
    This is probably due to the setup on the remote machine. Afair, the allow_url_fopen (either in php.ini or at compile time) also determines if you can access images from a URL. Your host most likely will have this disabled.


  • Advertisement
  • Registered Users Posts: 673 ✭✭✭Bananna man


    Nice one, the $_SERVER is working fine now :)


  • Closed Accounts Posts: 18,163 ✭✭✭✭Liam Byrne


    If i try ./ it does the opposite and works fine on the index.php file but not on the subdomains.

    If i try / on its own it doesnt work on index.php or the subdomains.
    Sneaking suspicion that there's a htdocs rewrite in play somewhere......


Advertisement