Page 1 of 1

fopen, x mode, curl, and fopencookie error

Posted: Tue Jun 10, 2008 1:46 pm
by tmoswift
Hello Folks,

We recently made a server change FreeBSD-->Linux, and I'm debugging some of my php. One error I found happens when I try to upload an image to a directory. My steps were:
1. fopen, x mode (Create & open with write access, if file does exist return false).
2. Curl an image, and output it to my file.
3. Close the file.

This worked great on my previous server. Now it returns this error:

Code: Select all

Fatal error: curl_setopt() [<a href='function.curl-setopt'>function.curl-setopt</a>]: fopencookie failed in /var/www/bbm/dealsontheweb.com/beta_htdocs/sandbox/curl.php on line 29
I can upload an image when I set the mode to 'a', so it must be some error relating to determining if the file exists or not. I could find any reference to this error on google. Does anyone know what could cause this problem or what changes to look for in the server configuration? I realize I could change my code to use 'a' mode, and check if the file exists beforehand... but now I'm more curious as to why it worked before and now.

Some perhaps relevant configuration info:

Code: Select all

Previous Server:
PHP Version: 5.2.1
--with-curl
Display Errors: On
Error Level: Not E_ALL
Register Globals: On
cURL: libcurl/7.13.1 OpenSSL/0.9.8a zlib/1.1.4
 
New Server:
PHP Version: 5.2.6RC4-pl0-gentoo
--with-curl --without-curlwrappers
Display Errors: On
Error Level: Not E_ALL
Register Globals: On
cURL: libcurl/7.17.1 OpenSSL/0.9.8g zlib/1.2.3
This is my code:

Code: Select all

<?php
    $sub_dir = "/sandbox/upload/";
    $basename = "logo";
    $ext = ".gif";
    $image_url = "http://www.google.com/intl/en_ALL/images/logo.gif";
    $upload = $_SERVER['DOCUMENT_ROOT'].$sub_dir.$basename.$ext;
    $handle = @fopen(urldecode($upload), "x");
    
        if (!$handle) {
            $copy="";
            $n = 1;
            $upload = $_SERVER['DOCUMENT_ROOT'].$sub_dir.$basename.$copy.$ext;
            while(file_exists(urldecode($upload))) {
        
                $copy = "_copy" . $n;
                ++$n;
                $upload=$_SERVER['DOCUMENT_ROOT'].$sub_dir.$basename.$copy.$ext;
                $filename = $basename.$copy.$ext;
            }
            $handle = fopen(urldecode($upload), "x");
        }
    
    $ch = curl_init();
    $timeout = 10;
    curl_setopt ($ch, CURLOPT_URL, $image_url);
    curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FILE, $handle);
    
    $image = curl_exec($ch);
    curl_close($ch);
    fclose($handle);
?>
Thank you very much for your help.

Re: fopen, x mode, curl, and fopencookie error

Posted: Tue Jun 10, 2008 3:04 pm
by Weirdan
tmoswift wrote: but now I'm more curious as to why it worked before and now.
*BSD doesn't have fopencookie at all. Here's a relevant snippet from PHP source:

Code: Select all

 
/* Under BSD, emulate fopencookie using funopen */
#if HAVE_FUNOPEN
typedef struct {
        int (*reader)(void *, char *, int);
        int (*writer)(void *, const char *, int);
        fpos_t (*seeker)(void *, fpos_t, int);
        int (*closer)(void *);
} COOKIE_IO_FUNCTIONS_T;
 
FILE *fopencookie(void *cookie, const char *mode, COOKIE_IO_FUNCTIONS_T *funcs)
{
        return funopen(cookie, funcs->reader, funcs->writer, funcs->seeker, funcs->closer);
}
# define HAVE_FOPENCOOKIE 1
# define PHP_STREAM_COOKIE_FUNCTIONS    &stream_cookie_functions