Well in my script there is a line that reads file through the function readfile(), the code is shown below:

header ($contentType);
$status = readfile($image);
if($status == "" or $status == "false" or $status == "FALSE"){
    // Reading the file failed, so show an error...	 
    header ("text/plain");
    die("Readfile appears to be disabled on your host.");
}

The problem now is that some users of my script have the property allow_url_fopen disabled on their webhost, and there is no way for them to change this setting in php.ini. I heard that it is possible to bypass this restriction by using curl functions, but I have no idea what they are and how to use them. Can anyone of you please let me know how to re-write the above code into curl functions? Even tips are appreciated. Also is curl functions better than readfile(), if so I may consider revising my old code with curl functions to improve the script.

    Lord Yggdrasill;11013051 wrote:

    I have no idea what they are and how to use them.

    RTFM is usually a good place to start, and the [man]cURL[/man] section is no exception. For example, not only will you find the complete list of functions (including coding examples on their respective pages), but there's even a basic curl example.

    By the way, "text/plain" isn't a valid HTTP header in your code above. (I'm guessing the value in $contentType isn't, either.)

      Well I did read the manual on PHP.net but still clueless. Would you mind providing me with an example though? You do not have to use the code I have in my first post, just to illustrate the idea is gonna be nice.

        Got it to work with the following code. XD Not sure if there's a way to optimize it though, lemme know if you have any comments to make:

        header ($contentType); 
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $image);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_exec($ch);
        curl_close($ch);  
          Lord Yggdrasill;11013141 wrote:
          header ($contentType); 
          

          May or may not be necessary, but has nothing to do with curl. This tells your webserver to send this header in response to a request to your server. It does not send this header in the request you do using curl. Also, as noted previously in this thread, the header should be on the form: "content-type: text/plain", "content-type: image/jpeg" etc.

          Apart from that, I'd also specify curl options for time outs which is one of the major advantages using curl over readfile. If the remote server is busy or does not respond, it's better to wait no more than 1 or 2 seconds total before resuming execution.

            Write a Reply...