Nayana Nayana - 10 days ago 5
PHP Question

Filename cannot be empty error while uploading image

i am storing 2 images in to my database . if i uploaded 2 images at a time then its storing successfully . but if try to upload only image1 or if i try to upload image2 or if i not uploaded any image then i am getting this Warning


Warning: file_get_contents() [function.file-get-contents]: Filename cannot be empty in C:\wamp\www\print\main.php on line 24


My code is :

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


php code:

$imageName = mysql_real_escape_string($_FILES["image"]["name"]);
$imageData = mysql_real_escape_string(file_get_contents($_FILES["image"]["tmp_name"]));

$QRimageName = mysql_real_escape_string($_FILES["QRimage"]["name"]);
$QRimageData = mysql_real_escape_string(file_get_contents($_FILES["QRimage"]["tmp_name"]));


even i tried with this all so:

if(isset($_FILES["image"]) && (!empty($_FILES["image"])) && ($_FILES['image']['error'] == 0))


And

if((isset($_FILES["QRimage"]["size"])) && ($_FILES["QRimage"]["size"] > 0))


then i can overcome that warning .

If i tried to upload Image1 then.

Notice: Undefined variable: QRimageName in C:\wamp\www\print\main.php on line 59
Notice: Undefined variable: QRimageData in C:\wamp\www\print\main.php on line 59


If i tried to upload Image2 then .

Notice: Undefined variable: imageName in C:\wamp\www\print\main.php on line 59
Notice: Undefined variable: imageData in C:\wamp\www\print\main.php on line 59


I am feed up with this error please help me how to solve this one

Answer

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"]));
}
Comments