Hướng dẫn display image php mysql

I have to display images on my php web page from mysql database where i have give the path of my images. but Its only displaying the path but not the image. I used the following Code:


and the output I am getting is :

images/DSC01750.jpg

Some body can you please help me to actually displaying the image but not the path.

Many Thanks

asked Nov 2, 2011 at 15:07

If you wish to display an image in an HTML page, you can construct the image tag like this:


If you are attempting to obscure the location of an image and serve it via the PHP script, your image tag will look something like this:


and you can modify your script to open and output the file:

$image = $row['Pics'];
header("Content-Type: image/jpg");
readfile( $image ); 

answered Nov 2, 2011 at 15:09

George CumminsGeorge Cummins

27.9k8 gold badges67 silver badges90 bronze badges

If $row['Pics'] contains the path to the image, then you'll need to translate that into an image reference to direct the browser to that path. Something like this:


Note that "path" can mean something entirely different here. If it's a server-side fully qualified path, you'll want to translate it into an application-relative path usable by the browser. Something that begins with /usr/web/something/blah/ won't be useful to the browser.

answered Nov 2, 2011 at 15:11

Hướng dẫn display image php mysql

DavidDavid

194k34 gold badges191 silver badges268 bronze badges

You need to wrap it in an img tag.

echo "

rmorerormorero

5964 silver badges10 bronze badges

5

Với mọi trang web đều phải có chức năng upload image lên đưa Database với mục đích thêm hình ảnh vào cơ sở dữ liệu MySQL. Bài viết này sẽ hướng dẫn bạn cách upload ảnh bằng PHP và MySQL với những đoạn code dễ hiểu đọc xong bạn hoàn toàn có thể làm được.

  • Readmore : Code hiển thị nội dung bài viết trong PHP

Với bài tập này bạn sẽ biết cách lưu đường dẫn ảnh vào Database và hiển thị bức ảnh lên trên website. Nào hãy bắt đầu thôi!

Hướng dẫn display image php mysql

Bước 1: Bạn sẽ khởi tạo 4 file như trong hình

Hướng dẫn display image php mysql

  • photo: dùng để lưu hình ảnh trên máy tính
  • connect.php; kết nối tới Database
  • style; thêm CSS để trang trí form
  • upload.php; file upload ảnh
  • xuly.php; xử lý ảnh sau khi upload

Tạo 1 cơ sở dữ liệu Database tên là ‘upload_image‘ => sau đó tạo bảng Table tên là images bằng paste đoạn MySQL vào Database

CREATE TABLE IF NOT EXISTS `images` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`image` varchar(100) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Hướng dẫn display image php mysql

Hướng dẫn display image php mysql

Bước 2: Tiếp theo bạn sẽ tạo một file upload.php

 
 
 
Image Upload 
 
 
 

Bước 3: Tiếp theo sẽ tạo một file là xuly.php

 2097152) {
$errors[]='Kích thước file không được lớn hơn 2MB';
}
$image = $_FILES['image']['name'];
$target = "photo/".basename($image);
$sql = "INSERT INTO images (image) VALUES ('$image')";
mysqli_query($conn, $sql);
if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
echo '';
}else{
echo '';
}
}
$result = mysqli_query($conn, "SELECT * FROM images");
?>



Image Upload



"; echo ""; echo "
"; } ?>

Bước 4: Tạo file connect.php để kết nối database

Bước 5: Thêm file style.css

#content{
margin: 20px auto;
border: 1px solid #cbcbcb;
overflow: auto;
padding: 20px;
}
form{
margin: 20px auto;
}
form div{
margin-top: 5px;
}
#img_div{
padding: 5px;
border: 1px solid #cbcbcb;
float: left
}
#img_div:after{
content: "";
display: block;
clear: both;
}
#img_div img{
float: left;
margin: 5px;
width: 400px;
height: auto;
}

Sau khi tạo xong 4 file: thư mục photo trong máy tính, upload.php, xuly.php và connect.php, style.css bạn thử chạy vào xem kết quả như thế nào.

Kết luận: Trên đây là code upload hình ảnh lên Database bằng PHP và MySQL, với bài hướng dẫn với các bước đơn giản trên hi vọng sẽ giúp bạn học thêm một chút kiến thức về lập trình PHP.

Đọc thêm: Cách lấy dữ liệu trong MySQL