Submitting text and uploaded files

I’m trying to figure out how to allow users to be able to input text and also allow them to attach pictures before submitting. It’s exactly like composing a gmail; you append pictures, input some body text, and send it to the user. i’m trying to do the same, but keep on getting an undefined index: uploadedfile.


<?php
/* This script adds a blog entry to the database. */
 include('inc/header.inc');
 include('inc/menu.inc'); 

$msg = "";
require_once('inc/config.inc'); //Include the configuration file for error management and such.

if(isset($_POST['submit'])) { //Handle the form.

	require_once ('scripts/mysql_connect.php'); //Connect to the database.

	//Validate the form data:
	if(!empty($_POST['name'])) {
		$n = escape_data($_POST['name']);
		$msg = "";
	} else {
		$n = FALSE;
		$msg .= '<p><font color="red" size="1">Please enter a name!</font></p>';
	} 

	if (!empty($_POST['title']) && (!empty($_POST['entry']))) {
		$t = mysql_real_escape_string(trim($_POST['title']));
		$e = mysql_real_escape_string(trim($_POST['entry']));
		$msg ="";
	} else {
		$msg .= '<p><font color="red" size="1">Please submit both a title and an entry.</font></p>';
		$t = FALSE;
		$e = FALSE;
	}
$uploadDir = 'members/' .$n. '/';

	if($n && $t && $e) {
		//Define the query:
		$query = "INSERT INTO stories(name, title, entry, date_entered) VALUES ('$n', '$t', '$e', NOW())";
		
				    mkdir("members/$n", 0755); // Create directory(folder) to hold each user's files(pics, MP3s, etc.)	
					
$fileName = $_FILES['uploadedfile']['name'];
$tmpName = $_FILES['uploadedfile']['tmp_name'];
$fileSize = $_FILES['uploadedfile']['size'];
$fileType = $_FILES['uploadedfile']['type'];

$filePath = $uploadDir . basename($fileName);

if (move_uploaded_file($tmpName, $filePath)) {
    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 
    " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";

exit;
}
	
	//Execute the query:
	if(@mysql_query($query)) { $msg .= '<p><font color="blue" size="2">The blog entry has been added!</font></p>';
	} else {
		print '<p style="color: red;">Could not add the entry because:<br/>' . mysql_error() . ' .</p><p>The query being run was: ' . $query . '</p>';
	}
} // No problem!

	mysql_close();

} //End of form submission IF.
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title></title>
<meta name="keywords" content="" />
<meta name="Instant " content="" />
<link href="default.css" rel="stylesheet" type="text/css" media="screen" />
</head>
<body>

<div id="wrapper">
<!-- start page -->
<div id="page">
	<!-- start content -->
	<div id="content">
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<p>Your name: <input type="text" name="name" size="30" maxsize="50" /></p>
<p>Entry title: <input type="text" name="title" size="30" maxsize="100" /></p>

<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" id="uploadedfile"/><br /><br />

<textarea name="entry" cols="75" rows="15"></textarea><br /><br />
<div align="right"><input type="submit" name="submit" value="Post This Entry!" id="submit"/></div>
<?php print "$msg"; ?>
</form>
		</div>
	</div>
	<!-- end content -->
	<div style="clear: both;">&nbsp;</div>
</div>
<!-- end page -->
</div>
<?php include('inc/footer.inc'); ?>
</body>
</html>

I didn’t read all your code, but I did find one big error.


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">

Change to this:


<form action="<?php echo $_SERVER['PHP_SELF']; ?>" enctype="multipart/form-data" method="post">

thanks jeremy for the reply! the file upload works great now, but i have another problem. when there is no file being upload and i hit submit, the text doesn’t go into the database.

I would say your problem is here:


if (move_uploaded_file($tmpName, $filePath)) {

    echo "The file ".  basename( $_FILES['uploadedfile']['name']). 

    " has been uploaded";

} else{

    echo "There was an error uploading the file, please try again!";


exit;

}

Removing the exit; will sort the problem ?

You may need to rethink your logic or do different things in a different order; this is where a simple upload file starts to explode in size !

thanks rubble for the response, but removing exit didn’t resolve anything.

i wonder, does anyone know where i can find codes like composing an email where users can input text and also upload files because that is essentially what i’m trying to do.

Sweet, I got it to work properly now. Turns out I needed to reorder the structure. So, here is the code that I modified:


    if($n && $t && $e) { 
        //Define the query: 
        $query = "INSERT INTO stories(name, title, entry, date_entered) VALUES ('$n', '$t', '$e', NOW())"; 
         			
    //Execute the query: 
    if(@mysql_query($query)) { $msg .= '<p><font color="blue" size="2">The blog entry has been added!</font></p>'; 
    } else { 
        print '<p style="color: red;">Could not add the entry because:<br/>' . mysql_error() . ' .</p><p>The query being run was: ' . $query . '</p>'; 
    }
	
	if(!empty($_FILES['uploadedfile'])) {         
		mkdir("members/$n", 0755); // Create directory(folder) to hold each user's files(pics, MP3s, etc.)     
		include('upload_file.php');				
	}
} 

    mysql_close(); 

} //End of form submission IF.