+ Reply to Thread
Results 1 to 4 of 4




  

Thread: PHP - Textbox Filename Name

      
  1. #1
    I'm New! jonambed is on a distinguished road
    Join Date
    March 9, 2009
    Posts
    6
    Rep Power
    0
    Feedback Score
    0

    Default PHP - Textbox Filename Name

    I'm currently using the following code to create and insert information into a file, but how do i save the file as a different filename through entering a filename inside a textbox?

    <html>
    <body>

    <?php
    $file=fopen("welcome.txt","r") or exit("Unable to open file!");
    ?>

    </body>
    </html>
    ________________________________
    Last edited by grim; May 25th, 2009 at 11:56 am.

  2. #2
    I'm New! Ewoot is on a distinguished road
    Join Date
    May 28, 2009
    Posts
    14
    Rep Power
    3
    Feedback Score
    0

    Default Re: PHP - Textbox Filename Name

    asuming you posted the textbox is posted with the name tag "customfile"


    <html>
    <body>
    <?php

    // CHECK WETHER TEXTBOX IS POSTED
    if (array_key_exists('customfile', $_POST)) {

    //FOR SECURITY REASONS I ALWAYS DO THE FOLLOWING ON POSTED VALUES
    $customfilename = nl2br(addslashes($_POST['customfile']));

    $file=fopen($customfilename . ".txt","r") or exit("Unable to open file!");

    } else {
    ?>
    <form name="testform" action="POST" method="test.php">
    Filename: <input type="text" name="customfile" /> <input type="submit" value="submit" />
    </form>

    <?php


    }

    ?>
    </body>
    </html>

  3. #3
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default Re: PHP - Textbox Filename Name

    While Ewoots idea is kind of correct if you use add slashes in a filename with a ' then the system will start to create folders which can then be abused. You would need to sanatize the post filename, strip_tags should be a must and then replace special characters by using a regular expression, sorry I dont have the function I use at hand as i'm out of the office.

    Also as an extra security measure you should only allow them to name the file NOT the extension, this should be taken from the uploaded file otherwise they can upload a text file and save it as a batch file, then when ran it can quick format the machine.

    File uploads should always be treated as a high security risk
    Matthew Bagley
    Paramiliar Design Studios
    IT Consultant | Website Design | Website Development

  4. #4
    Jedi Master Paramiliar is on a distinguished road Paramiliar's Avatar
    Join Date
    July 24, 2007
    Location
    West Midlands
    Posts
    105
    Rep Power
    5
    Feedback Score
    0

    Default Re: PHP - Textbox Filename Name

    Sorry for the double post but I cant edit my past post, the function I use to sanatize filenames is as follows

    PHP Code:
        function sanatize_filename($filename){
            
    $okchars "/[^0-9a-zA-Z()_.-]/";
            
    $filename preg_replace($okchars"_"$filename);
            return 
    $filename;
        } 
    This will remove any character that isn't a number, letter, brackets, underscore, full stop or a hyphon.
    Matthew Bagley
    Paramiliar Design Studios
    IT Consultant | Website Design | Website Development

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

     

Similar Threads

  1. Replies: 7
    Last Post: May 1st, 2008, 1:03 am

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
WebTalkForums
WebTalkForums
Recent Forum Threads