writing input list box to file

Discussion in 'PHP' started by deemainer, Feb 8, 2010.

  1. #1
    Hi all

    I need to be able to have a page with an input box that i can past a list of picture names. On hitting submit the code runs through the list and writes the list as links to a file checking to see if it already exists. Is that easy to do?

    Any help appreciated.
     
    deemainer, Feb 8, 2010 IP
  2. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #2
    What did you mean saying "write the list of picture names as links"? How does a picture name become a link?
     
    s_ruben, Feb 8, 2010 IP
  3. deemainer

    deemainer Active Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #3
    sorry...i should have been more specific. I mean that in the input box i can list have a list for example:

    1.jpg
    2.gif
    3.jpeg

    etc etc...It would loop through list and write to sitemap.html (for example) a list like this:

    <a href="http://mysite/foldername/1.jpg">1.jpg</a>
    <a href="http://mysite/foldername/2.gif">2.gif</a>
    ...etc

    Its basically a sitemap generator for any list . Hope that makes sense.
     
    deemainer, Feb 8, 2010 IP
  4. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #4
    Do a foreach loop and then use preg_replace.
     
    danx10, Feb 8, 2010 IP
  5. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #5
    I think it is just that what you want:

    
    <html>
    
    <head>
      <title>Links To File</title>
    </head>
    
    <body>
    
    <?php
    if(isset($_POST["submit"])){
        $filename = "sitemap.html";
        $href = '<a href="http://mysite/foldername/%s">%s</a><br />';
    
        $list = explode("\n",$_POST["list"]);
    
        $handle = fopen($filename,"a+");
        if(filesize($filename)>0){
            if($handle){
                while(!feof($handle)){
                    $line = fgets($handle,filesize($filename));
                    for($i=0; $i<count($list); $i++){
                        if(sprintf($href,trim($list[$i]),trim($list[$i]))==trim($line)){
                            unset($list[$i]);
                        }
                    }
                }
            }
        }
    
    
        $string = "";
        foreach($list as $link){
            $string .= "\n".sprintf($href,trim($link),trim($link));
        }
    
        fwrite($handle,$string);
    
        fclose($handle);
    }
    ?>
    
    <form method="post">
        <div><textarea name="list" id="list" cols="30" rows="10"></textarea></div>
        <div><input type="submit" name="submit" value="Submit"></div>
    </form>
    
    </body>
    
    </html>
    
    Code (markup):
     
    s_ruben, Feb 8, 2010 IP
  6. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #6
    I forgot to write how to use it. :) Just enter in the textfield the picture names (each picture name in the new line) and submit that.
     
    s_ruben, Feb 8, 2010 IP
  7. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #7
    @s_ruben

    You don't need so much code

    <html>
    
    <head>
      <title>Links To File</title>
    </head>
    
    <body>
    
    <?php
    
    if(isset($_POST['submit']) && !empty($_POST['list'])){
    $list = str_replace(array("\r\n", "\r"), "\n", strip_tags($_POST['list']));
    
    $list = explode("\n", $list);
    
    foreach($list as $picturename)
    {
    
    $picturename = preg_replace("/($picturename*)/", "<a href=\"http://mysite/foldername/$1\">$1</a>", $picturename);
    
    ob_start();
    echo $picturename."\n";
    $list = ob_get_contents();
    
    }
    ob_end_clean();
    
    file_put_contents("sitemap.html", $list);
    
    }
    ?>
    
    <form method="post">
        <div><textarea name="list" id="list" cols="30" rows="10"></textarea></div>
        <div><input type="submit" name="submit" value="Submit"></div>
    </form>
    
    </body>
    
    </html>
    PHP:
     
    danx10, Feb 8, 2010 IP
  8. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #8
    @danx10

    Have you tested your code?? I have tested and it doesn't work correctly!
     
    s_ruben, Feb 8, 2010 IP
  9. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #9
    Woops, I need to start testing my code!.

    This should work:

    <html>
    
    <head>
      <title>Links To File</title>
    </head>
    
    <body>
    
    <?php
    
    if(isset($_POST['submit']) && !empty($_POST['list'])){
    $list = str_replace(array("\r\n", "\r"), "\n", strip_tags($_POST['list']));
    
    $list = explode("\n", $list);
    
    foreach($list as $picturename){
    
    if(!stristr(file_get_contents('sitemap.html'), $picturename)){
    $picturename = preg_replace("/($picturename*)/", "<a href=\"http://mysite/foldername/$1\">$1</a>", $picturename);
    
    $list = $picturename."\n";
    $fp = fopen('sitemap.html', 'a');
    fwrite($fp, $list);
    fclose($fp);
    }
    }
    
    }
    ?>
    
    <form method="post">
        <div><textarea name="list" id="list" cols="30" rows="10"></textarea></div>
        <div><input type="submit" name="submit" value="Submit"></div>
    </form>
    
    </body>
    
    </html>
    PHP:
     
    Last edited: Feb 8, 2010
    danx10, Feb 8, 2010 IP
  10. s_ruben

    s_ruben Active Member

    Messages:
    735
    Likes Received:
    26
    Best Answers:
    1
    Trophy Points:
    78
    #10
    @danx10

    File cannot contain the same links. If you enter the same link it must not be written in the file again.
     
    s_ruben, Feb 8, 2010 IP
    deemainer likes this.
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    Maybe, but the OP didn't provide this info, however I've modified the post to prevent this from happening, thanks :)
     
    danx10, Feb 8, 2010 IP
  12. deemainer

    deemainer Active Member

    Messages:
    351
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    78
    #12
    Thank you both for your help ! Got it working and very much appreciate. Rep added...although danx10 you helped me on something else recently so it wont let me rep you again so soon !
     
    deemainer, Feb 8, 2010 IP