Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I badly need to create an .XML file inside my theme every time user comes to my site.

But I can't.

This code in header:

    <?php $ourFileName = "testFile.txt";
          $ourFileHandle = fopen($ourFileName, 'w') or die("can't open file");
          fclose($ourFileHandle); ?>

Just does nothing. It doesn't display "can't open file", but it doesn't create the file as well.

I'm testing it at my /localhost machine and this code works perfectly everywhere there (so file permissions are alright), but not in Wordpress.

Why is it happening? :(

share|improve this question

2 Answers 2

up vote 2 down vote accepted

File system access is tricky thing between different possible server configurations. If you need to implement it reliably in WP you should look into its API for such.

See Using the WP_Filesystem.

share|improve this answer

It's not a WordPress problem; WordPress will execute fopen() just like any other PHP function.

It may be an issue with your server configuration (e.g. safe mode).

share|improve this answer
1  
To expand on Chip's answer, make sure that the directory you are writing to (which wasn't specified) has write permissions for the server. A quick test is to specify /tmp/testfile.txt for your file and see if that works -- it probably will. –  Peter Rowell Apr 15 '11 at 23:38
    
that's strange, because I've got clean, valid XAMPP installation and fopen() works EVERYWHERE excepting Wordpress... So I'm not sure about server configuration issues... –  Wordpressor Apr 19 '11 at 15:27
    
I've seen fopen() used - to incredibly ill intent - in some of the Themes submitted to the WPORG Theme repository. –  Chip Bennett Apr 19 '11 at 16:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.