Problems creating and writing to file.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Problems creating and writing to file.

Post by Bill H »

First, the include file codes.php:

Code: Select all

<?php
$names = array( "Bill", "Kathleen", "Kris", "Susan", "Don", "Kimberly", "Matt", "Makena");
$codes = array(  5431,   6147,       6742,   9086,    5442,  7593,       3234,   6561);
?>
and functs.php:

Code: Select all

<?php

// Place all records from the file in a array and return it
function listall()
&#123;
     $name = "files/lst" . $_SESSION&#1111;'viewing'] . ".dat";         // ie "lst6147.dat"
     $r = "";

     $f = @fopen($name, "r");
     if ($f != FALSE)
     &#123;    $a = fread($f, filesize($name));        // read the whole file
	     fclose($f);
          $r = trim($a);
     &#125;
     return $r;
&#125;

// Get a specific record from the file based on the record number
function getrec($id)
&#123;
// Integer: $id   =   record number to retrieve
     $d = explode("~", listall());
	return $d&#1111;$id];
&#125;

// Put a specific record into file based on id number
function putrec($id, $rec)
&#123;
// Integer: $id   =   record number to replace
// String: $rec   =   string to replace in the file

     $d = explode("~", listall());
     $d&#1111;$id] = $rec;
     $w = implode("~", $d);

     $name = "files/lst" . $_SESSION&#1111;'viewing'] . ".dat";         // ie "lst6147.dat"
     $f = fopen($name, "w");                                // for writing, truncated
     fwrite($f, $w);
     fclose($f);
&#125;

// Put a specific record onto end of file
function addrec($rec)
&#123;
// String: $rec   =   string to replace in the file

     $name = "files/lst" . $_SESSION&#1111;'viewing'] . ".dat";         // ie "lst6147.dat"
     $s = @filesize($name);
     $f = fopen($name, "a");                                // for append at end of file
     if ($s) fwrite($f, "~");
     fwrite($f, $rec);
     fclose($f);
&#125;

?>
And, finally, the one causing errors:

Code: Select all

<?php
session_start();
include "codes.php";
include "functs.php";

$r = array($Name, $Desc, $Size, $Store, $Cost);
$r&#1111;5] = $_SESSION&#1111;'ucode'];
$r&#1111;6] = $Vis;
$r&#1111;7] = "";
if (!strcmp($Flagged, "yes")) $r&#1111;7] = $_SESSION&#1111;'ucode'];
$f = implode("|", $r);
addrec($f);
header("Location:select.php?Selection=" . $_SESSION&#1111;'viewing']);

?>
firstly, I'm getting a "permission denied" error in the addrec function.
The file does not exist, but the fopen function should creadte it, right?
The "files" directory has permission for all levels (CHMOD 777).

Secondly, I'm getting a "cannot add headers, data already sent" error on the last line of addrec.php, but I simply cannot see where any data has been sent to the browser.

Help?
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

After you created file chmod that file to 777 by using chmod function like this

Code: Select all

&lt;?php
  chmd($name,777);
?&gt;
And on the second question add ob_start() and the very beginning of the script (before anything else but <?php), and ob_flash() at the end.
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post by hob_goblin »

Takuma wrote:ob_flash() at the end.
ob_end_flush();
User avatar
Bill H
DevNet Resident
Posts: 1136
Joined: Sat Jun 01, 2002 10:16 am
Location: San Diego CA
Contact:

Post by Bill H »

Thanks for the responses. I do have the file process working now, but...

I'm not sure I understood the instruction to "CHMOD 777" the file after it was created. The permission failure is preventing its creation so it doesn't seem possible to follow that direction. Am I missing something?

I am getting around that for now by manually creating the files and uploading them with the 777 attribute. There are only a few files, so that works for now.

But I could really use some help understanding how to programmatically create a file that doesn't exist.

Also, the ob_start() and ob_end_flush() cleared up the header error. Thank you.
But why? I cannot see where anything was sent to the browser.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

maybe you're not allowed to write in that directory you want to create a subdir.
search for a tutorial on unix file permissions
User avatar
Takuma
Forum Regular
Posts: 931
Joined: Sun Aug 04, 2002 10:24 am
Location: UK
Contact:

Post by Takuma »

MMmm, how about leaving the file handle open... So have a variable like
$file_variable and stored the file handle there and use it... And also your error code were the problem I think...
Post Reply