1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need a (simple) php script

Discussion in 'PHP & Perl' started by wkrappen91, Nov 27, 2010.

  1. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    Hey guys.
    Im currently working on a project for my fellow BHWlers (will share it soon) but i need one more thing and i cant figure it out.
    I want the visitor of my website to be able to load a txt file and then, by clicking a button display one line of it in a textfield.
    when the button is clicked again, the next line should appear.
    When the end is reached, it should start over at the top.

    I hope you can help me:)
     
  2. ivictus

    ivictus Regular Member

    Joined:
    Jan 26, 2010
    Messages:
    223
    Likes Received:
    31
    In php I would do it with a form that includes a hidden field that pass the variable for the current line number in the file.
     
    • Thanks Thanks x 1
  3. hi.imandrew

    hi.imandrew Junior Member

    Joined:
    Nov 22, 2009
    Messages:
    159
    Likes Received:
    42
    Location:
    Arkansas, USA
    PHP:
    <?php
    $file 
    file("filename.txt");//load contents into array which is split by each new line
    //to print the 5th line of the file:
    print $file[4];
    ?>
     
    • Thanks Thanks x 2
  4. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    Thanks so far but i still cant figure it out (0 PHP skills..)
    If anyone could go a little further and post the complete html/php code, i would be very very thankfull:)
     
  5. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    *Bump*
    Im offering a free scrapebox blast to the person who gives me the working code:)
     
  6. risefromdeath

    risefromdeath Power Member

    Joined:
    Jul 1, 2009
    Messages:
    650
    Likes Received:
    107
    i will wait untill you offer a better deal :)
     
  7. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    :D Really?
    This should take you like 5 minutes to throw down:(
     
  8. machoguapo

    machoguapo Newbie

    Joined:
    Sep 10, 2009
    Messages:
    14
    Likes Received:
    5
    hi, i have that kind of script... i can't post the link.
     
    • Thanks Thanks x 1
  9. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    post the code:)
     
  10. beaglejuice

    beaglejuice Power Member

    Joined:
    Mar 12, 2009
    Messages:
    595
    Likes Received:
    423
    Hmmm, so you want everything free?
     
  11. ivictus

    ivictus Regular Member

    Joined:
    Jan 26, 2010
    Messages:
    223
    Likes Received:
    31
    Try this
    Code:
    
    <?php
    $line_no = 0;
    if(isset($_POST['line_no'])) {  $line_no = $_POST['line_no']; }
    $lines = file("robots.txt");
    $last_line = count($lines);
    if ($line_no == $last_line) {
        $line_no = 0;
        } else {
        $line_no = $line_no +1;
        }
    ?> 
    <form name="main_form" method="POST" action="<?php echo $PHP_SELF;?>" >
    <input type="hidden" name="line_no" value="<?php echo $line_no;?>">
    <?php print $lines[$line_no]; ?>
    <p align="center"><input type="submit" value="Next"></p></form>
     
    • Thanks Thanks x 1
  12. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    Nope. I said i will give away a free scapebox blast to the one who solves it:)
    Thanks man:) this works.
    but im missing the ability to upload your own txt(that is where i am stuck...)
     
  13. ivictus

    ivictus Regular Member

    Joined:
    Jan 26, 2010
    Messages:
    223
    Likes Received:
    31
    How can you upload a php file but not a txt file?
     
  14. hi.imandrew

    hi.imandrew Junior Member

    Joined:
    Nov 22, 2009
    Messages:
    159
    Likes Received:
    42
    Location:
    Arkansas, USA
    PHP:
    <?
    $file = @explode(".",strrev($_FILES['upload']['tmp_name']));
    $target_path getcwd(); //gets current working directory
    if (strrev($file[0]) == "txt"){ // check to make sure file ends in txt
     
    if(move_uploaded_file($_FILES['upload']['tmp_name'], $target_path)) {
      
    //successfully uploaded
     
    }else{
      
    //error
     
    }
    }else{
     
    //wrong file format
    }
    ?>
    HTML:
    <form method='post' action='' enctype='multipart/form-data'>
     <center>
     <input type='file' name='upload'>
     <input type='submit' value='Upload'>
     </center>
     </form>
    
     
    • Thanks Thanks x 1
  15. wkrappen91

    wkrappen91 Power Member

    Joined:
    Sep 9, 2010
    Messages:
    588
    Likes Received:
    720
    Location:
    127.0.0.1
    I can but i want the user to be able to do so.

    hi.imandrew:
    This is exactly what i need:) now i just need a combination of those two scripts:) thanks for your work sofar:)
     
  16. machoguapo

    machoguapo Newbie

    Joined:
    Sep 10, 2009
    Messages:
    14
    Likes Received:
    5
    PHP:
    <?php
    session_start
    ();

    /*
    if(isset($_POST)&&is_array($_POST)&&sizeof($_POST)>0) {
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";    
    }
    */

    if(isset($_FILES)&&is_array($_FILES)&&isset($_FILES['uploadedfile']['tmp_name'])&&trim($_FILES['uploadedfile']['tmp_name'])!='') {
        
        
        
    //$dest = '/tmp/'.basename($_FILES['uploadedfile']['tmp_name']);
        
        
    $dest './temp/'.basename($_FILES['uploadedfile']['tmp_name']);
        
        
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $dest);

        if(
    file_exists($dest)) {
            
    $cont file_get_contents($dest);
            if(
    trim($cont)!='') {
                
    $bcont explode("\n",$cont);
                
    $acont = array();
                
                foreach(
    $bcont as $v) {
                    if(
    trim($v)!='') {
                        
    $acont[] = trim($v);
                    }
                }
                
                echo 
    'line #1 => ' $acont[0] . '<br><br>';
                
    $_SESSION['file'] = $dest;
                
    $_SESSION['line'] = 1;
                
    //echo "<pre>";
                //print_r($acont);
                //echo "</pre>";
            
    }
            
    //echo $cont;
        
    } else {
            unset(
    $_SESSION['file']);
            unset(
    $_SESSION['line']);
            echo 
    'file does not exists';
        }

        
        
    //echo "<pre>";
        //print_r($_FILES);
        //echo "</pre>";
    } else

    if(isset(
    $_SESSION)&&is_array($_SESSION)&&sizeof($_SESSION)>0) {
        
    //echo "<pre>";
        //print_r($_SESSION);
        //echo "</pre>";
        
        
    if(isset($_POST['next'])&&$_POST['next']=='next'&&isset($_SESSION['line'])&&is_numeric($_SESSION['line'])) {
            
    $_SESSION['line']++;
        }
        
        if(
    file_exists($_SESSION['file'])) {
            
    $cont file_get_contents($_SESSION['file']);
            if(
    trim($cont)!='') {
                
    $bcont explode("\n",$cont);
                
    $acont = array();
                
                foreach(
    $bcont as $v) {
                    if(
    trim($v)!='') {
                        
    $acont[] = trim($v);
                    }
                }
                
                if(!isset(
    $acont[$_SESSION['line']])) {
                    
    $_SESSION['line'] = 1;
                }
                
                echo 
    'line #'.$_SESSION['line'].' => ' $acont[$_SESSION['line']-1] . '<br><br>';

                
    //echo "<pre>";
                //print_r($acont);
                //echo "</pre>";
            
    }
            
    //echo $cont;
        
    } else {
            echo 
    'file does not exists';
        }
        
    }


    ?>
    <form enctype="multipart/form-data" action="upload.php" method="POST">
    <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
    Choose a file to upload: <input name="uploadedfile" type="file" /><br />
    <input type="submit" name="submit" value="Upload File" />
    <?php if(isset($_SESSION['file'])&&trim($_SESSION['file'])!='') { ?><input type="submit" name="next" value="next" /><?php ?>
    </form>
     
    • Thanks Thanks x 1
  17. machoguapo

    machoguapo Newbie

    Joined:
    Sep 10, 2009
    Messages:
    14
    Likes Received:
    5
    sorry the html was not posted. here is the full code.

    PHP:
    <?php
    session_start
    ();

    /*
    if(isset($_POST)&&is_array($_POST)&&sizeof($_POST)>0) {
        echo "<pre>";
        print_r($_POST);
        echo "</pre>";    
    }
    */

    if(isset($_FILES)&&is_array($_FILES)&&isset($_FILES['uploadedfile']['tmp_name'])&&trim($_FILES['uploadedfile']['tmp_name'])!='') {
        
        
        
    //$dest = '/tmp/'.basename($_FILES['uploadedfile']['tmp_name']);
        
        
    $dest './temp/'.basename($_FILES['uploadedfile']['tmp_name']);
        
        
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $dest);

        if(
    file_exists($dest)) {
            
    $cont file_get_contents($dest);
            if(
    trim($cont)!='') {
                
    $bcont explode("\n",$cont);
                
    $acont = array();
                
                foreach(
    $bcont as $v) {
                    if(
    trim($v)!='') {
                        
    $acont[] = trim($v);
                    }
                }
                
                echo 
    'line #1 => ' $acont[0] . '<br><br>';
                
    $_SESSION['file'] = $dest;
                
    $_SESSION['line'] = 1;
                
    //echo "<pre>";
                //print_r($acont);
                //echo "</pre>";
            
    }
            
    //echo $cont;
        
    } else {
            unset(
    $_SESSION['file']);
            unset(
    $_SESSION['line']);
            echo 
    'file does not exists';
        }

        
        
    //echo "<pre>";
        //print_r($_FILES);
        //echo "</pre>";
    } else

    if(isset(
    $_SESSION)&&is_array($_SESSION)&&sizeof($_SESSION)>0) {
        
    //echo "<pre>";
        //print_r($_SESSION);
        //echo "</pre>";
        
        
    if(isset($_POST['next'])&&$_POST['next']=='next'&&isset($_SESSION['line'])&&is_numeric($_SESSION['line'])) {
            
    $_SESSION['line']++;
        }
        
        if(
    file_exists($_SESSION['file'])) {
            
    $cont file_get_contents($_SESSION['file']);
            if(
    trim($cont)!='') {
                
    $bcont explode("\n",$cont);
                
    $acont = array();
                
                foreach(
    $bcont as $v) {
                    if(
    trim($v)!='') {
                        
    $acont[] = trim($v);
                    }
                }
                
                if(!isset(
    $acont[$_SESSION['line']])) {
                    
    $_SESSION['line'] = 1;
                }
                
                echo 
    'line #'.$_SESSION['line'].' => ' $acont[$_SESSION['line']-1] . '<br><br>';

                
    //echo "<pre>";
                //print_r($acont);
                //echo "</pre>";
            
    }
            
    //echo $cont;
        
    } else {
            echo 
    'file does not exists';
        }
        
    }


    echo 
    '<form enctype="multipart/form-data" action="upload.php" method="POST">'."\n";
    echo 
    '<input type="hidden" name="MAX_FILE_SIZE" value="100000" />'."\n"
    echo 'Choose a file to upload: <input name="uploadedfile" type="file" /><br />'."\n";
    echo 
    '<input type="submit" name="submit" value="Upload File" />'."\n";

    if(isset(
    $_SESSION['file'])&&trim($_SESSION['file'])!='') { 
        echo 
    '<input type="submit" name="next" value="next" />'."\n";
    }

    echo 
    '</form>';

    ?>
     
    • Thanks Thanks x 1