[PHP] PHP Newbie, Experienced Porgrammer, Annoying fread() problem

2001-02-07 Thread John Vanderbeck

Hello all,

Please bear with me for a moment.  I am new to PHP work, but have over 10
years experience in various other programming languages including C/C++

I'm just getting really frustrated here, and i'm hoping its a simple
mistake.  What I am running into is that ever file I open and read from, I
never actually get any data.  I know the data is in the file, but I don't
get it.  I can write to it fine.

Basic scenario is this...I have 2 modules, one that creates my data and one
that reads it.  I'm setting up a system for book reviews.  The first module
has a forms page that gets the book's ASIN, TITLE, and DESCRIPTION.  There
is a common books directory.  The module creates a subdirectory off of this
dir, and the sub dir's name is the ASIN of the book.  In this new sub
directory a file is created that contains 3 lines.  The ASIN, the TITLE, and
the DESCRIPTION, each seperated by a newline (\n).  This seems to work fine.

The second module is one that will display everythign to the users, and
eventually allow user submitted reviews.  This module opens a directory
handle for the common books directory, and enumerates each subdir (skipping
'.' and '..').  It then attempts to open the information file in each subdir
that was created above, and display the results.  Its always empty.  I can
include code if requested, but here are a few snippets of the immediate
areas..

first module 1 which does the saving of info:

 case 'save_info':
  if ($book_file = fopen("$booksdir/books.dat", "a"))
  {
   fwrite($book_file, $asin);
  }
  fclose($book_file);
  if ($book_file = fopen("$thisdir/book_info.dat", "w"))
  {
   fwrite($book_file, "$asin\n$title\n$description");
  }
  fclose($book_file);
  break;


and the module 2, the reads:
 // get directory handle
 $dir_handle = opendir($thisdir);
 $books = array();
 // load valid book directories into an array
 while($book_file = readdir($dir_handle))
 {
  if ($book_file != "."  $book_file != ".." 
is_dir("$thisdir/$book_file"))
  {
   $books[] = $book_file;
   echo "pAdded: $book_file";
  }
 }
 // loop through books and create HTML entry for each
 $index = 0;
 for ($index = 0; $index  sizeof($books); $index++)
 {
  $book_data_file = fopen("$thisdir/$books[$index]/book_info.dat", "r");
  echo "pOpened $thisdir/$books[$index]/book_info.dat/p";
  $book_data_in = fread($book_data_file, filesize($book_data_file));
  echo "pbook_data_in = $book_data_in/p";
  fclose($book_data_file);
  $book_data = explode("\n", $book_data_in);
  echo "ASIN:";
  echo "$book_data[0]";
  echo "br";
  echo "Title:";
  echo "$book_data[1]";
  echo "br";
  echo "Description:";
  echo "$book_data[2]";
  echo "p/p";
 }


If anyoen can clear this up for me, I would most appreciate it.  I'm on a
tight deadline, learning PHP as I go, and losing my mind on this seemingly
simple excersise!

- John Vanderbeck
- Admin, GameDesign


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




RE: [PHP] PHP Newbie, Experienced Porgrammer, Annoying fread() problem

2001-02-07 Thread James Moore

[snip]
  for ($index = 0; $index  sizeof($books); $index++)
  {
   $book_data_file = fopen("$thisdir/$books[$index]/book_info.dat", "r");
   echo "pOpened $thisdir/$books[$index]/book_info.dat/p";
   $book_data_in = fread($book_data_file, filesize($book_data_file));
 ^
should be:   
filesize("$thisdir/$books[$index]/book_info.dat")

   echo "pbook_data_in = $book_data_in/p";
   fclose($book_data_file);
[snip]

HTH

James
--  
James Moore
PHP QA Team
[EMAIL PROTECTED]

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]




Re: [PHP] PHP Newbie, Experienced Porgrammer, Annoying fread() problem

2001-02-07 Thread Brian Clark


Hi John,

@ 2:01:49 PM on 2/7/2001, [EMAIL PROTECTED] wrote:

 Please bear with me for a moment. I am new to PHP work, but have
 over 10 years experience in various other programming languages
 including C/C++

 I'm just getting really frustrated here, and i'm hoping its a simple
 mistake. What I am running into is that ever file I open and read
 from, I never actually get any data. I know the data is in the file,
 but I don't get it. I can write to it fine.

[...]

 If anyoen can clear this up for me, I would most appreciate it. I'm
 on a tight deadline, learning PHP as I go, and losing my mind on
 this seemingly simple excersise!

Obviously, James has answered your question, but I'm curious as to why
you aren't using a database for this particular task?

Another suggestion, for the actual book data file(s) is to just
include the 'cosmetic' information in with the actual $book_data when
you write it to disk.

IOW, instead of doing this:

$book_data = explode("\n", $book_data_in);
  echo "ASIN:";
  echo "$book_data[0]";
  echo "br";
  echo "Title:";
  echo "$book_data[1]";
  echo "br";
  echo "Description:";
  echo "$book_data[2]";
  echo "p/p";

Why didn't you just write that information to the file from the very
beginning?

If you were to do that, all you'd need to do is use include() instead
of 'module 2, the reads'

It just seems like that would be a lot less painful.
  
-Brian



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
To contact the list administrators, e-mail: [EMAIL PROTECTED]