Results 1 to 5 of 5

Thread: Script not working, no errors though.

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Script not working, no errors though.

    PHP Code:
    <?php
    function open_file($filetype,$increment,$filename)
    {
        
    $handle fopen($filename'r');
        
    $contents = @fread($handle,filesize($filename));
        
    fclose($handle);
            If(
    $increment == '-'){
              
    $contents--;
            }else{
              
    $contents++;
            }
        
    $handle fopen($filename$filetype);
        
    $contents = @fread($handle,filesize($filename));
        
    fwrite($handle,$contents);
        
    fclose($handle);
            return 
    $contents;
    }
    Switch(
    $_GET['user']){
        Case 
    "+":
            echo 
    open_file('w','+','users.txt');
        Case 
    "-":
            echo 
    open_file('w','-','users.txt');
    }
    ?>
    Ive never worked with functions, so bear with me if thats the problem. Im not getting any errors but my echo isnt returning anything and the file isnt getting added to.

    Im making a script to add one if in the url add=+ or minus one from the file if its add=-

    Thanks

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: Script not working, no errors though.

    You read the contents. You interpret them numerically and add/substract 1. Then you read the contents again, overwriting the modified value, and write these contents back into the file.

    In other words, you read the file twice, write its contents back once.
    Oh, and since the second time you only open it for writing, the second read operation fails and thus you probably write garbage back.

    I suppose the poor choice of name doesn't really matter anymore.

    Remove the second occurrence of this line:
    $contents = @fread($handle,filesize($filename));
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Script not working, no errors though.

    Thanks cornedbee, as you typed that i figured out that i was reading it twice and killing the data.

    PHP Code:
    <?php
    function open_file($filetype,$increment,$filename)
    {
        
    $handle fopen($filename'r');
        
    $contents = @fread($handle,filesize($filename));
        
    fclose($handle);
            If(
    $increment == '-'){
              
    $contents--;
            }else{
              
    $contents++;
            }
        
    $handle fopen($filename$filetype);
        
    fwrite($handle,$contents);
        
    fclose($handle);
        echo 
    $contents;
            return 
    $contents;
    }
    Switch(
    $_GET['user']){
        Case 
    "+":
            echo 
    open_file('w','+','users.txt');
        Case 
    "-":
            echo 
    open_file('w','-','users.txt');
    }
    ?>
    but i dont think im even getting to the open_file function, because it refuses to echo anything, even the first $contents.

    Thanks much

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: Script not working, no errors though.

    anyone?

  5. #5
    Lively Member {yak}'s Avatar
    Join Date
    Aug 2005
    Posts
    119

    Re: Script not working, no errors though.

    You don't need to echo $contents in your function, unless you want it to show up twice. Also, I doubt it's like the '+' and '-' signs in the query string, try something different:
    PHP Code:
    <?php

    function open_file($filetype,$increment,$filename)
    {
        
    $handle fopen($filename'r');
        
    $contents fread($handle,filesize($filename));
        
    fclose($handle);
            If(
    $increment == 'minus'){
              
    $contents--;
            }else{
              
    $contents++;
            }
        
    $handle fopen($filename$filetype);
        
    fwrite($handle,$contents);
        
    fclose($handle);
        return 
    $contents;
    }
    Switch(
    $_GET['user']){
        Case 
    "plus":
            echo 
    open_file('w','plus','users.txt');
            break;
        Case 
    "minus":
            echo 
    open_file('w','minus','users.txt');
            break;
    }


    ?>
    {yak}

Posting Permissions

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



Click Here to Expand Forum to Full Width