Download Image

Discussion in 'PHP' started by Chetan Tudor, Jan 6, 2016.

  1. #1
    Hello, I have this file_get_contents("http://www.intend.ro/images/produse/48999.jpg"). It give me: file_get_contents(http://www.intend.ro/images/produse/48999.jpg) [function.file-get-contents]: failed to open stream: Permission denied. I have tried with an image from another site and it worked. I don't really understand why is that happening.
     
    Chetan Tudor, Jan 6, 2016 IP
  2. malky66

    malky66 Acclaimed Member

    Messages:
    3,997
    Likes Received:
    2,248
    Best Answers:
    88
    Trophy Points:
    515
    #2
    The host has probably blocked file_get_contents with URLs because they don't want you stealing their images
     
    Last edited: Jan 6, 2016
    malky66, Jan 6, 2016 IP
    deathshadow likes this.
  3. Chetan Tudor

    Chetan Tudor Greenhorn

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    The problem is I didn't find the directive or the way they were blocked by the admin. The person who built the site doesn't work there anymore and I have to tell to the new staff how to unblock them, so the problem is that I don't know what to tell them to do
     
    Chetan Tudor, Jan 6, 2016 IP
  4. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #4
    Have them look in the server configuration, or the htaccess files.
     
    PoPSiCLe, Jan 6, 2016 IP
    deathshadow likes this.
  5. stats

    stats Well-Known Member

    Messages:
    586
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    110
    #5
    use Curl instead of file_get_contents()
     
    stats, Jan 7, 2016 IP
    deathshadow likes this.
  6. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #6
    Specifically the thing to look for should look something like this if it's in the php.ini file:

    disable_functions = file_get_contents

    ... or in a .htaccess file as:

    php_value disable_functions file_get_contents

    There may be more than just file_get_contents blocked too... which could mean that while @stats suggestion is a good one, that may be blocked as well.

    Generally speaking if such a block is in place it was done for security reasons, such as the codebase being outdated or failing to validate user-side input properly. A code audit may be in order to make sure that re-enabling that doesn't open your site up to being raped by every two bit cracker and script-kiddie out there.
     
    deathshadow, Jan 10, 2016 IP
  7. David@CDNsun

    David@CDNsun Active Member

    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    61
    #7
    I'm surprised that file_get_contents can open an URI :) Never saw that before. I would use PHP implementation of curl, it is build for this.
     
    David@CDNsun, Jan 11, 2016 IP
  8. Chetan Tudor

    Chetan Tudor Greenhorn

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #8
    I don't get something right, maybe I didn't described the problem too good. I am using file_get_contents() from my domain, which is on server X, to download a photo from their domain, which is on server Y. If they have this:"disable_functions = file_get_contents" set in their server's php.ini, doesn't it mean that they won't be able to use the function( from their Y server ) not me( from server X, trying to download something from their server )?
     
    Chetan Tudor, Jan 24, 2016 IP
  9. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,999
    Best Answers:
    253
    Trophy Points:
    515
    #9
    If it's someone else's domain you have no control over that you are trying to pull the file from, they probably have a hotlinking block in place to prevent you from doing just that!

    It's called content theft, and a LOT of sites go to extremes to try and stop it.
     
    deathshadow, Jan 24, 2016 IP
  10. Chetan Tudor

    Chetan Tudor Greenhorn

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #10
    The thing is that they will change what I tell them, in order for this to work, but I don't know what to tell them.
     
    Chetan Tudor, Jan 24, 2016 IP
  11. ThePHPMaster

    ThePHPMaster Well-Known Member

    Messages:
    737
    Likes Received:
    52
    Best Answers:
    33
    Trophy Points:
    150
    #11
    You mis-understood. The issue is from your server, not theirs. Server X must be set to allow file_get_contents to URLs.

    What is confusing me is that you said that it works on other images but not that domain, I have tested it and it works fine on my local server. The only thing I can advise you try is adding headers to mimic actual user, not sure if that would help:

    
    // Create headers to mimic actual user:
    $headers = array ();
    $headers[] = 'User-Agent: Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0';
    $headers[] = 'Accept-Encoding: gzip, deflate';
    $headers[] = 'Accept-Language: en-US,en;q=0.5';
    $headers[] = 'Referer: http://www.intend.ro';
    
    // Organize them for context
    $opts = array(
        'http' => array(
             'method'  =>  'GET',
             'header'  => implode("\r\n", $headers)
        )
    );
    
    // Create a context stream
    $context = stream_context_create($opts);
    
    // Open the file using the HTTP headers set above
    $file = file_get_contents('http://www.intend.ro/images/produse/48999.jpg', false, $context);
    
    PHP:
     
    ThePHPMaster, Jan 24, 2016 IP
  12. Chetan Tudor

    Chetan Tudor Greenhorn

    Messages:
    35
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #12
    I managed to get to the person who built the website and he said that only one domain is allowed access to download photos. I don't know how he did it, but I have run the script from that one domain and it worked. Thanks and sorry for wasting your time.
     
    Chetan Tudor, Jan 26, 2016 IP