Undefined index

PHP

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

39 Replies - 15892 Views - Last Post: 23 October 2011 - 03:20 AM Rate Topic: -----

#1 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Undefined index

Posted 04 November 2009 - 11:56 PM

Hi, I get this error message on my server everytime I try to upload a file to my database.
Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 5

Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 6

Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 7

Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 8
Error uploading file.

The Code is
<?php
$uploadDir = 'C:/webroot/upload/';
if (isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
$filePath = $uploadDir . $fileName;
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}
 include 'config.php'; 
 include 'opendb.php';
if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());
}
?>
<?php
include 'closedb.php';
echo "<br>Files uploaded<br>";
?>


Can anyone please tell me what the unidentified index is all about on

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];


Thank you.

** edit ** :code:

Is This A Good Question/Topic? 0
  • +

Replies To: Undefined index

#2 BGDeveloper  Icon User is offline

  • D.I.C Head

Reputation: 16
  • View blog
  • Posts: 84
  • Joined: 01-April 09

Re: Undefined index

Posted 05 November 2009 - 03:55 AM

PHP is case sensitive. The warning you get means that your variable is not defined. Look up your HTML <form> again.
['Userfile'] and ['userfile'] may look the same, but are completely different.
Was This Post Helpful? 0
  • +
  • -

#3 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Re: Undefined index

Posted 05 November 2009 - 05:20 AM

Thank you very much for the reply. But this is the entire block of codes.

CREATE TABLE upload2 (
id INT NOT NULL AUTO_INCREMENT,
name VARCHAR(30) NOT NULL,
type VARCHAR(30) NOT NULL,
size INT NOT NULL,
path VARCHAR(60) NOT NULL,
PRIMARY KEY(id)
);


<form method="post" enctype="multipart/form-data" action="upload2.php">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>



<?php
$uploadDir = 'http://locallhost/CourseWork/upload.htm';

if(isset($_POST['upload']))
{
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";
exit;
}

include 'config.php';
include 'opendb.php';

if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'closedb.php';

echo "<br>Files uploaded<br>";

}
?>

I will be glad if you can spot out the error in the codes.

Thank you.
Was This Post Helpful? 0
  • +
  • -

#4 jaql  Icon User is offline

  • D.I.C Head

Reputation: 7
  • View blog
  • Posts: 73
  • Joined: 19-October 09

Re: Undefined index

Posted 05 November 2009 - 06:01 AM

Do this and see if the 'userfile' index exists because apparently it doesn't:
<?php
$uploadDir = 'C:/webroot/upload/';
if (isset($_POST['upload']))
{
  print '<pre>';
  print_r($_FILES); 
  print '</pre>';
  die();
}

This post has been edited by jaql: 05 November 2009 - 06:01 AM

Was This Post Helpful? 0
  • +
  • -

#5 RPGonzo  Icon User is offline

  • // Note to self: hmphh .... I forgot
  • member icon

Reputation: 151
  • View blog
  • Posts: 954
  • Joined: 16-March 09

Re: Undefined index

Posted 05 November 2009 - 08:44 AM

<form method="post" enctype="multipart/form-data" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>



<?php
error_reporting(E_ALL);

$uploadDir = '';

// if there is no post we don't need to continue
if(!isset($_POST['upload'])) { exit; }

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

$filePath = $uploadDir . $fileName;

$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
	echo "Error uploading file";
	exit;
}

include 'config.php';
include 'opendb.php';

