Professional Web Applications Themes

fwrite problem - PHP Development

Hi all, I am writing a small script which will run on solaris. I want to get the IP address and write it to a file log.dat. File is created fine and changed to 777 mode. I have no idea why it has to die at fwrite. Thanks in advance sanjay <? $fileName = "log.dat"; $fp = fopen($fileName, "a") || die ("cannot open $fileName"); chmod ($fileName, 0777) || die ("cannot change mode"); $string = $_SERVER['REMOTE_ADDR'] . " | " . date ("l dS of F Y h:i:s A") . "\n"; print $fp . " - " . $string; fwrite($fp, $string, ...

  1. #1

    Default fwrite problem

    Hi all,

    I am writing a small script which will run on solaris. I want to get the IP
    address and write it to a file log.dat.
    File is created fine and changed to 777 mode.
    I have no idea why it has to die at fwrite.

    Thanks in advance

    sanjay

    <?

    $fileName = "log.dat";

    $fp = fopen($fileName, "a") || die ("cannot open $fileName");
    chmod ($fileName, 0777) || die ("cannot change mode");

    $string = $_SERVER['REMOTE_ADDR'] . " | " . date ("l dS of F Y h:i:s
    A") . "\n";


    print $fp . " - " . $string;
    fwrite($fp, $string, strlen($string)) || die ("<BR>cannot write in the
    file $fileName");
    fclose($fp);

    ?>


    s a n j a y Guest

  2. #2

    Default Re: fwrite problem


    "s a n j a y" <someonesomewhere.com> schreef in bericht
    news:BXfRa.22309$BM.6265662newssrv26.news.prodigy .com...
    > Hi all,
    >
    > I am writing a small script which will run on solaris. I want to get the
    IP
    > address and write it to a file log.dat.
    > File is created fine and changed to 777 mode.
    > I have no idea why it has to die at fwrite.
    >
    > Thanks in advance
    >
    > sanjay
    >
    > <?
    >
    > $fileName = "log.dat";
    >
    > $fp = fopen($fileName, "a") || die ("cannot open $fileName");
    This statement will cause PHP to cast the file pointer into an integer.
    > chmod ($fileName, 0777) || die ("cannot change mode");
    It is unnessecary to chmod each time you run the code. When it's in another
    mode then 777, chmod will fail in most cases anyway.

    Below is the code that will work:

    <?
    $fileName = "log.dat";
    if ($fp = fopen($fileName, "a")) {
    $string = $_SERVER['REMOTE_ADDR'] . " | " . date ("l dS of F Y h:i:s
    A") . "\n";
    print $fp . " - " . $string;
    fwrite($fp, $string, strlen($string)) || die ("<br />fwrite
    failed");
    fclose($fp);
    } else {
    die ("Cannot open $fileName");
    }
    ?>

    JW



    Janwillem Borleffs Guest

  3. #3

    Default Re: fwrite problem

    Thanks a ton Janwillem ! I actually figured it out myself. But I am still
    thinking why adding

    || die ("blah")

    will cast the file pointer into an integer?

    sanjay


    "Janwillem Borleffs" <jwbjwbfoto.demon.nl> wrote in message
    news:3f15b6e1$0$28885$1b62eedfnews.euronet.nl...
    >
    > "s a n j a y" <someonesomewhere.com> schreef in bericht
    > news:BXfRa.22309$BM.6265662newssrv26.news.prodigy .com...
    > > Hi all,
    > >
    > > I am writing a small script which will run on solaris. I want to get the
    > IP
    > > address and write it to a file log.dat.
    > > File is created fine and changed to 777 mode.
    > > I have no idea why it has to die at fwrite.
    > >
    > > Thanks in advance
    > >
    > > sanjay
    > >
    > > <?
    > >
    > > $fileName = "log.dat";
    > >
    > > $fp = fopen($fileName, "a") || die ("cannot open $fileName");
    >
    > This statement will cause PHP to cast the file pointer into an integer.
    >
    > > chmod ($fileName, 0777) || die ("cannot change mode");
    >
    > It is unnessecary to chmod each time you run the code. When it's in
    another
    > mode then 777, chmod will fail in most cases anyway.
    >
    > Below is the code that will work:
    >
    > <?
    > $fileName = "log.dat";
    > if ($fp = fopen($fileName, "a")) {
    > $string = $_SERVER['REMOTE_ADDR'] . " | " . date ("l dS of F Y
    h:i:s
    > A") . "\n";
    > print $fp . " - " . $string;
    > fwrite($fp, $string, strlen($string)) || die ("<br />fwrite
    > failed");
    > fclose($fp);
    > } else {
    > die ("Cannot open $fileName");
    > }
    > ?>
    >
    > JW
    >
    >
    >

    s a n j a y Guest

  4. #4

    Default Re: fwrite problem


    "Janwillem Borleffs" <jwbjwbfoto.demon.nl> wrote in message
    news:3f15bd0b$0$28891$1b62eedfnews.euronet.nl...
    >
    > "s a n j a y" <someonesomewhere.com> schreef in bericht
    > news:HQiRa.22365$BM.6289336newssrv26.news.prodigy .com...
    > > Thanks a ton Janwillem ! I actually figured it out myself. But I am
    still
    > > thinking why adding
    > >
    > > || die ("blah")
    > >
    > > will cast the file pointer into an integer?
    > >
    >
    > That's because of the || operator. If you would use 'or' instead, it will
    > work too.
    >
    > I'm not shure why, perhaps someone else can explain this.
    ||=or

    ;)


    slapkicksy Guest

Similar Threads

  1. fwrite text problem
    By JDJones in forum PHP Development
    Replies: 3
    Last Post: September 26th, 12:52 PM
  2. fwrite() help needed
    By Craig Keightley in forum PHP Development
    Replies: 3
    Last Post: September 19th, 07:09 AM
  3. Is fwrite atomic or not?
    By Herouth Maoz in forum PHP Development
    Replies: 0
    Last Post: August 6th, 02:25 PM
  4. fwrite int or float
    By Ted Huntington in forum PHP Development
    Replies: 4
    Last Post: July 25th, 04:51 AM
  5. fwrite not working in php3
    By daniel@electroteque.org in forum PHP Development
    Replies: 3
    Last Post: July 17th, 01:18 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139