Jump to content

Cant view fwrite text file in Browser


glapo21

Recommended Posts

Hey,

I am pretty new to php. I am creating a simple website to handle a http get I need to do for a Android Application. I can write to a file but I am unable to open it in a browser. It just shows a blank page. When I download it from my browser (Firefox) to my hard drive (Windows 7) using my ftp I can view the .txt file. All i want to do is write to a text file.

 

Any help would be nice. Sorry for sparse information. I am hosting it on 000webhost.com.

 

Thanks

My Code: 3 files blank "data.txt", index.html, process.php

 

Index.html

<html><body>
<h4>Tizag Art Supply Order Form</h4>
<form action="process.php" method="post"> 
<select name="item"> 
<option>Paint</option>
<option>Brushes</option>
<option>Erasers</option>
</select>
Quantity: <input name="quantity" type="text" /> 
<input type="submit" />
</form>
</body></html>

 

process.php

<html><body>
<?php
$quantity = $_POST['quantity'];
$item = $_POST['item'];

$myFile = "data.txt";
$fh = fopen($myFile, 'at') or die("can't open file");
$stringData = "Bobby Bopper";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner";
fwrite($fh, $stringData);
fclose($fh);


echo "You ordered ". $quantity . " " . $item . ".<br />";
echo "Thank you for ordering from Tizag Art Supplies!";

?>
</body></html>

Link to comment
https://forums.phpfreaks.com/topic/213185-cant-view-fwrite-text-file-in-browser/
Share on other sites

use this

<?php
$quantity = $_POST['quantity'];
$item = $_POST['item'];

$myFile = "data.txt";
$fh = fopen($myFile, 'a');
$stringData = "Bobby Bopper\n";
fwrite($fh, $stringData);
$stringData = "Tracy Tanner\n";
fwrite($fh, $stringData);
fclose($fh);


echo "You ordered ". $quantity . " " . $item . ".<br />";
echo "Thank you for ordering from Tizag Art Supplies!";
?>

 

I dont see where the form is used at all except in the echo, maybe thats all you wanted.

 

-edit- Made sure the php statement was closed. -edit-

Hey,

 

Thanks for the reply. Sorry all that code is just for testing. I will have a url HTTP GET specified in a Android Application. Here is my final code:

 

getTask.php

<html><body>
<?php
$time = $_GET['time'];
$task = $_GET['task'];

$myFile = "data.txt";
$fh = fopen($myFile, 'at') or die("can't open file");
$stringData = "Time Received: ";
fwrite($fh, $stringData);
fwrite($fh, $time);
$stringData = "   Task: ";
fwrite($fh, $stringData);
fwrite($fh, $task);
$stringData = "\n\n";
fwrite($fh, $stringData);
fclose($fh);

?>
</body></html>

Oh ok now I understand.

 

At

$fh = fopen($myFile, 'at') or die("can't open file");

I'm pretty sure you want:

$fh = fopen($myFile, 'a+') or die("can't open file");

a+ not at

there is no fopen mode "t" at-least that i've heard of.

Well I heard somewhere t was a translation flag  for windows/linux \n and \r\n I am not 100% sure either. I can write to the file "data.txt" fine. I can download it using my ftp. However I cannot access it anywhere else. I cant download it using a browser or a download manager. I have other files on this server that I can download. The permission for the .txt file is 666. Any idea?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.