Forum Moderators: coopster

Message Too Old, No Replies

undefined index: uploadedfile in C:\wamp\www\Anand\upload.php line 9

         

An156

8:09 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



Please help me...
i am getting error undefined index: uploadedfile in C:\wamp\www\Anand\upload.php on line 9



<?php
if(isset($_REQUEST['sub']))
{ $target = "upload/";
echo basename($_FILES['uploaded']['name']);
$target = $target . basename( $_FILES['uploaded']['name']) ;
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target))
{
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
}
else
{
echo "Sorry, there was a problem uploading your file.";
}
}

else
{ ?> <html>
<body>
<form enctype="multipart/form-data" action="upload.php" method="POST">
Please choose a file: <input name="uploaded" type="file" /><br />
<input type="submit" value="Upload" name="sub" />
</form>
</body>
</html><?php
}?>

CyBerAliEn

8:22 pm on Apr 27, 2010 (gmt 0)

10+ Year Member



echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded"; 


Should be:

echo "The file ". basename( $_FILES['uploaded']['name']). " has been uploaded"; 




In your previous code, you used 'uploaded' as the key. But in this one line (and as indicated by your error), the index 'uploadedfile' doesn't exist.

Matthew1980

8:33 pm on Apr 27, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there cyberalien,

You type faster than me! ;-p

I will add one thing to this thread, whenever possible, try not to use the $_REQUEST global, as there are vulnerabilities with the data as it contains. There was a post about this not so long ago :)

Where possible, access the $_POST data directly from the $_POST array, less strain on overheads too :)

Your code: if(isset($_REQUEST['sub']))

could be done a little better by checking the key & value like this:-

if(isset($_POST['sub']) && ($_POST['sub'] == "Upload")){
//process data!
}

Good luck with the rest of the project :)

Cheers,
MRb

An156

3:30 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



i tried

if(isset($_POST['sub']) && ($_POST['sub'] == "Upload")){
//process data!
}

but still $_FILES['imagename']['name']; is empty

i even tried print_r($_FILES); and print_r($_REQUEST); but the request array is showing all values like product_category, product_subcategory, product_name, product_description & product_price except the file uploaded content.

plz help

An156

3:36 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



please consider this code for the error...


<?php session_start();