if(!get_magic_quotes_gpc()) {
	$fileName = addslashes($fileName);
	$filePath = addslashes($filePath);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
		"VALUES ('$fileName', '$fileSize', '$fileType', '$filePath')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'closedb.php';

echo "<br>Files uploaded<br>";
$qry = "SELECT * FROM upload2";
$res = mysql_query($qry);
while ($obj = mysql_fetch_object($res)) {
	echo "<img src='$obj->path' height='50' alt='uploadedimage'/><br/>$obj->name(" . round($obj->size/1048576,2) . "MB)<br/>";
}
?>



I tested this and it works fine ... BUT your uploaddir had a filename in it .. that should just be a path not a filename ...

in this case i left it blank and it uploads to the same dir as the upload script ...
Was This Post Helpful? 0
  • +
  • -

#6 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Re: Undefined index

Posted 06 November 2009 - 03:32 AM

Thank you very much for your reply, but I still get the same error message

Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 9

Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 10

Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 11

Notice: Undefined index: userfile in C:\wamp\www\CourseWork\upload2.php on line 12
Error uploading file

These are the lines

$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

I seperated the php code from the html and changed the FORM ACTION...(I dont want to include PHP on the HTML document).

Well, I'm new to the world of PHP so I'm struggling with it lol.

One other Question, if I have to put the PHP on the HTML document, where on the html page will it be?

Thank you.
Was This Post Helpful? 0
  • +
  • -

#7 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Undefined index

Posted 06 November 2009 - 06:22 AM

Can you show us your <form tag please??

Make sure that enctype="multipart/form-data" is specified.
Was This Post Helpful? 0
  • +
  • -

#8 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Re: Undefined index

Posted 06 November 2009 - 07:02 AM

below is the form tag. Please

<form method="post" enctype="multipart/form-data" action="upload2.php">
<table width="350" border="0" cellpadding="1" cellspacing="1" class="box">
<tr>
<td width="246">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<input name="userfile" type="file" id="userfile">
</td>
<td width="80"><input name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>

Thank you. I can also show the entire block of code.
Was This Post Helpful? 0
  • +
  • -

#9 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Re: Undefined index

Posted 06 November 2009 - 10:31 AM

Hi guys, I am unable to resolve the "Undefined Index" problem on my PHP code. Below are the blocks of codes for both the HTML as well as the PHP. Thank you.

this is the HTML CODE:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<FORM ENCTYPE='multipart/form-data' action='uploadck.php' METHOD='POST'>
Upload this file: <INPUT NAME='file_up' TYPE="file">
<INPUT TYPE="submit" VALUE="Send File"></FORM>
</body>
</html>


This is the PHP code;
<?php

$name = $_FILES['file_up']['name'];
$size= $_FILES['file_up']['size'];
$type =$_FILES['file_up']['type'];
$tmp_name = $_FILES['file_up']['tmp_name'];
$error = $_FILES['file_up']['error'];

$file_upload='true';
$file_up_size=$_FILES['file_up'][$size];
echo $_FILES['file_up'][$name];
if ($_FILES['file_up'][$size]>250000){$error=$error."Your uploaded file size is more than 250KB so please reduce the file size and then upload. Visit the 

help page to know how to reduce the file size.<BR>";
$file_upload='false';}
if (!($_FILES['file_up'][$type] =="image/jpeg" OR $_FILES['file_up'][$type] =="image/gif")){$error=$error."Your uploaded file must be of JPG or GIF. Other 

file types are not allowed<BR>";
$file_upload='false';}

$file_name=$_FILES['file_up'][$name];
$add='uploaded/$file_name'; // the path with the file name where the file will be stored, upload is the directory name.
if($file_upload=='true'){

if(move_uploaded_file ($_FILES['file_up'][$tmp_name], $add)){
// do your coding here to give a thanks message or any other thing.
}else{echo "Failed to upload file Contact Site admin to fix the problem";}

}else{echo $error;}
?> 



I will be most happy if anyone can help to resolve the Undefined Index it is given me on

Quote

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 3

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 4

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 5

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 6

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 7

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 10

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 11

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 12

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 14

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 14

Notice: Undefined index: file_up in C:\wamp\www\CourseWork\uploadck.php on line 17


Admin Edit: Please use code tags when posting your code. Code tags are used like so => :code:

Thanks,
PsychoCoder :)
Was This Post Helpful? 0
  • +
  • -

#10 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Undefined index

Posted 06 November 2009 - 10:51 AM

Hmm, are file uploads actually enabled on your server??

echo ini_get("file_uploads");
Was This Post Helpful? 0
  • +
  • -

#11 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Re: Undefined index

Posted 06 November 2009 - 11:16 AM

View PostRudiVisser, on 6 Nov, 2009 - 09:51 AM, said:

Hmm, are file uploads actually enabled on your server??

echo ini_get("file_uploads");



Please, I dont know how to enable it. Can you give me a step by step instruction on how to do that?

Thank you.
Was This Post Helpful? 0
  • +
  • -

#12 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Undefined index

Posted 06 November 2009 - 11:30 AM

Well, check if it's off first..

What's the output of that code??

<?php
echo ini_get("file_uploads");
?>

Was This Post Helpful? 0
  • +
  • -

#13 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Re: Undefined index

Posted 06 November 2009 - 12:11 PM

Please, I am a beginner. Pardon me if I ask a stupid question. I just did that and I still get same errors. It is really frustrating! Please advice more. Thank you.
Was This Post Helpful? 0
  • +
  • -

#14 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Undefined index

Posted 06 November 2009 - 12:48 PM

?

Just create a new page with that in it, it will either give you a 1/0 (or maybe true/false).
Was This Post Helpful? 0
  • +
  • -

#15 oaluyi  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 67
  • Joined: 04-November 09

Re: Undefined index

Posted 06 November 2009 - 01:44 PM

hmmm....Same error message.

this is what I did.

<?php
error_reporting(E_ALL);

$uploadDir = 'uploaded/';
echo ini_get("file_uploads");

if(!isset($_POST['upload'])) { exit; }
$result = move_uploaded_file($_FILES['userfile']['name'], $uploadDir);
if (!$result) {
echo "Error uploading file";
exit;
}

include 'config.php';
include 'opendb.php';

if(!get_magic_quotes_gpc()) {
$name = addslashes($name);
$path = addslashes($path);
}

$query = "INSERT INTO upload2 (name, size, type, path ) ".
"VALUES ('$name', '$size', '$type', '$path')";

mysql_query($query) or die('Error, query failed : ' . mysql_error());

include 'closedb.php';

echo "<br>Files uploaded<br>";
$qry = "SELECT * FROM upload2";
$res = mysql_query($qry);
while ($obj = mysql_fetch_object($res)) {
echo "<img src='$obj->path' height='50' alt='uploadedimage'/><br/>$obj->name(" . round($obj->size/1048576,2) . "MB)<br/>";
}
?>
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3