SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    fwrite() alternative

    Hello,

    My host does not allow fopen, fwrite... functions.
    So, I want to use cURL to write to a .txt file.
    Since, I can enable fopen for a running script using: ini_set(allow_url_fopen, 1). I can make cURL return its output to a file opened by fopen and so no need for fwrite. The problem is that cURL only reads from URLs but I want it to read from a string, how can I do that?
    I've tried to simulate a server's response and sent the string to cURL as a server response but it didn't work.

    Here's my code:

    PHP Code:
    $string "Append this text to the file named target_file.txt\r\n";

    $opts = array( 'http' => array('method'=>'GET','header'=>'status: HTTP/1.1 200 OK\r\n','content' => $string) );

    $context_resource stream_context_create($opts); // Try to simulate a server response

    $curl curl_init();

    $fp fopen("target_file.txt"'a'); // This is the file to which I want to append the text in $string

    curl_setopt ($curlCURLOPT_URL$context_resource);
    curl_setopt($curlCURLOPT_FILE$fp); //Store output in $fp
    curl_exec ($curl);
    if (
    curl_errno($curl))
    {
       echo 
    curl_error($curl);
    }
    curl_close ($curl); 

    I want to append $string to "target_file.txt" without overwriting.
    Any help would be much appreciated.

  2. #2
    SitePoint Guru Ruben K.'s Avatar
    Join Date
    Jun 2005
    Location
    Alkmaar, The Netherlands
    Posts
    693
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you're on php5, you can use file_put_contents()

  3. #3
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    curl isn't used to access local files, which I assume that target_file.txt is?

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2005
    Posts
    52
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    curl isn't used to access local files, which I assume that target_file.txt is?
    target_file.txt is a local file that's opened by fopen(), the stream resource returned by fopen is stored in $fp which is then passed to CURLOPT_FILE. The default value of CURLOPT_FILE is STDOUT (the browser window) but when you pass a stream resource to it returned by fopen it'll send the output directly to the file opened by fopen instead of sending it (the output) to the browser.

    But the problem now is how can I make cURL read from a string not a URL?

  5. #5
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    If your host doesn't let you use basic file I/O operations, either ask them to lift such a stupid restriction or take your money else where.

    File operations is an important job for any application restricting those is just retarded.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •