Making an upload form with email and name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gavin1084
Forum Newbie
Posts: 3
Joined: Tue May 13, 2008 7:18 pm

Making an upload form with email and name

Post by gavin1084 »

I'm trying to make an upload form that has not only the file on it, but also has an email address and name of the person uploading. I've got a form that has the Name, Email, File slots, but I can't get the PHP to send all the information on the form to the folder in my host's file manager. This is my form code in the website html:

<form action="upload_file.php" method="post"
enctype="multipart/form-data"><br>
<label for="name">Name:</label> &nbsp;
<input name="Name:" id="name" type="text"> <br> <br>
<label for="email">Email:</label>&nbsp;
<input name="Email:" id="email" type="text"> <br> <br>
&nbsp;
<label for="file">Select File:</label> &nbsp;
<input name="file" id="file" type="file">&nbsp;
<input name="submit" value="Submit" type="submit"></form>

My PHP as of now, allows images and Word documents to upload only and then forwards you to a thank you page. All of that works fine, but the Name and Email don't go to my upload/ folder as specified. Here is my PHP.

<?php
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/jif")
|| ($_FILES["file"]["type"] == "application/msword")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 1000000))
{
if ($_FILES["file"]["error"] > 0)
{
header( 'Location: http://inizioriders.startlogic.com//sub ... error.html' ) ;
}

else
{

if (file_exists("upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
header( 'Location: http://inizioriders.startlogic.com//sub ... nkyou.html' ) ;
}
}
}
else
{
header( 'Location: http://inizioriders.startlogic.com//sub ... error.html' ) ;
}
?>


Any suggestions for how to get it to upload everything?

Also, is there a way to get the uploaded files forwarded to an email account or somehow notify me that someone has uploaded something? Thank you!
JacobT
Forum Commoner
Posts: 43
Joined: Tue May 13, 2008 11:07 am
Location: Los Angeles, CA

Re: Making an upload form with email and name

Post by JacobT »

Hey Gavin, I don't see where you have any PHP code that handles the Name or Email fields.

You're missing any reference to the $_POST variable for $_POST['Name:'] or $_POST['Email:']. Also, just my opinion, but it's bad practice to put semicolon's or any other non-alphanumeric character in your field names. It can lead to security issues if you're not cleaning your data properly after it's submitted.

You need something like this:

Code: Select all

else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
 
$fp = fopen("upload/".$_FILES["file"]["name"].".txt", "w");
fwrite($fp, "Name: ".$_POST["Name"]." Email: ".$_POST["Email"]);
fclose($fp);
 
header( 'Location: http://inizioriders.startlogic.com//sub ... kyou.html' ) ;
}
This assumes you've removed the semi-colons from your field names. This will create a TXT file with the same name, in the same directory, as your uploaded file just with .txt added onto the end.

To get it to notify you, you can add this piece of code BEFORE your header() call:

Code: Select all

mail('youremail@domain.com', 'File Uploaded - '.$_FILES['file']['name'], 'The file was uploaded.');
Hope this helps!
gavin1084
Forum Newbie
Posts: 3
Joined: Tue May 13, 2008 7:18 pm

Re: Making an upload form with email and name

Post by gavin1084 »

That worked really well, except for one thing:

The txt file that gets made doesn't have the submitted entry, just "Name: Email: "

here's the PHP again:

$fp = fopen("upload/".$_FILES["file"]["name"].".txt", "w");
fwrite($fp, "Name: ".$_POST["Name"]." Email: ".$_POST["Email"]);
fclose($fp);

It's probably just something little, any ideas?
JacobT
Forum Commoner
Posts: 43
Joined: Tue May 13, 2008 11:07 am
Location: Los Angeles, CA

Re: Making an upload form with email and name

Post by JacobT »

What do you mean the "submitted entry"? The file you're uploading is the submitted entry isn't it? In which case the file is saved in the same folder. The code I provided was only to save the Name and Email in a file with a similar name as the one that was uploaded. You can't save the JPG inside the TXT file.

If you're referring to just adding the filename inside the TXT file as well, then you can just change

Code: Select all

fwrite($fp, "Name: ".$_POST["Name"]." Email: ".$_POST["Email"]);
to

Code: Select all

fwrite($fp, "Name: ".$_POST["Name"]." Email: ".$_POST["Email"]." Filename: ".$_FILES["file"]["name"]);
The rest is up to you...
gavin1084
Forum Newbie
Posts: 3
Joined: Tue May 13, 2008 7:18 pm

Re: Making an upload form with email and name

Post by gavin1084 »

Hey sorry for the confusion,

What I meant was that in the txt file, instead of showing the person's actual name and email that they type in the Name and Email fields, the file contains only the words "Name: Email:" with nothing following

(ex. I see "Name: Email:" instead of "Name: John Email: john@something.com")

I don't need the filename to be in the txt that is sent along with the uploaded file. That part all worked just fine. I just need to be able to see the person's name and email that they entered in the form.

here's the form html:
<form action="upload_file.php" method="post"
enctype="multipart/form-data"><br>
<label for="name">Name</label> &nbsp;<input
name="name" id="name" type="text"> <br>
<br>
<label for="email">Email</label>&nbsp; <input
name="email" id="email" type="text"><br>
<br>
&nbsp;<label for="file">Select File</label>
&nbsp;
<input name="file" id="file" type="file">&nbsp;
<input name="submit" value="Submit" type="submit"></form>

and here's the PHP lines:

else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);

$fp = fopen("upload/".$_FILES["file"]["name"].".txt", "w");
fwrite($fp, "Name:" . $_POST["Name"] . "Email:" . $_POST["Email"]);
fclose($fp);


Sorry again and thanks for all the help!
JacobT
Forum Commoner
Posts: 43
Joined: Tue May 13, 2008 11:07 am
Location: Los Angeles, CA

Re: Making an upload form with email and name

Post by JacobT »

Hey Gavin, no worries, basically just change the Name and Email variables inside the $_POST[] to lowercase. My code assumed all you did was remove the semi-colon, but it appears you also made them lowercase (i.e. Name: became name) .. PHP is case sensitive for a lot of things.

Change that and you should be good to go. Cheers!
Post Reply