Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Jun 2002
    Location
    Iowa / Notre Dame
    Posts
    538
    Thanks
    0
    Thanked 0 Times in 0 Posts

    fwrite issue with writable file

    My issues always seem so trivial... oh well.

    I am currentyl writing a script that retrieves a user's email address and adds it to a text file. It's a rather simple script that allows users to sign up for a newsletter. I would use a database, but the university doesn't provide me with one...

    Anyway, I know all my code works up until the fwrite() command. I'm not sure exactly what is happening, but the script always says it "could not write to file" (as per the die() command). Here's the code:
    PHP Code:
    <?php

        $email 
    $_GET['email'];
        
    $filename "emails.txt";

        if(
    eregi("^[a-z0-9\._-]+".
            
    "@{1}"."([a-z0-9]{1}[a-z0-9-]*[a-z0-9]{1}\.{1})+".
            
    "([a-z]+\.){0,1}".
            
    "([a-z]+){1}$"$email)) { 
            
                
    $handle fopen($filename"a+");
                
    $contents fread($handlefilesize($filename));
                
    $emails explode("; "$contents);
                
    $count count($emails);
            
                for(
    $i=0;$i<$count$i++) {
                    if(
    $email == $emails[$i]) {
                        exit(
    "email already exists");
                    }
                }
                
                
    $femail $email "; ";
                echo(
    is_writable($filename)) ? "is writable" "is not writable"//just to test
                
    fwrite($handle$femail) or die("could not write to file");
        }
        else {
            echo 
    "already there";
        }
        
    ?>
    My Site {Mike's Adventures}

    Yikes, forums are almost too much fun.

  • #2
    Regular Coder
    Join Date
    Mar 2004
    Posts
    115
    Thanks
    0
    Thanked 0 Times in 0 Posts
    is the file your writing to have the right permissions

    ie writable by group chmod 666 on unix

    a mystery? on windows

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Iowa / Notre Dame
    Posts
    538
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It is on a windows machine, and the script doesn't even work if the script itself creates the file (it will work the first time, but doesn't work once the file is already created). Also, the is_writable() command returns true, which makes me believe the file is writable...
    My Site {Mike's Adventures}

    Yikes, forums are almost too much fun.

  • #4
    Super Moderator
    Join Date
    May 2002
    Location
    Perth Australia
    Posts
    4,095
    Thanks
    11
    Thanked 101 Times in 99 Posts
    fwrite() on win32 requires a binary flag ...
    PHP Code:
    <?php
    $fp 
    fopen$file'a[b]b[/b]' ) ;
    ?>
    or just use fputs() instead of fwrite() .
    resistance is...

    MVC is the current buzz in web application architectures. It comes from event-driven desktop application design and doesn't fit into web application design very well. But luckily nobody really knows what MVC means, so we can call our presentation layer separation mechanism MVC and move on. (Rasmus Lerdorf)

  • #5
    Regular Coder
    Join Date
    Jun 2002
    Location
    Iowa / Notre Dame
    Posts
    538
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Adding the binary flag makes the script work locally, but when I upload it to my personal web space (provided by the university) the script stops working. I added some if else statements to act as little error checkers, and it seems that neither fopen() nor fwrite() works. I noticed fopen() wasn't working when a my specified file wasn't created despite it not existing. This may be a server issue, I don't know. The university's server doesn't have the mod_rewrite module installed on apache, so who knows what else they won't allow us to do.

    Here is the code so far:
    PHP Code:
    <?php

    $email 
    $_GET['email'];
    $filename "emails.txt";
    chmod($filename0777); //just to try and chmod it...

    if(eregi("^[a-z0-9\._-]+".
        
    "@{1}"."([a-z0-9]{1}[a-z0-9-]*[a-z0-9]{1}\.{1})+".
        
    "([a-z]+\.){0,1}".
        
    "([a-z]+){1}$"$email)) { 
        
            if(
    $handle fopen($filename"a+")) {
                echo 
    "opened\n";
            }
            else {
                echo 
    "not opened\n";
            }
            
    $contents fread($handlefilesize($filename));
            
    $emails explode("; "$contents);
            
    $count count($emails);
        
            for(
    $i=0;$i<$count$i++) {
                if(
    $email == $emails[$i]) {
                    
    header("Location: http:\/\/" $_SERVER['HTTP_HOST'] . "/~mking5/?e=twice");
                    exit;
                }
            }
            
            
    $femail $email "; ";
            if(
    is_writable($filename)) {
                echo 
    "is writable\n";
            }
            else {
                echo 
    "not writable\n";
            }
            if(
    fwrite($handle$femail)) {
                
    header("Location: http:\/\/" $_SERVER['HTTP_HOST'] . "/~mking5/?e=entered");
                exit;
            }
    }
    else {
        
    header("Location: http:\/\/" $_SERVER['HTTP_HOST'] . "/~mking5/?e=invalid");
    }
        
    ?>
    If you want to try the script, you can go to http://www3.nd.edu/~mking5/ and then enter an email in the newsletter form. You should see the script output both "not opened" and "not writable".
    My Site {Mike's Adventures}

    Yikes, forums are almost too much fun.


  •  

    Posting Permissions

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