Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Undefined variable while uploading a file in php

Tags:

html

string

php

i am trying to store 2 images in my database . when i upload 2 images then both are storing successfully but when in try to upload single image then its giving undefined error for un-uploaded image. where i am wrong ?

My code is:

         <label for="certificate">Upload Scaned Document:</label>
         <input type="file" id="uploadImage" name="image" />
         <label for="certificate">Upload Scaned QR Code</label>
         <input type="file" name="QRimage" id="File2" />

And php code is

if((!empty($_FILES["image"])) && ($_FILES['image']['error'] == 0))   {
    $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
    $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
    $imageType = mysql_real_escape_string($_FILES["image"]["type"]);

}
if((!empty($_FILES["QRimage"])) && ($_FILES['QRimage']['error'] == 0)) {
    $QRimageName = mysql_real_escape_string($_FILES["QRimage"]["name"]);
    $QRimageData = mysql_real_escape_string(file_get_contents($_FILES["QRimage"]["tmp_name"])); 
}
like image 974
Nayana Avatar asked Oct 19 '22 17:10

Nayana


2 Answers

Try this

$imageName = "";
$imageData = "";
$QRimageName = "";
$QRimageData = "";
if(!empty($_FILES["image"]["name"])){ 
 $imageName = mysql_real_escape_string($_FILES["image"]["name"]);
 $imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));
 }
 if(!empty($_FILES["QRimage"]["name"])){
 $QRimageName = mysql_real_escape_string($_FILES["QRimage"]["name"]);
  $QRimageData = mysql_real_escape_string(file_get_contents($_FILES["QRimage"]["tmp_name"]));
     }
like image 115
mani Avatar answered Oct 22 '22 16:10

mani


Check with isset() like

if((isset($_FILES["image"]["size"]) && 
    ($_FILES["image"]["size"] > 0))
 {
 }

if((isset($_FILES["QRimage"]["size"]) && 
    ($_FILES["QRimage"]["size"] > 0))
{
   $QRimageName = mysql_real_escape_string($_FILES["QRimage"]["name"]);     
   $QRimageData = mysql_real_escape_string(file_get_contents($_FILES["QRimage"]["tmp_name"]));
}
like image 36
Sadikhasan Avatar answered Oct 22 '22 14:10

Sadikhasan