...

View Full Version : How to post to a .txt file?



Noreply18
04-11-2012, 07:51 PM
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
// 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

mlseim
04-11-2012, 08:01 PM
I would say this line is wrong:
fwrite("$write","$email","$repassword","$passwords","$lastname");

Try this:

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

Fou-Lu
04-11-2012, 08:02 PM
There's no error handling in here at all.
Start by adding to the top:


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.

Noreply18
04-11-2012, 08:10 PM
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

mlseim
04-11-2012, 08:17 PM
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+form+image+submit+button&btnG=Search

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum