Custom Page Template in WordPress

I am using a Custom Page Template in WordPress. I followed these steps to make it:

  1. Duplicated page.php file in the /wp-content/themes/themename/ directory to another file called, submit.php
  2. Inserted the Template Name in the PHP code at the starting. I am unable to post the snippet here, stackoverflow is not displaying it.

    Read More
  3. Now it shows up in the wp-admin panel under Page Templates.

I have an HTML form on the Custom Page of WordPress. The PHP will read the data entered by the end user in the text area and write it to a file stored on the server.

All goes well as long as I write this data to a file this way:

$fp=fopen("filename.txt","w") or die("Can't open the file");

However, as a result of this file will be available in the path:

http://sitename.com/wordpress/wp-content/themes/themename/filename.txt

So, it will store the data read from the HTML form in the current directory. Instead I want to write this to a file stored in another location. Let’s say:

http://sitename.com/wordpress/files/filename.txt

to do this, I modified the code this way:

$fp=fopen("../../../files/filename.txt","w") or die("Can't open the file");

The PHP script dies with the error message, could not open the file.

I tried writing the output to wp-content/uploads directory as follows.

$filename="../../uploads/".$cartype.".txt";
$fp=fopen($filename,"w") or die("can't open the file");

It Gives the error: can’t open the file.

$cartype is just a value that is read via the HTML Form input.

I have set the permissions for the uploads folder to 777 already.

I tried this as well:

$filename="/wp-content/uploads/".$cartype.".txt";
$fp=fopen(ABSPATH.$filename,"w") or die("can't open the file");

It again gives the error: can’t open the file.

Please note that, I am writing data into the file later this way:

fwrite($fp,$data)

Also, once I am able to do this, I would like to restrict the permissions to the “files” directory so that only the wordpress admin can read the files in that directory using FTP or similar alternative.

Thanks.

Related posts

Leave a Reply

1 comment

  1. You could chmod 777 /path/to/wordpress/files/ however, I would put them in wp-content/uploads since that’s probably already writeable.