if(isset($_SESSION['session_admin']))
{ if(isset($_REQUEST['sub']))
{ $product_cat=$_REQUEST['cat1'];
$product_subcat=$_REQUEST['cat2'];
$product_name=$_REQUEST['product_name'];
$product_price=$_REQUEST['product_price'];
$product_description=$_REQUEST['product_description'];
print_r($_REQUEST);
echo "name"."<br>".$product_image_name=$_REQUEST['imagename'];
$target = "upload/";

function findexts ($filename)
{
$filename = strtolower($filename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$ext = findexts ($product_image_name) ;
if(($ext=="jpeg")||($ext=="jpg")||($ext=="gif")||($ext=="png"))
{ $newfile_name = $product_name.".".$ext;
$target = $target.$product_name.".".$ext;
if(file_exists("upload"))
{ }
else
{ mkdir("upload",0777,true);
}

$target = $target . basename( $_FILES['imagename']['name']) ;
if(move_uploaded_file($_FILES['imagename']['tmp_name'], $target))
{ echo "The file ". basename( $_FILES['imagename']['name']). " has been uploaded";
}
else { echo "Sorry, there was a problem uploading your file.";
}

}
else
{ echo "You cannot upload this file";
}

}

else if(isset($_REQUEST['combo']))
{?>
<form enctype="multipart/form-data" method="POST" action="add_product.php"><?php
$var=$_REQUEST['combo'];
$host = "localhost";$user = "root";$pass = "";
$query = "select cat_name from category where cat_id='$var'";
$db = mysql_connect($host,$user,$pass);
mysql_select_db("products",$db);
$result = mysql_query($query);
if(!$result){die(mysql_error());};
$rows = mysql_num_rows($result);
?>

<center>
<table class="tableformat" width="500" bgcolor="#FFFFFF">
<td><font face="Arial" size="2">Products Display&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=add_product.php>Add Product</a> | <a href=view_product.php>View Products</a></font><br />
<br /><br />
<table cellpadding="5" cellspacing="0">
<tr>
<td><label>Product Category</label></td>
<td>
<select name="cat1">
<?php //$query = "select cat_name from category where cat_id='$var'";
for($i=0; $i < $rows; $i++)
{
$name =mysql_result($result,$i,"cat_name");
echo "<option>$name</option>";
};
?></select></td></tr>
<tr><td><label>Product Sub-Category</label></td>
<td>
<select name="cat2"><?php
$query2 ="select subcat_name from subcategory where cat_id= $var";
$result2 = mysql_query($query2);
$rows2 = mysql_num_rows($result2);
for($i=0; $i < $rows2; $i++) {
$name =mysql_result($result2,$i,"subcat_name");
echo "<option>$name</option>";
};
?>
</select>
</td>
</tr>
<tr><td><label>Product Name</label></td><td><input type="text" name="product_name" size="33"/></td></tr>
<tr><td><label>Product Price</label></td><td><input type="text" name="product_price" size="33"/></td></tr>
<tr><td><label>Product Description</label></td><td><textarea rows="4" cols="25" name="product_description"></textarea><div class="clear"></div></td></tr>
<tr><td><label>Product Image</label></td><td><input type='file' name=imagename size=25/></td></tr>
<tr><td colspan="2" align="right"><input type="submit" name="sub" class="button" Value="Submit"/></td></tr>
</table></td></table></center></form>
<?php
}

else if(!isset($_REQUEST['combo']))
{
$host = "localhost";$user = "root";$pass = "";
$query = "select cat_id,cat_name from category";
$db = mysql_connect($host,$user,$pass);
mysql_select_db("products",$db);
$result = mysql_query($query);
if(!$result){die(mysql_error());};
$rows = mysql_num_rows($result);
?>
<form enctype="multipart/form-data" action="add_product.php" method="POST">
<center>
<table class="tableformat" width="500" bgcolor="#FFFFFF">
<td><font face="Arial" size="2">Products Display&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href=add_product.php>Add Product</a> | <a href=view_product.php>View Products</a></font><br />
<br /><br />
<table cellpadding="5" cellspacing="0">
<tr>
<td><label>Product Category</label></td>
<td>
<select name="combo" onChange="submit();"><option>Select Category</option>
<?php $query = "select cat_id,cat_name from category";
for($i=0; $i < $rows; $i++)
{ $id =mysql_result($result,$i,"cat_id");
$name =mysql_result($result,$i,"cat_name");
echo "<option value=\"$id\">$name</option>";
};
?></select></td></tr>
<tr><td><label>Product Sub-Category</label></td>
<td>
<select ><option>Sub-Category</option>
</td>
</tr>
<tr><td><label>Product Name</label></td><td><input type="text" name="asdas" size="33"/></td></tr>
<tr><td><label>Product Price</label></td><td><input type="text" name="sda" size="33"/></td></tr>
<tr><td><label>Product Description</label></td><td><textarea name="area" rows="4" cols="25"></textarea></td></tr>
<tr><td><label>Product Image</label></td><td><input type="file" name="file" /></td></tr>
<tr><td colspan="2" align="right"><input type="submit" class="button" Value="Submit"/></td></tr>
</table></form>
<?php
}
}
else
{ echo "<font face=Arial color=2 size=2>Session Expired : Please Contact Administrator</font>";
}
?>


Thanks in advance
Anand

rocknbil

5:40 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<input type="file" name="file" />

So why are you expecting $_FILES['uploadedfile'] to be posted? :-)

Change one or the other and it should start to resolve.

Edit: Just saw your other thread, the answer to that Q is that you were looking for file in $_REQUEST, and it won't even be in $_POST. It's in $_FILES.

$_FILES['file']['name']

You're on the right track here, you just need to make the connection.

An156

5:53 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



if(isset($_POST['sub']))

{ echo $product_cat=$_REQUEST['cat1'];
echo $product_subcat=$_REQUEST['cat2'];
echo $product_name=$_REQUEST['product_name'];
echo $product_price=$_REQUEST['product_price'];
echo $product_description=$_REQUEST['product_description'];
echo $product_image_name=$_POST['file']; // everything is displaying except this line... and i am also getting the undefined index:file in this line

}


everything is displaying except this line...

echo $product_image_name=$_POST['file'];

and i am also getting the undefined index:file in this line

Please help :(

rocknbil

6:09 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



echo $product_image_name=$_POST['file'];


Re-read the above post. 'file' will not be in $_POST. It's in $_FILES. But I just noticed something else, and I'm not sure which process this is coming from. You have two upload fields.

<input type="file" name="file" />
<input type="file" name="imagename" size=25/>

Though I don't know why you'd want to, and depending on which one you're referencing, this should set it right:

$product_image_name=$_FILES['file']['name'];
echo "$product_image_name <br>\n";

OR

$product_image_name=$_FILES['imagename']['name'];
echo "$product_image_name <br>\n";

See how that works?

[edited by: rocknbil at 6:12 pm (utc) on Apr 28, 2010]

An156

6:10 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



hey thanks buddies...
PROBLEM SOLVED!
what i was doin is
echo $product_image_name=$_POST['file'];
which was giving the undefined error:file
i tried this and it worked

echo $product_image_name=basename( $_FILES['file']['name']);

thanks

Matthew1980

6:27 pm on Apr 28, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there An156,

<tr><td><label>Product Image</label></td><td><input type='file' name=imagename size=25/></td></tr>


doesn't match this code:-

echo $product_image_name=$_FILES['file']['name']; // everything is displaying except this line... and i am also getting the undefined index:file in this line


The quoted part is off the first form, not the second.

This should stop the error.

The names have to match :) as rocknbil pointed out previously..

IE:

<input type="text" name="zip_code">

Will translate when posted/submitted into:-

$_POST['zip_code'];

The names need to marry up, or the parser will throw an error, this is fundamental to the form code working

Cheers,
MRb

An156

6:45 pm on Apr 28, 2010 (gmt 0)

10+ Year Member



hey rocknbill...
u suggested the right thing man..
i really appreciated ur help
thanks