fwrite() alternative

Discussion in 'PHP' started by AHA7, Nov 19, 2007.

  1. #1
    Hello,

    My host does not allow fopen,fwrite... functions, so what's the best alternative way to write to text files with PHP? Can I use cURL to write to files?
     
    AHA7, Nov 19, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    cURL can write to files, but it needs fopen() as well. (example) But you cannot write normal content from a variable or string.
     
    nico_swd, Nov 19, 2007 IP
  3. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #3
    So, if fopen() is not supported then there's no way ever I can write to a .txt file in PHP?
     
    AHA7, Nov 19, 2007 IP
  4. themasterinc

    themasterinc Banned

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $url = file('www.xxxxxxxxx.com')



    create

    .htaccess



    php_value allow_url_fopen On
     
    themasterinc, Nov 19, 2007 IP
  5. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #5

    But my host runs PHP as CGI and I don't think that will work.
     
    AHA7, Nov 19, 2007 IP
  6. themasterinc

    themasterinc Banned

    Messages:
    216
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Trying to activate this function through. Htaccess if not functioning tries
     
    themasterinc, Nov 19, 2007 IP
  7. AHA7

    AHA7 Peon

    Messages:
    445
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    OK, I've tried the following but it's not working. Could someone please tell me how to get it working. I want cURL to read text from a variable and then store it into a .txt file. Here's my failed try (It says can't resolve host, but I want it to read from a string not a URL):

    	
    	$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 ($curl, CURLOPT_URL, $context_resource);
    	curl_setopt($curl, CURLOPT_FILE, $fp); //Store output in $fp
    	curl_exec ($curl);
    	if (curl_errno($curl))
    	{
    		echo curl_error($curl);
    	}
    	curl_close ($curl);
    PHP:
     
    AHA7, Nov 19, 2007 IP