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 5 of 5
  1. #1
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    How to post to a .txt file?

    This php code is supposed to save the data inside a .txt file, but the problem is that it doesnt. No error shows up, the code just skips that process and jumps over to the next process(redirects to another website).

    Whats the problem?

    You can find the HTML source here:
    http://zete.net23.net/
    Just right click "source", and find the <form>
    PHP Code:
    <?php
    // Get the name they entered in the form
    // We'll be naming the file this
    $file $_POST['name'];
    // Get the email from the form
    $lastname $_POST['lname'];
    $passwords $_POST['password'];
    $repassword $_POST['password2'];
    $email$_POST['email'];


    // We want the file to be a text file right?
    $ex ".txt";
    // Try to open a file named $file$ex (johndoe.txt for example)
    // Because this file doesn't exist yet the server creates it
    $write fopen("$file$ex","w");
    // Now open the file up again but this time save the email in it
    fwrite("$write","$email","$repassword","$passwords","$lastname");
    // MAKE SURE you close the file!!!
    fclose($write);
    // The folder that this script is in on the server is where the file we just made was saved
    // We can 'rename' it to another folder
    // The folder on the server we want to move it to
    $data "../emails/";
    // Now put it all together: This example goes out of the folder we're in and into the folder 'emails'
    // The new 'name' would be this now (../emails/johndoe.txt): So now the file is moved to where we want for storage
    rename ("$file","$data$file$ex");
    // The script is done, send the user to another page (Just read the address below and you'll get it)
    // Its just an example fyi change to what you want
    header('Location:http://www.vg.no');
    exit();
    ?>
    By the way, im new to this forum :P maybe im staying, it depends :P

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I would say this line is wrong:
    fwrite("$write","$email","$repassword","$passwords","$lastname");

    Try this:

    fwrite($write,$email.",".$repassword.",".$passwords.",".$lastname);

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    There's no error handling in here at all.
    Start by adding to the top:
    PHP Code:
    ini_set('display_errors'1);
    error_reporting(E_ALL); 
    fwrite is wrong. It only accepts two required arguments, the file pointer defined here by $write, and a string to write to the file (and it will accept a third is the length, but it is optional). That likely should be a concatenated string composed of those five pieces. That will be the cause of the errors.

    On a side note, this is terrible insecure. Do not use this on a live environment as it provides full access to any .txt file writable by the apache user including any other domains hosted by that machine.

  • Users who have thanked Fou-Lu for this post:

    Noreply18 (04-11-2012)

  • #4
    New to the CF scene
    Join Date
    Apr 2012
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by mlseim View Post
    I would say this line is wrong:
    fwrite("$write","$email","$repassword","$passwords","$lastname");

    Try this:

    fwrite($write,$email.",".$repassword.",".$passwords.",".$lastname);
    It works!

    Thanks bro!

    But one more question..How do i put a image as a submit button?

    Fouf-fou:

    Yeah, i know its not secure, but im just a learning student, so im experimenting at the time :P

  • #5
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,495
    Thanks
    8
    Thanked 1,089 Times in 1,080 Posts
    I'm not trying to be a wise-guy here, but you should be using Google.
    Ask your questions, or search for similar terms ... like this:
    http://www.google.com/search?q=html+...on&btnG=Search

    You'll get instant help, without relying on a forum.


  •  

    Posting Permissions

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