PHP School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a PHP Expert!

Join 307,140 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,751 people online right now. Registration is fast and FREE... Join Now!




Undefined index

2 Pages V  1 2 >  

Undefined index, PHP

oaluyi

4 Nov, 2009 - 10:56 PM
Post #1

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
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
CODE

<?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.gif

User is offlineProfile CardPM
+Quote Post


BGDeveloper

RE: Undefined Index

5 Nov, 2009 - 02:55 AM
Post #2

D.I.C Head
**

Joined: 1 Apr, 2009
Posts: 57



Thanked: 10 times
My Contributions
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.
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

5 Nov, 2009 - 04:20 AM
Post #3

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

jaql

RE: Undefined Index

5 Nov, 2009 - 05:01 AM
Post #4

D.I.C Head
**

Joined: 19 Oct, 2009
Posts: 59



Thanked: 5 times
My Contributions
Do this and see if the 'userfile' index exists because apparently it doesn't:
CODE

<?php
$uploadDir = 'C:/webroot/upload/';
if (isset($_POST['upload']))
{
  print '<pre>';
  print_r($_FILES);
  print '</pre>';
  die();
}


This post has been edited by jaql: 5 Nov, 2009 - 05:01 AM
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Undefined Index

5 Nov, 2009 - 07:44 AM
Post #5

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
CODE

<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 ...
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 02:32 AM
Post #6

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Undefined Index

6 Nov, 2009 - 05:22 AM
Post #7

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Can you show us your <form tag please??

Make sure that enctype="multipart/form-data" is specified.
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 06:02 AM
Post #8

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 09:31 AM
Post #9

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
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:

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.gif

Thanks,
PsychoCoder smile.gif
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Undefined Index

6 Nov, 2009 - 09:51 AM
Post #10

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

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

echo ini_get("file_uploads");
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 10:16 AM
Post #11

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
QUOTE(RudiVisser @ 6 Nov, 2009 - 09:51 AM) *

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.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Undefined Index

6 Nov, 2009 - 10:30 AM
Post #12

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
Well, check if it's off first..

What's the output of that code??

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



User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 11:11 AM
Post #13

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
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.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Undefined Index

6 Nov, 2009 - 11:48 AM
Post #14

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
?

Just create a new page with that in it, it will either give you a 1/0 (or maybe true/false).
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 12:44 PM
Post #15

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
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/>";
}
?>

User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Undefined Index

6 Nov, 2009 - 12:49 PM
Post #16

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
?

Make a new file, call it test.php or something..

Make the contents exactly this:
CODE
<?php
echo ini_get("file_uploads");
?>


Then visit the page, and post what is on it.
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 01:09 PM
Post #17

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
I had a blank screen when I clicked the Upload button.
User is offlineProfile CardPM
+Quote Post

RudiVisser

RE: Undefined Index

6 Nov, 2009 - 01:14 PM
Post #18

.. does not guess solutions
Group Icon

Joined: 5 Jun, 2009
Posts: 1,891



Thanked: 139 times
Dream Kudos: 125
Expert In: PHP, MySQL, HTML, CSS, C#

My Contributions
I'm sorry, I really don't know how to make what I said any clearer..
User is offlineProfile CardPM
+Quote Post

oaluyi

RE: Undefined Index

6 Nov, 2009 - 01:17 PM
Post #19

D.I.C Head
**

Joined: 4 Nov, 2009
Posts: 67


My Contributions
I had exactly this on the PHP page. I didn't add any other code to it. Is that what you meant?


<?php
echo ini_get("file_uploads");
?>
User is offlineProfile CardPM
+Quote Post

RPGonzo

RE: Undefined Index

6 Nov, 2009 - 01:22 PM
Post #20

// Note to self: hmphh .... I forgot
Group Icon

Joined: 16 Mar, 2009
Posts: 771



Thanked: 92 times
Dream Kudos: 25
My Contributions
blank screen could also mean false ... Rudi i see this from time to time on my local machine but not on live server ... just FYI

Sounds like its disabled
User is offlineProfile CardPM
+Quote Post

2 Pages V  1 2 >
Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 03:29PM

Live PHP Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

PHP Tutorials

Reference Sheets

PHP Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month