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.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. 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! Bước 1: Bạn sẽ khởi tạo 4 file như trong hình Bạn sẽ khởi tạo 4 file như trong hình
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 Databaseupload_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 ; Bước 2: Tiếp theo bạn sẽ tạo một file upload.php Tiếp theo bạn sẽ tạo một file upload.php <!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html> Bước 3: Tiếp theo sẽ tạo một file là xuly.php Tiếp theo sẽ tạo một file là xuly.php <?php include 'connect.php'; if (isset($_POST['upload'])) { $errors= array(); $file_name = $_FILES['image']['name']; $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; $file_type = $_FILES['image']['type']; $file_parts =explode('.',$_FILES['image']['name']); $file_ext=strtolower(end($file_parts)); $expensions= array("jpeg","jpg","png"); if(in_array($file_ext,$expensions)=== false){ $errors[]="Chỉ hỗ trợ upload file JPEG hoặc PNG."; } if($file_size > 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 '<script language="javascript">alert("Đã upload thành công!");</script>'; }else{ echo '<script language="javascript">alert("Đã upload thất bại!");</script>'; } } $result = mysqli_query($conn, "SELECT * FROM images"); ?> <!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <?php while ($row = mysqli_fetch_array($result)) { echo "<div id='img_div'>"; echo "<img src='photo/".$row['image']."' >"; echo "</div>"; } ?> </div> </body> </html> Bước 4: Tạo file connect.php để kết nối database Tạo file connect.php để kết nối database <?php $host ="localhost"; $uname = "root"; $pwd = ''; $db_name = "upload_image"; $conn = mysqli_connect($host, $uname, $pwd, $db_name); mysqli_set_charset($conn, 'UTF8'); ?> Bước 5: Thêm file style.cssstyle.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.
Đây là chức năng giúp chúng ta có thể gửi file lên server đó là file ảnh hoặc các loại tập tin. Cấu hình: Tìm vào file php.ini bật trạng thái upload file: Tìm vào file php.ini bật trạng thái upload file
FORM HTML UPLOAD FILE
Chú ý::
XỬ LÝ UPLOAD FORM
Hàm hỗ trợ
Bước 1: Tạo đường dẫn chứa file upload: Tạo đường dẫn chứa file upload
Ta có đường dẫn file sau khi upload <!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html>0 Bước 2: Kiểm tra file trước khi upload: Kiểm tra file trước khi upload
<!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html>1
<!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html>2
<!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html>3 Bước 3: Chuyển file từ thư mục tạm lên server: Chuyển file từ thư mục tạm lên server <!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html>4 CODE UPLOAD FILE ĐẦY ĐỦ<!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html>5 GHI NHỚ
Url Linkhttp://hocweb123.com/cach-upload-file-len-server-bang-php.html 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.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.
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! Bước 1: Bạn sẽ khởi tạo 4 file như trong hình Bạn sẽ khởi tạo 4 file như trong hình
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 Databaseupload_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 ; Bước 2: Tiếp theo bạn sẽ tạo một file upload.php Tiếp theo bạn sẽ tạo một file upload.php <!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <form method="POST" action="upload.php" enctype="multipart/form-data"> <input type="hidden" name="size" value="1000000"> <input type="file" name="image"> <button type="submit" name="upload">POST</button> <?php require "xuly.php"?> </form> </div> </body> </html> Bước 3: Tiếp theo sẽ tạo một file là xuly.php Tiếp theo sẽ tạo một file là xuly.php <?php include 'connect.php'; if (isset($_POST['upload'])) { $errors= array(); $file_name = $_FILES['image']['name']; $file_size = $_FILES['image']['size']; $file_tmp = $_FILES['image']['tmp_name']; $file_type = $_FILES['image']['type']; $file_parts =explode('.',$_FILES['image']['name']); $file_ext=strtolower(end($file_parts)); $expensions= array("jpeg","jpg","png"); if(in_array($file_ext,$expensions)=== false){ $errors[]="Chỉ hỗ trợ upload file JPEG hoặc PNG."; } if($file_size > 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 '<script language="javascript">alert("Đã upload thành công!");</script>'; }else{ echo '<script language="javascript">alert("Đã upload thất bại!");</script>'; } } $result = mysqli_query($conn, "SELECT * FROM images"); ?> <!DOCTYPE html> <html> <head> <title>Image Upload</title> <link rel="stylesheet" href="style.css"> </head> <body> <div id="content"> <?php while ($row = mysqli_fetch_array($result)) { echo "<div id='img_div'>"; echo "<img src='photo/".$row['image']."' >"; echo "</div>"; } ?> </div> </body> </html> Bước 4: Tạo file connect.php để kết nối database Tạo file connect.php để kết nối database <?php $host ="localhost"; $uname = "root"; $pwd = ''; $db_name = "upload_image"; $conn = mysqli_connect($host, $uname, $pwd, $db_name); mysqli_set_charset($conn, 'UTF8'); ?> Bước 5: Thêm file style.cssstyle.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.
|