Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3403426
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:15:35+00:00 2026-05-18T05:15:35+00:00

I’m trying to write a script, which would cache images, but am stuck with

  • 0

I’m trying to write a script, which would cache images, but am stuck with the following error message:

Nov  4 12:55:19 centos httpd: PHP Fatal error:  curl_setopt() [<a href='function.curl-setopt'>function.curl-setopt</a>]: fopencookie failed in /var/www/html/proxy3.php on line 6

I have prepared a simpler script which still has this problem:

<?php
#phpinfo();
$fh = fopen('/tmp/yahoo.html', 'xb');
if ($fh) {
        $ch = curl_init('http://www.yahoo.com/');
        curl_setopt($ch, CURLOPT_FILE, $fh); # XXX the line 6
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        #curl_setopt($ch, CURLOPT_COOKIEJAR, '/dev/null');
        #curl_setopt($ch, CURLOPT_COOKIEFILE, '/dev/null');
        #curl_setopt($ch, CURLOPT_COOKIEJAR, '/tmp/cookies.txt');
        #curl_setopt($ch, CURLOPT_COOKIEFILE, '/tmp/cookies.txt');
        curl_exec($ch);

        if(!curl_errno($ch)) {
                 $info = curl_getinfo($ch);
                  echo 'Took '.$info['total_time'] .
                    's to send a request to '.$info['url'];
        }
        curl_close($ch);
        fclose($fh);
} else {
        echo 'Can not open /tmp/yahoo.html';
}
?>

In the /tmp dir I then see a zero-sized file:

afarber@centos:html> ls -alZ /tmp/yahoo.html
-rw-r--r--  apache apache user_u:object_r:httpd_tmp_t      /tmp/yahoo.html

Does anybody please have an idea, what is going wrong here?

I’ve tried setting/not setting CURLOPT_COOKIEJAR and CURLOPT_COOKIEFILE – to /dev/null and/or to /tmp/cookies.txt. I’ve tried sudo touch /tmp/cookies.txt; sudo chown apache.apache /tmp/cookies.txt too. This just doesn’t work.

Actually I don’t need cookies in my script, I’d be happy to disable them in curl.

I’m using fopen(… , “xb”) on purpose, so that only 1 script instance will write to the cached file in my real script.

I’m using CentOS 5.5 with php-5.1.6-27.el5 and unmodified php.ini

Thank you,
Alex

P.S. And here is my real image proxy script, which fails with the same fopencookie error message. I can’t use fopen(…., ‘wb’) there, I must use fopen(…. ‘xb’):

<?php

define('MIN_SIZE', 1024);
define('MAX_SIZE', 1024 * 1024);
define('CACHE_DIR', '/var/www/cached_avatars/');

$img = urldecode($_GET['img']);
# URL sanity checks omitted here for brevity
$cached = CACHE_DIR . md5($img);
$writefh = @fopen($cached, 'xb');
# the file is not cached yet, download it!
if ($writefh) {
        $ch = curl_init($img);
        curl_setopt($ch, CURLOPT_FILE, $writefh);
        curl_setopt($ch, CURLOPT_HEADER, FALSE);
        #curl_setopt($ch, CURLOPT_REFERER, $matches[1]);
        curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
        #curl_setopt($ch, CURLOPT_COOKIEJAR, '/dev/null');
        #curl_setopt($ch, CURLOPT_COOKIEFILE, '/dev/null');
        #curl_setopt($ch, CURLOPT_COOKIEJAR, CACHE_DIR . 'cookies.txt');
        #curl_setopt($ch, CURLOPT_COOKIEFILE, CACHE_DIR . 'cookies.txt');
        curl_exec($ch);

        $error    = curl_errno($ch);
        $length   = curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
        $mime     = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
        $is_image = ($mime != NULL &&
                     (stripos($mime, 'image/gif') !== FALSE ||
                      stripos($mime, 'image/png') !== FALSE ||
                      stripos($mime, 'image/jpg') !== FALSE ||
                      stripos($mime, 'image/jpeg') !== FALSE));

        curl_close($ch);
        fclose($writefh);

        if ($error || $length < MIN_SIZE || $length > MAX_SIZE || !$is_image) {
                unlink($cached);
                exit('Download failed: ' . $img);
        }
} else {
        $finfo  = finfo_open(FILEINFO_MIME);
        $mime   = finfo_file($finfo, $cached);
        $length = filesize($cached);
        finfo_close($finfo);
}

$readfh = fopen($cached, 'rb');
if ($readfh) {
        header('Content-Type: ' . $mime);
        header('Content-Length: ' . $length);

        while (!feof($readfh)) {
                $buf = fread($readfh, 8192);
                echo $buf;
        }

        fclose($readfh);

}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:15:35+00:00Added an answer on May 18, 2026 at 5:15 am

    Thanks for all the responses, I’ve ended up with this PHP/cURL-script for caching images (needed by Flash apps to circumvent a missing crossdomain.xml) – seems to work ok with CentOS 5 Linux and php-5.1.6-27.el5:

    <?php
    
    define('MIN_SIZE', 512);
    define('MAX_SIZE', 1024 * 1024);
    define('CACHE_DIR', '/var/www/cached_avatars/');
    
    $img = urldecode($_GET['img']);
    # img sanity checks omitted here
    $cached = CACHE_DIR . md5($img);
    
    if (is_readable($cached)) {
            $finfo  = finfo_open(FILEINFO_MIME);
            $mime   = finfo_file($finfo, $cached);
            $length = filesize($cached);
            finfo_close($finfo);
    } else {
            $writefh = fopen($cached, 'wb');
            if ($writefh) {
                    flock($writefh, LOCK_EX);
                    $ch = curl_init($img);
                    curl_setopt($ch, CURLOPT_FILE, $writefh);
                    curl_setopt($ch, CURLOPT_HEADER, FALSE);
                    curl_setopt($ch, CURLOPT_REFERER, $matches[1]);
                    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
                    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
                    curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
                    curl_exec($ch);
    
                    $error    = curl_errno($ch);
                    $length   = curl_getinfo($ch, CURLINFO_SIZE_DOWNLOAD);
                    $mime     = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);
                    $is_image = ($mime != NULL &&
                                 (stripos($mime, 'image/gif') !== FALSE ||
                                  stripos($mime, 'image/png') !== FALSE ||
                                  stripos($mime, 'image/jpg') !== FALSE ||
                                  stripos($mime, 'image/jpeg') !== FALSE));
    
                    curl_close($ch);
                    fclose($writefh);
                    if ($error || $length < MIN_SIZE || $length > MAX_SIZE || !$is_image) {
                            unlink($cached);
                            exit('Download failed: ' . $img);
                    }
            }
    }
    
    $readfh = fopen($cached, 'rb');
    if ($readfh) {
            header('Content-Type: ' . $mime);
            header('Content-Length: ' . $length);
    
            flock($readfh, LOCK_SH);
    
            while (!feof($readfh)) {
                    $buf = fread($readfh, 8192);
                    echo $buf;
            }
    
            fclose($readfh);
    }
    
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to loop through a bunch of documents I have to put
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a bunch of posts stored in text files formatted in yaml/textile (from
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.