WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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
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
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


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.