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 13 of 13

Thread: Forms and PHP

  1. #1
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Forms and PHP

    I have a question, but I'm not sure if it would be considered simple or complex. What I'm trying to do is help users of a certain account management software.

    What I want to do is have a "multipart" form on my web site. This form will allow users to select a text file from their computer. The text file being selected will be numerous lines of text in the following format:

    Code:
    firstemail@domain.com:password
    secondemail@domain.net:password
    thirdemail@domain.info:password
    Within this particular script, I would like to change all colons ":" to semicolons ";". After that I would like to convert every line to hexadecimal. So the above file would have the following output:

    Code:
    6669727374656D61696C40646F6D61696E2E636F6D3B70617373776F7264
    7365636F6E64656D61696C40646F6D61696E2E6E65743B70617373776F7264
    7468697264656D61696C40646F6D61696E2E696E666F3B70617373776F7264
    After this is done, a randomly named folder needs to be created within a specified directory on my web site. (It's important that this folder is not named the same as another folder that already exists) Then the output file will save itself as "scalemode.txt" within the aforementioned randomly named folder.

    Any help would be greatly appreciated. Thanks in advance for any help you all provide.



    UPDATE:

    I have put together a little something that I would like to use to create the random directory:

    PHP Code:
    <?php
    $randnum 
    rand(11000000000);
    $other time();
    $dirname "$randnum$other";
    $old_umask umask(0);
    mkdir("/path/to/my/server/FOLDER/".$dirname0777);
    ?>
    That's just what I'm using to create the random directory in which to save the file to. I still need help with the rest though.
    Last edited by PutterPlace; 03-31-2007 at 06:53 AM.

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    This sounds like a homework assignment ... is it?

  • #3
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No. Actually, there's an automated account management software that it'll be used for. The software allows you to input an unlimited number of accounts, but they all have to be entered manually, and one at a time. This software then saves the inputted accounts into a text file in hexadecimal format, separating the account name (represented with the email address) and password with a semicolon.

    This form will allow users to input their accounts from a text file, and overwrite the file that this software creates. It will help those who have a lot of accounts to manage.

  • #4
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    UPDATE:

    I have put together a little something that I would like to use to create the random directory:

    PHP Code:
    <?php
    $randnum 
    rand(11000000000);
    $other time();
    $dirname "$randnum$other";
    $old_umask umask(0);
    mkdir("/path/to/my/server/FOLDER/".$dirname0777);
    ?>
    I'm still working on the script as a whole. That's just what I'm using to create the random directory in which to save the file to.

  • #5
    Regular Coder
    Join Date
    Oct 2006
    Location
    Bristol
    Posts
    128
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To change the colons to semi-colons you could use preg_replace:

    http://uk.php.net/manual/en/function.preg-replace.php

    And to convert the strings to hex you obviously haven't bothered searching google :/

    http://www.google.co.uk/search?hl=en...G=Search&meta=

    There's a function on the first link which seems to do what you want.

    Alex
    Give me Rep if I was helpful and ignore if I wasn't ;)

    http://www.google.com <--use this before asking

    Nominate a Helpful Member


  • #6
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks very much for your help. I will definitely work on this some more when I get home, and post my progress here.

    By the way, when I searched google, I wrote ascii to hexadecimal. I didn't think of actually replacing "ascii" with "string". :P Thanks again.



    -PutterPlace

  • #7
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,502
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    One question ...

    Once you create those random directories, how will you know
    which one contains the "scalemode.txt" file that you're looking for?

    I guess the direction I'm going is ...

    Instead of creating a randomly-named directory and putting
    "scalemode.txt" into it, why not just name "scalemode.txt"
    something random (example: "3U3tgy36.txt" ), and save them
    all on one .htaccess protected directory?

    That way, you could have one file in that protected directory,
    called something like, "index.txt" that keeps track of which ones
    are which, like a log (a list of the IP's that were responsible for
    saving the file, the random file name, the save date, number of
    emails in that file, a non-cryptic way of identifying it, etc).


    .
    Last edited by mlseim; 03-31-2007 at 05:55 PM.

  • #8
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I want it to create random directories so that the text file can be named scalemode.txt. This file must be named this way, and it's easier to jst have it already named like that for the users using this form. That way they don't have to type in the file name when they're saving it, or rename it later.

    UPDATE: I fixed that problem by changing "text/plain" to "plain/text". Maybe I was wrong, but I thought, "text/plain" was correct. lol Anyways, now I'm fixing my script to replace all colons with semicolons. I'll post my progress

    Anyways, here's what I have so far. The only thing I need to add is the ascii to hex thing, but I'm trying to fix another problem right now. The only problem I'm having right now is validating the type of the uploaded file. It's set to "text/plain" which is what a TXT file should be. However, when selecting a file of that type, it returns the error that I set, "Error: You must select an account list in .txt format.". I'm not sure why it's doing this. I tried setting it to image/gif, just to make sure that it would work with a different mime type. After selecting a GIF file, it worked just fine. Any ideas why I'm having this problem?
    Last edited by PutterPlace; 03-31-2007 at 11:45 PM.

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    Quote Originally Posted by Alex! View Post
    To change the colons to semi-colons you could use preg_replace:

    http://uk.php.net/manual/en/function.preg-replace.php

    And to convert the strings to hex you obviously haven't bothered searching google :/

    http://www.google.co.uk/search?hl=en...G=Search&meta=

    There's a function on the first link which seems to do what you want.

    Alex
    Why preg_replace? Its just a simple colon to semi-colon straight swap, why bother with regular expressions? A simple str_replace would do the job

    for the OP, what your asking for is really quite simple but I'm not going to do it all for you But heres a little pseudo-code/process flow to help you along the way - you've pretty much been given all the functions you need anyway

    Code:
    upload the file 
    create directory
    create new file - scalemode.txt in directory (*function hint* fopen)
    for each line of uploaded file (*function hint* file)
         replace colons with semi colons (*function hint* str_replace)
         convert to hex (see google results)
         write line to new file (*function hint* fwrite)
    close uploaded file (*function hint* fclose)

  • #10
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Below is what I have so far. Now I need a little more help. I've already put in fopen and fclose a few lines down from the part labeled "UPLOAD PROCESS" within basic.php (I wasn't sure what parameter should be used, so I just used "r" for now. Should I be using "w" since the file will be overwritten with the new data once it's ready?). I'm not completely sure how I would go about processing each line of the opened file. Any suggestions?

    Thanks for everyone's help. I really do appreciate it.

    index.php:
    PHP Code:
    Select your account list below.<br><br>

    <
    form method="post" enctype="multipart/form-data" action="basic.php"
    <
    input type="file" name="userfile" size="27">
    <
    br><br>
    <
    input type="submit" value="Upload" name="upload">
    </
    form
    config.php:
    PHP Code:
    <?
    $domain 
    "domain.com/FOLDER";
    ?>
    basic.php:
    PHP Code:
    <? 
    include ('config.php');
    ?>

    <div class="text">
    <div align="center">

    <?
    // CHECK IF FILE HAS BEEN SELECTED
    if (!isset($_FILES['userfile']))

        echo 
    "<font color=\"white\" face=\"Geneva, Arial, Helvetica, sans-serif\">Error: You must select a file first.</font><br><br>\n";
        echo 
    "<center><a href=\"./index.php\">Back</a></center><br><br><br>";
        echo 
    "</div></div>";
        exit;


    if (
    is_uploaded_file($_FILES['userfile']['tmp_name'])) {

    if ((
    $_FILES['userfile']['type']=="plain/text"))
    {

    // CREATE RANDOM DIRECTORY
    $randnum rand(11000000000);
    $other time();
    $dirname $randnum.$other;
    $old_umask umask(0);
    mkdir("/path/to/my/server/FOLDER/".$dirname0777);

    // UPLOAD PROCESS
    $newfilename "scalemode.txt";
    $file $_FILES['userfile']['name'];
    $res move_uploaded_file($_FILES['userfile']['tmp_name'], "./" $dirname "/" $newfilename);

    $openfile fopen("./" $dirname "/scalemode.txt""r");
    fclose($openfile);

    if (!
    $res

        echo 
    "<font color=\"white\" face=\"Geneva, Arial, Helvetica, sans-serif\">Unknown Error: Please try again.</font><br><br>\n";
        echo 
    "<center><a href=\"./index.php\">Back</a></center><br><br><br>";
        echo 
    "</div></div>";
        exit;


    else 
    {
    ?>
    <br>
    <?

    // SET URL VARIABLES
    $urlf 'http://' .$domain .'/' .$dirname '/' $newfilename;

    ?>
    <script type="text/javascript">
    <!--
    function highlight(field)
        {
        field.focus();
        field.select();
        }
    //-->
    </script>
    <table border="0" width="100%">
    <tr><td class="imagehosting"><font color="white">DIRECT URL:</font></td><td><input type="text" name="thetext" onClick='highlight(this);' size="40" value="<? echo $urlf?>"></td></tr>
    </table><table border='0' width='100%'><tr><td><center><font color="white"><a href='./index.php'>Back</a></font></td></tr></table><br><br>

    <?
    }


    else 

        echo 
    "<font color=\"white\" face=\"Geneva, Arial, Helvetica, sans-serif\">Error: You must select an account list in .txt format.</font><br><br>\n";
        echo 
    "<center><a href=\"./index.php\">Back</a></center><br><br><br>";
        echo 
    "</div></div>";
        exit;
    }

    }
    ?>


    </div>
    </div>

  • #11
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I think I may have gotten something. Here is what I cam up with for the fopen/fclose from basic.php above.

    PHP Code:
    $openfile fopen("./" $dirname "/scalemode.txt""r");
    while (!
    feof($openfile))
        {
        
    $buffer fgets($openfile);
        
    $buffer_replace str_replace(":"";"$buffer);
        echo 
    $buffer_replace;
        }
    fclose($openfile); 
    I used the echo command to make sure that I'm doing it right, and the text shows up correctly. This processes each line and replaces colons with semicolons. Did I at least do that much correctly?

    Also, when using fopen, which parameter should I be using for opening "scalemode.txt"? I want to read the file, then overwrite it with the new content.

    Thanks again.

    UPDATE: Is there an easier way of doing this so that I don't have to move the uploaded file directly to the random directory, and name it as "scalemode.txt"? For example:

    In basic.php, I have it do this after the form is submitted:
    PHP Code:
    // UPLOAD PROCESS
    $newfilename "scalemode.txt";
    $file $_FILES['userfile']['name'];
    $res move_uploaded_file($_FILES['userfile']['tmp_name'], "./" $dirname "/" $newfilename); 
    How could I go about processing this file (replace all colons, and convert to hexadecimal), and then using fopen to create "scalemode.txt"? Or is there just a better way to set it up in general?
    Last edited by PutterPlace; 04-01-2007 at 07:20 AM.

  • #12
    New Coder
    Join Date
    Mar 2007
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I found a way to process the file, and then save the processed file to scalemode.txt using fopen. Now I'm working on the ascii to hex conversion. I'll post back if I have anymore problems.

    UPDATE: I've successfully finished this script. Thanks for all the help that everyone provided. This is DEFINITELY my favorite coding forum.
    Last edited by PutterPlace; 04-01-2007 at 11:23 PM.

  • #13
    Senior Coder
    Join Date
    Jan 2007
    Posts
    1,648
    Thanks
    1
    Thanked 58 Times in 54 Posts
    If this is your software you are creating a script for, then I think you need to fix the problem (function to create multiple users), not the symptom.

    Is this legacy software? Font tags? I thought we ditched those when 2000 passed.


  •  

    Posting Permissions

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