Hi everyone
I am having problems with handling files in PHP. I have some code that posts an announcement from a user which is shown below. I want to provide the option for the user to upload an associated file. In the bold section of the code i check to see if the user selected a file but i always get an error of "Undefined index: file" the value of file is set on the second page called
postannounce_form_inc.php which is shown at the bottom of this post.
PAGE postannounce.php
<?PHP
session_start();
include ($_SERVER['DOCUMENT_ROOT']. '/includes/functions.php');
if (!isset($_POST['submit'])) //if they haven't pressed the submit button, then show the form
{
require ($_SERVER['DOCUMENT_ROOT']. '/includes/header.php');
?>
<tr>
<td width="780" colspan="4" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<?PHP
include 'postannounce_form.inc.php'; //show form
//exit;
?>
</td>
</tr>
<?php require ($_SERVER['DOCUMENT_ROOT']. '/includes/footer.php');
}
else //they pressed the button so process their input EACH IF WILL NEED ITS OWN TABLE SETTINGS ETC
{
if (empty($_POST['subject']) || empty($_POST['content']))
{
require ($_SERVER['DOCUMENT_ROOT']. '/includes/header.php'); ?>
<tr>
<td width="780" colspan="4" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<?php
// Reshow the form with an error
$reg_error = 'Please enter a subject and some content';
include 'postannounce_form.inc.php';
?>
</td>
</tr>
<?php
require ($_SERVER['DOCUMENT_ROOT']. '/includes/footer.php');
exit;
}
// Everything is ok, upload the announcement
$subject = $_POST['subject'];
$content = $_POST['content'];
if ($_SESSION['userid'] = !""); //sets user var if a member is logged in
{
$user = $_SESSION['userid'];
}
if ($_SESSION['adminid'] = !""); //sets user var if an admin is logged in
{
$user = $_SESSION['adminid'];
}
$datetime = date('Y-m-d'); //sets date for recording when it was posted
[B]
if ( $_FILES['file'] ['name'] != "")
{
$title = $_POST['title'];
$description = $_POST['description'];
$catagory = $_POST['catagory'];
$uploadedFile = $_FILES['file']['tmp_name'];
$newFile = $GLOBALS['filepath'].$_FILES['file']['name'];
move_uploaded_file($uploadedFile, $newFile); //moves physical file
post_document ($title, $catagory, $description, $newFile, $datetime); //posts document if one is selected **
$docid = mysql_insert_id(); //this might need to go into the functions page in post_document but how to get the value here?
post_announce_file ($subject, $content, $newFile, $datetime, $user, $docid);
} [/B]else
{
post_announce ($subject, $content, $datetime, $user); //might fail if no file is uploaded as newfile is still referanced
}
echo 'Your announcement has been posted and will be shown once it has been approved , <a href="index.php">click here</a> to go back.';
}
?>
Page postannounce_form.inc.php
<tr>
<td width="780" colspan="4" border="1" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">
<br>
<b>Create an Announcement</b>
<br>
<p>Please enter the details below:</P>
<?php if (isset($reg_error)) { ?>
There was an error <?php echo $reg_error; ?>, please try again.
<?php } ?>
<form action="postannounce.php" method="post">
<b>Subject:</b> <input type="text" size="25" maxlength="100" name="subject" /><br />
<b>Content:</b> <input type="textbox" size="35" maxlength="500" name="content" /><br />
<br>
<p>If your announcement has an associated document enter its details below, otherwise leave the fields blank</p>
<br>
<p>
<b>Document Catagory:</b> <select name="catagory">
<option value="meeting">Meeting Minutes
<option value="event">Event Documents
<option value="other">Other Documents
</select> <br />
[B]<b>Select Document File:</b> <input type="file" name="file" /><br />[/B]
<br>
<input type="submit" name="submit" value="Post Announcement" /><br />
<br>
</form>
</td>
</tr>