php - Can't display image because it contains errors -


i have been trying display image in php last 3 hours. followed few threads on how achieve , pass 2 hours on stackoverflow , other forum try fix problem.

the following code trim down of 1 use. (i have more code handle jpeg, png , different sql tables)

mysql settings

i have table called teams in have these 3 values : id, image , mime

current php code

i have file called show_image.php pass id right image.

if(isset ($_get['id'])) {     $id = $mysqli->real_escape_string($_get['id']);     $query = 'select `image` `teams` `id` = ?';      if ($stmt = $mysqli->prepare($query)) {         $stmt->bind_param('i', $id);         $stmt->execute();         $stmt->store_result();         if ($stmt->num_rows == 1) {             $stmt->bind_result($image);             $stmt->fetch();             header('content-type: image/jpeg');    // can use db query change type needed             echo $image;         }     } } 

and says on internet, access image in html file

<img src="show_image.php?id=1" /> 

results

when call php code (or go directly path /show_image.php?id=1) message says "the image [url] cannot displayed because contains errors". (i did google , duckduckgo error many times.)

the closest came make work when comment line says header('content-type: image/jpeg'); can see binary code (converted in characters) image. although when check properties of page, changed image/jpeg text/html (like expected)

i tried replacing echo $image; by

$img = imagecreatefromjpeg($image); $imagejpeg($img); $imagedestroy($img); 

but not work either.

edit - inserting image in db

if (isset ($_files) && is_array($_files)) {     $filename = $_files['team-logo']['name'][0];     $filesize = $_files['team-logo']['size'][0];     $filetype = $_files['team-logo']['type'][0];      if (substr($filetype,0,5) == "image") {         $imagedata = $mysqli->real_escape_string(file_get_contents($_files['team-logo']['tmp_name'][0]));     } else {         $error_msg .= '<p class="error">you need select , image</p>';     } }  if (empty($error_msg)) {     if ($insert_stmt = $mysqli("insert `teams` (`id`, `image`, `mime`) values ('', ?, ?)")) {         $insert_stmt->bind_param('ss', $imagedata, $filetype);         if (! $insert_stmt->execute()) {             //display error         }         //else image inserted      } } 

does 1 have idea how fix this?

the issue because escaping binary data using real_escape_string , storing via bound parameter.

change insert code following...

$imagedata = file_get_contents($_files['team-logo']['tmp_name'][0]);  // snip  if ($insert_stmt = $mysqli("insert `teams` (`image`, `mime`) values (?, ?)")) {     $insert_stmt->bind_param('ss', $imagedata, $filetype);     if (! $insert_stmt->execute()) {         //display error     }     //else image inserted  } 

to illustrate problem, binary data contained following...

1234'5678 

running through real_escape_string produce this

1234\'5678 

or

1234''5678 

depending on how mysql feels escaping strings @ time.

when bind insert statement, escape characters literally stored, corrupting binary data.

binding parameters safest way inject values queries. not need sanitise value strings safe use within query.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -