mysql - Display an image from PHP using header and echo -
for school project have save images in mysql database , show images on website. so, use following code save images:
$link = $link = new pdo('mysql:host=localhost;dbname=mydatabase', 'root', ''); $user = $_post['user']; $image = []; $image = $_files['file']; $image['string_scape'] = $link->quote(file_get_contents($_files['file']['tmp_name'])); $link->query('insert userimage values(null, "'.$image['string_scape'].'", "'.$image['type'].'", '.$image['size'].', '.$user.', now())') or die (print_r($link->errorinfo())); $link = null;
this table made images:
+------------+----------+------+-----+---------+----------------+ | field | type | null | key | default | | +------------+----------+------+-----+---------+----------------+ | id | int(11) | no | pri | null | auto_increment | | file | longblob | no | | null | | | type | text | no | | null | | | size | int(11) | no | | null | | | user | int(11) | no | | null | | | date_added | datetime | no | | null | | +------------+----------+------+-----+---------+----------------+
and show images use following code:
//php file "show-image.php" $link = new pdo('mysql:host=localhost;dbname=distribuidora', 'root', ''); $result = $link->query('select * userimage id = '.$_get['id']) or die (print_r($link->errorinfo())); $image = $result->fetch(pdo::fetch_assoc); header("content-type:".$image["type"]); header("content-length:".$image["size"]); echo $image["file"];
the html:
<img src="show-image.php?id=1" alt="image" />
this how i've done last year , used work fine, since last week can't make image shown. i've tried different headers , different solutions given in other questions in here, still won't work.
i think error in $image = $_files['file'];
u should name file $image = $_files['file']['name'];
, change html <img src="show-image.php?id=1" alt="image" name='name' />
Comments
Post a Comment