PHP write to text file not working -- blank page

I am using a simple script to write form data to a text file. I am developing on a local server for an intranet using IIS 7. When I enter content in the form and click submit, I am redirected to the php processing file but all I get is a blank page and no new text file.

Here is form code:

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=“http://www.w3.org/1999/xhtml”>

<head>
<meta content=“text/html; charset=utf-8” http-equiv=“Content-Type” />
<title>Untitled 1</title>
</head>

<body>
<form name=“myform” method=“post” action=“process.php”>
<textarea cols=“100” rows=“25” name=“content”>Enter something</textarea><br />
<input type=“submit” name=“submitform” value=“Sendform” />
</form>

</body>

</html>

Here is the process.php code:

<?php

$content = $_POST[“content”];
$file = fopen(”announcement-7.txt”, “w”) or die(“can’t open file”);
fwrite($file, $content);
fclose($file);

?>

Am I running into a server permission issue? As far as I can tell I have permissions to write files to the server directory. I am new to PHP, so is my code wrong?

Any help would be appreciated. I am not interested in other methods of storing form data. I want to save to a .txt file.

Did you type that in from MS Word?

Those quotes look a bit crazy to me.

Put this as the first line in your process.php

<?php

var_dump($_POST);

?>

That should prove a) your html is ok b) PHP got the message you sent to it.

Take a close look at those quotes next.


$content = $_POST["content"];
$file = fopen('announcement-7.txt', 'w') or die("can't open file");
// and so on

You could use single or double quotes as long as you don’t mix them but not those nutty MS Word open and close double quotes.

No…I did not use Microsoft Word :mad:…I used Microsoft Publisher :lol:!

seriously though,

I grabbed some tutorials online and so the quotes probably came from there. Anyways, I reviewed my source code and cleaned up the quotes and added the line you suggested, but I am still getting the same response; a blank page.

I’ll keep looking into it. It’s got to be a simple that I am overlooking.

Are both files in the same folder?

Yeah, both files are in the same folder. I’m wondering if I am overlooking a file/security permission. I tried building the form with ASP.NET using a VB script and received a server error message “File/page not found”.

It is acting as if the browser is restricted from opening the file.

You seem to be saying that process.php is not available…

Change the content to this, temporarily


<p>1 Some HTML, you are not losing your mind</p>
<?php
echo '<p>2 Furthermore process.php is processing PHP correctly.</p>';

var_dump($_POST);

?>

I retried this function with ASP.NET, turned error reporting on (duh!:confused:), and received an accessed denied error. My guess is that I am running into the same problem with PHP.

Do you know of any good resources explaining how to grant write security permission for PHP applications. As I review the security, it appears that I should be able to run these scripts on the server, but obviously I am missing something.

I have to say that so far I have seen no evidence you have even got PHP running on this server.

If that file I suggested to you in post #6 is in your main doc root (wherever that is, depending on whether your are running iis or apache, whether you told IIS or Apache what your home directory is, and if you have the correct setting in PHP.ini AND you have it in your hosts file … ) then you should be accessing it as:

http://localhost/process.php

OR

http://127.0.0.1/process.php

How do you know you have PHP installed and running correctly?

Ah, there may be a PHP issue. I ran the code you suggested and the only return I receive was the first paragraph element. The phpecho did not return the 2nd paragraph. Hmm…

I know PHP is installed and working because I successfully ran these scripts:

<?php
phpinfo() ;
?>

and

<?php
echo date(‘l, F dS Y.’);
?>

I saved the files in the same directory I have the form process php file saved.

Never mind. Your script did run successfully, there was an error when I copied the script into my web editor. I fixed the typo and it works fine.

Running this script doesnt generate any screen output - a white screen is ‘normal’.

That said, if the system encountered a opening the file, it doesnt necessarily return false, it throws a E_WARNING and continues…

Try the standard WPSE debugging method…


ini_set('display_errors',1);
error_reporting(E_ALL);

at the top of your process script.

EDIT: PS: Hope you didnt put single quotes around that die message that has a single quote in it.

Thanks for the advice. I was hoping there was some easy debugging feature that did not require me to dig into the server error logs (which I already did). I have confirmed that my problem is permissions. I do not have access to write to the directory, see errors below.

I’ll need to do some more research, because I thought I granted the right access to this folder. With it being on a local server, I do have full control of the file directory (create, delete, move, change files etc). My guess is that the server requires another permission setting so that the files can be created via the browser.

How can I check for proper file permissions? I am using IIS7?

–Here is the modified script I received the errors for—
<?php

ini_set(‘display_errors’,1);error_reporting(E_ALL);
$file = fopen(‘/data/intranet/Design-Development/slideshow/announcement7.txt’, ‘w’);
fwrite($file, ‘Test data input’);
fclose($file);

echo(‘Complete’);
?>

–Error Log Results–

Warning: fopen(/data/intranet/Design-Development/slideshow/announcement7.txt): failed to open stream: Permission denied in D:\Data\Intranet\Design-Development\Slideshow\announcement7-process.php on line 5
Warning: fwrite() expects parameter 1 to be resource, boolean given in D:\Data\Intranet\Design-Development\Slideshow\announcement7-process.php on line 6
Warning: fclose() expects parameter 1 to be resource, boolean given in D:\Data\Intranet\Design-Development\Slideshow\announcement7-process.php on line 7
Complete

Thanks for the input and the time. I figured it out. Everyhing works once I got the permissions in order.

However, I do need to learn more about best-practice when setting permissions. To make the script work I add IUSR to the directory permissions were the txt file was to be written. I have a feeling this is not the best or safest way to enable sercurity permission.

I’ll appreciate any pointers on this one.

Well tip #1 is gonna be ‘dont use Windows’…

yeah…not helpful. for this project I am stuck with Windows.