0

Hi

I just wan to to pass the value of Input Type File html tag to a 2nd PHP page where I will insert the image in mysql but I am always getting a notice and isset() is not getting the $_FILE('IMAGE').
Here is the notice - Notice: Undefined index: IMAGE in C:\xampp\htdocs\billing\prodinsert.php on line 11
This is my HTML TAGS -

<body>
<hr />
<form id="form1" name="form1" method="post" action="prodinsert.php" enctype="multipart/form-data">
<input name="ICODE" type="text" size="10" maxlength="6" />
<input name="DESCR" type="text" size="50" maxlength="45" />
<input name="RATE" type="text" size="10" maxlength="9" />
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" />
    <input name="IMAGE" type="file" />
</form>

And this is my PHP code-

<?php
$host="localhost";
$user="root";
$pass="";
$db="bill";
mysql_connect($host, $user, $pass) OR DIE (mysql_error());
mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error());
$code=$_POST['ICODE'];
$descp=$_POST['DESCR'];
$rate=$_POST['RATE'];
$image=$_POST['IMAGE'];

if(!isset($_FILES[$image]))
{
    echo '<p>Please select a file</p>';
    echo $image;
}
else
{
    echo "File Uploaded";
    echo $image;
}

Where I am making mistake ??? Please guide me.

3
Contributors
7
Replies
9
Views
4 Years
Discussion Span
Last Post by vishalonne
0

I don't think $_POST['IMAGE'] will contain anything, and therefore $image won't have anything either. You need to use $_FILES['IMAGE'] instead. To see if a file was upload successfully you would do:

if ($_FILES['IMAGE']['error'] === UPLOAD_ERR_OK) {
    // ... handle the uploaded file ...
}

UPLOAD_ERR_OK means file was uploaded successfully. See http://php.net/manual/en/features.file-upload.errors.php for a list of the PHP upload file error codes that you can check for.

Also, here is a basic tutorial on how to do file uploads in PHP: http://www.tizag.com/phpT/fileupload.php

0

Hi Diafol
I made some changes in my code here it is

$code=$_POST['ICODE'];
$descp=$_POST['DESCR'];
$rate=$_POST['RATE'];
$image=$_FILES["IMAGE"]["name"];

if(!isset($_FILES[$image]))
{
    echo '<p>Please select a file</p>';
    echo $image;
}
else
{
    echo "File Uploaded";
    echo $image;
    ......

Now notice is gone but
still I am getting "Please select a file" message and the name of the image file
Suggest me what should I do

0

No it won't work. You're passing the $_FILES['IMAGE']['name'] variable (e.g. myfile.png) into the if block - parses as...

if(!isset($_FILES[myfile.png]))

WHich obviously makes no sense. If you have an enctype multipart/form-data with file input, you will always get a $_FILES array, whether a file is chosen or not. The trick is to read the 'error' item to decide whether a file has been successfully uploaded to the php tmp dir. Here's some sample code:

<?php
if(isset($_POST['send'])){
    if($_FILES['file']['error'] == 0){
        echo "File uploaded successfully: ";    
    }else{
        echo "Problem with upload: ";
    }
    print_r($_FILES);
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
    <input name="file" type="file" />
    <input name="send" value="Go" type="submit" />
</form>
</body>
</html>

Edited by diafol

0

OK that means I have to check write this block of PHP validation code on that PHP page where I am selecting the image file, I means to say on <form> ....</form> page. If am right please tell me.

0

No the validation code should be in the page described by your action attribute:

<form method="post" action="somepage.php" enctype="multipart/form-data">

I left action out for clarity and so that you could test out the code in just one page.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.