Hi Friends,

I am using this code to upload image but it seems my call is not getting inside function. I put some echo commands for debugging. all I get output is after selecting file "hello entering function". but do not get other echo command print.
Neither insert command is executed.

Can you suggest what is wrong here.

     <td><div align="left">Upload Image:(bmp,gif,jpg,png)</div></td>
<td><input name="uploadedimage" type="file" id="uploadedimage">  
    <input type="submit" name="uploadedimage" value="Upload Image" align="center"style="background-color: transparent;color:#F7F7F7;font-size:14px;font-weight:bold; border: none;text-decoration: underline;cursor: hand;cursor: pointer;">
<?php

if(isset($_POST['uploadedimage'])) 
{ 
// start of image
require "connection.php";
echo "debug hello entering function";
function GetImageExtension($imagetype)
     {
echo "debug1";
       if(empty($imagetype)) return false;
echo "debug2";
     switch($imagetype)
       {

           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }

if (!empty($_FILES["uploadedimage"]["name"]))
 {
    $file_name=$_FILES["uploadedimage"]["name"];

    $temp_name=$_FILES["uploadedimage"]["tmp_name"];

    $imgtype=$_FILES["uploadedimage"]["type"];

    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    echo "debug3";
    $target_path = "image/".$imagename;

if(move_uploaded_file($temp_name, $target_path)) {
    $query_upload="INSERT into `images_index` (images_path) VALUES
('".$target_path."')";
    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); 
echo "INSERT into `images_index` (images_path,submission_date) VALUES
('$target_path')";
echo "debug4";
}
else{
  exit("Error While uploading image on the server");
}
 }


}

Can you put an echo after this line:
if (!empty($_FILES["uploadedimage"]["name"]))
to prove this IF condition is being met?

Thank for your suggestion.

I have now added echo with numbers after every line to see call steps.

and here is my output after uploading.

hello entering function456718913Error While uploading image on the server

Kindly suggest.

Check code attached for numbers

   <td><div align="left">Upload Image:(bmp,gif,jpg,png)</div></td>
<td><input name="uploadedimage" type="file" id="uploadedimage">  
    <input type="submit" name="uploadimage" value="Upload Image" align="center"style="background-color: transparent;color:#F7F7F7;font-size:14px;font-weight:bold; border: none;text-decoration: underline;cursor: hand;cursor: pointer;">

   </form>
</div>

    <?php
    require "connection.php";
if(isset($_POST['uploadimage'])) 
{ 
// start of image

echo "hello entering function";
function GetImageExtension($imagetype)
     {
echo "1";
       if(empty($imagetype)) return false;
echo "2";
     switch($imagetype)
       {

           case 'image/bmp': return '.bmp';
           case 'image/gif': return '.gif';
           case 'image/jpeg': return '.jpg';
           case 'image/png': return '.png';
           default: return false;
       }
     }

if (!empty($_FILES["uploadedimage"]["name"]))
echo "3";
 {
 echo "4";
    $file_name=$_FILES["uploadedimage"]["name"];
echo "5";
    $temp_name=$_FILES["uploadedimage"]["tmp_name"];
    echo "6";
    $imgtype=$_FILES["uploadedimage"]["type"];
    echo "7";
    $ext= GetImageExtension($imgtype);
    $imagename=date("d-m-Y")."-".time().$ext;
    echo "8";
    $target_path = "image/".$imagename;
    echo "9";
if(move_uploaded_file($temp_name, $target_path)) {
    echo "10";
    $query_upload="INSERT into `images_index` (images_path) VALUES
('".$target_path."')";
    echo "11";
    mysql_query($query_upload) or die("error in $query_upload == ----> ".mysql_error()); 
echo "INSERT into `images_index` (images_path,submission_date) VALUES
('$target_path')";
echo "12";
}
else{
echo "13";
  exit("Error While uploading image on the server");
}
 }


}

Based on this:

echo "1";
       if(empty($imagetype)) return false;

your $imagetype variable is empty. Which comes back to this:
$imgtype=$_FILES["uploadedimage"]["type"];

Check the actual values of those variables.

Thanks Hericles.

You pointed me in right direction. I missed out in my form declaring syntax about "enctype="multipart/form-data" due to which my files were not getting reflected by php code.

Can you send me your email contact at ankit.baphna@gmail.com for future queries. I will be active here as well.

Regards,
Ankit

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.