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.

Dani AI

Generated

Quick summary and the actual bug: file inputs are not sent in $_POST — they arrive in $_FILES. In this thread and were right to point that out. The concrete mistake in 's later code was assigning the uploaded filename to $image and then checking isset($_FILES[$image]). That checks an index like "myfile.png", not the field name "IMAGE", so it always fails.

Practical checklist and safe flow to handle uploads (server side only):

  • Ensure the form uses method="post" and enctype="multipart/form-data".
  • Inspect $_FILES['IMAGE'] and its error, tmp_name, size, and name fields.
  • Confirm the upload actually arrived with is_uploaded_file() and then move it with move_uploaded_file() to a writable directory.
  • Validate size and type on the server; treat the client-side MAX_FILE_SIZE as advisory only.
  • Make unique, sanitized filenames before saving to avoid collisions and directory traversal.

Example (minimal, not identical to earlier snippets):

if (!empty($_FILES['IMAGE']) && $_FILES['IMAGE']['error'] === 0) {
    $tmp  = $_FILES['IMAGE']['tmp_name'];
    $name = preg_replace('/[^A-Za-z0-9._-]/', '_', basename($_FILES['IMAGE']['name']));
    $dst  = __DIR__ . '/uploads/' . time() . '_' . $name;
    if (is_uploaded_file($tmp) && move_uploaded_file($tmp, $dst)) {
        // $dst is safe to store (store path, not raw file in DB)
    }
}

Other important notes: check php.ini settings (file_uploads, upload_max_filesize, post_max_size), ensure the uploads folder is not web-executable, and avoid the old mysql_* API — use mysqli or PDO with prepared statements and store paths instead of raw blobs unless there is a strong reason.

Recommended Answers

All 7 Replies

Member Avatar for Member #120589
$image=$_POST['IMAGE'];

No such thing. It's the $_FILES['IMAGE'] you need.

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

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

Member Avatar for Member #120589

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>

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.

Member Avatar for Member #120589

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.

Problem Solved Diafol. I'm a great FAN your knowledge. Than you very much

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.