Forum Moderators: coopster
I've signed below where is line 82.
I dont know why I get this error since everything seems to be clear and curly brakets are well.
<html>
<head>
<title>upload</title></head>
<body>
<?
function error_bool($error, $field) {
if($error[$field]) {
print("<td style=color:red>");
}
else {
print("<td>");
}
}
function show_form() {
global $print_again, $error;
?>
<form name="upload" method="post" enctype="multipart/form-data">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td>
<?php error_bool($error, "subject_code"); ?> Subject Code: </td><td><input type="text" name="subject_code" id="subject_code" value="<?php echo $_POST["subject_code"]; ?>">
</td></tr><tr><td>
<?php error_bool($error, "subject_name"); ?>Subject Name: </td><td><input type="text" name="subject_name" id="subject_name" value="<?php echo $_POST["subject_name"]; ?>">
</td></tr><tr><td>
<?php error_bool($error, "lecturer"); ?>Lecturer: </td><td> <input type="text" name="lecturer" id="lecturer" value="<?php echo $_POST["lecturer"]; ?>">
</td></tr><tr><td>
Department: </td><td>
<select name= "department" id="department" compulsory="yes">
<option value = "" SELECTED>Select
<option value = "IS">Information System
<option value = "GM">Graphics & Multimedia
<option value = "SN">Systems Networking
<option value = "SE">Software Engineering
</select>
</td></tr><tr><td>
Year of exam: </td><td>
<select name= "year" id="year" compulsory="yes">
<option value = "" SELECTED>Select
<option value = "2004">2005-06
<option value = "2005">2005-06
<option value = "2006">2006-07
<option value = "2007">2007-08
<option value = "2008">2008-09
</select>
</td></tr><tr><td>
Semester: </td><td>
<select name= "semester" id="semester" compulsory="yes">
<option value = "" SELECTED>Select
<option value = "1">One
<option value = "2">Two
<option value = "special">Special
</select>
</td>
</tr><tr><td><input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
77 </tr>
78 </table>
79 </form>
80
81 <?php
82 }
83 if(isset($_POST["Submit"])) {
84 check_form();
} else {
show_form();
}
extract($_POST);
function subject_code($subject_code)
{
if(!preg_match("/[^a-zA-Z0-9]+$/s",$subject_code))
return TRUE;
else
return FALSE;
}
function subject_name($subject_name)
{
if(!preg_match("/[^a-zA-Z\ ]+$/s",$subject_name))
return TRUE;
else
return FALSE;
}
function lecturer($lecturer)
{
if(!preg_match("/[^a-zA-Z\ ]+$/s",$lecturer))
return TRUE;
else
return FALSE;
}
function check_form()
{
global $error, $print_again;
$error['subject_code'] = false;
$error['subject_name'] = false;
$error['lecturer'] = false;
$error['department'] = false;
$error['semester'] = false;
$error['year'] = false;
if($_POST["subject_code"]=="") {
$error['subject_code'] = true;
$print_again = true;
$message="The subject code field is empty<br>";
}
if($_POST["subject_name"]=="") {
$error['subject_name'] = true;
$print_again = true;
$message="The subject name field is empty<br>";
}
if($_POST["lecturer"]=="") {
$error['lecturer'] = true;
$print_again = true;
$message="The lecturer field is empty<br>";
}
if($_POST["department"]=="") {
$error['department'] = true;
$print_again = true;
$message="The department field is empty<br>";
}
if($_POST["semester"]=="") {
$error['semester'] = true;
$print_again = true;
$message="The semester field is empty<br>";
}
if($_POST["year"]=="") {
$error['year'] = true;
$print_again = true;
$message="The year field is empty<br>";
}
if(!subject_code($_POST['subject_code'])) {
$error['subject_code'] = true;
$print_again = true;
$message="Invalid subject code <br>";
}
if(!subject_name($_POST['subject_name'])) {
$error['subject_name'] = true;
$print_again = true;
$message="Invalid name <br>";
}
if(!lecturer($_POST['lecturer'])) {
$error['lecturer'] = true;
$print_again = true;
$message="Invalid lecturer <br>";
}
if($print_again) {
show_form();
} else {
show_form();
include_once "connection.php";
$uploadDir = 'C:/wamp/www/uploads/';
if(isset($_POST['upload']))
{
$fileName = $_REQUEST[subject_name];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// get the file extension first
$ext = ".pdf";
// make the file name
$uniqueName = $_REQUEST[subject_code] ."-". $_REQUEST[year] ."-". $_REQUEST[semester];
//the unique file name for the upload file
$filePath = $uploadDir . $uniqueName . $ext;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "<br>Error uploading file";
exit
}
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO exam_papers (subject_code, subject_name, lecturer, department, semester, year_of_exam, file_path) ".
"VALUES ('$_REQUEST[subject_code]','$fileName','$_REQUEST[lecturer]','$_REQUEST[department]','$_REQUEST[semester]','$_REQUEST[year]', '$filePath')";
mysql_query($query) or die('<br>Error, query failed : ' . mysql_error());
echo
"
You have uploaded 1 exam paper with following information:<p>
Subject Code: $subject_code<br>
Subject Name: $subject_name<br>
Lecturer: $lecturer<br>
Department: $department<br>
Semester: $semester<br>
Year Of Exam: $year<br>
Location: $filePath<br>
";
}
$message=" You have uploaded 1 exam paper with following information:<p>
Subject Code: $subject_code<br>
Subject Name: $subject_name<br>
Lecturer: $lecturer<br>
Department: $department<br>
Semester: $semester<br>
Year Of Exam: $year<br>
Location: $filePath<br>";
}
echo "$message";
}
?>
</body>
</html>
What I have seen here is that the lines:
83 if(isset($_POST["Submit"])) {
84 check_form();
} else {
show_form();
}
extract($_POST);
Actually sit outside any function. It is the only code that is not included in a function.
As I said I'm only a novice here but seems strange to me.
Maybe that helped. Hope so.
Dave
<form name="upload" method="post" enctype="multipart/form-data">
should be
<form name="upload" method="post" action="$_SERVER["PHP_SELF"]" enctype="multipart/form-data">.
Keep me posted please.
Dave
<form name="upload" method="post" action="$_SERVER["PHP_SELF"]" enctype="multipart/form-data">
Not using the <?php ?> tags
I have now loaded this onto my apache server on Fedora Core 8
Error log tells me you have a missing ; on the exit word I marked it with <--------
//the unique file name for the upload file
$filePath = $uploadDir . $uniqueName . $ext;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "<br>Error uploading file";
exit <------- ;
}
After adding the "action" and the ; I get something in the web browser.
Good luck, please keep me posted.
Dave
The code below works but it uses a separate php file to do the processing
uploadtest.html
<html>
<head></head>
<body>
<form name="upload" method="post" action="uploadscript.php" enctype="multipart/form-data">
<input name="userfile" type="file" id="userfile">
<input name="upload" type="submit" class="box" id="upload" value=" Upload ">
</body
</html>
uploadscript.php
<?php
$filename = $_FILES['userfile']['name'];
printf($filename);
?>
The error that you are getting on your main script is a "file not found" error. You need to look at your logic for file upoad.
Try creating a separate file test.php and get just the file upload working and then replace the code in the main file with the known good from the test file.
Also you need to be reading your error.log more all the information gets printed there..
If you have PHP installed on your local Windows PC, try using PHP Designer 2007 which is available free, just google for it, under the tools menu it has a DEBUG tool, I have never used the DEBUG as I run my PHP on a linux server.
Good luck
Dave