├── README.md ├── db_conn.php ├── index.php ├── test_db.sql ├── upload.php ├── uploads ├── IMG-5f8954bd209a92.78214246.jpg └── IMG-5f8954caa02539.76436861.jpg └── view.php /README.md: -------------------------------------------------------------------------------- 1 | # Image Upload PHP and MYSQL 2 | 3 | Just a simple Image Upload PHP and MYSQL 4 | version: 1.0.0 5 | 6 | ## Full Tutorial 7 | 8 | [On YouTube](https://youtu.be/onu3w8kqASU) 9 | 10 | ## Authors 11 | 12 | [Elias Abdurrahman](https://github.com/codingWithElias) 13 | -------------------------------------------------------------------------------- /db_conn.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Image Upload Using PHP 5 | 14 | 15 | 16 | 17 |

18 | 19 |
22 | 23 | 25 | 26 | 29 | 30 |
31 | 32 | -------------------------------------------------------------------------------- /test_db.sql: -------------------------------------------------------------------------------- 1 | -- phpMyAdmin SQL Dump 2 | -- version 4.9.0.1 3 | -- https://www.phpmyadmin.net/ 4 | -- 5 | -- Host: 127.0.0.1 6 | -- Generation Time: Oct 16, 2020 at 10:22 AM 7 | -- Server version: 10.4.6-MariaDB 8 | -- PHP Version: 7.3.9 9 | 10 | SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; 11 | SET AUTOCOMMIT = 0; 12 | START TRANSACTION; 13 | SET time_zone = "+00:00"; 14 | 15 | 16 | /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; 17 | /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; 18 | /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; 19 | /*!40101 SET NAMES utf8mb4 */; 20 | 21 | -- 22 | -- Database: `test_db` 23 | -- 24 | 25 | -- -------------------------------------------------------- 26 | 27 | -- 28 | -- Table structure for table `images` 29 | -- 30 | 31 | CREATE TABLE `images` ( 32 | `id` int(11) NOT NULL, 33 | `image_url` text NOT NULL 34 | ) ENGINE=InnoDB DEFAULT CHARSET=latin1; 35 | 36 | -- 37 | -- Dumping data for table `images` 38 | -- 39 | 40 | INSERT INTO `images` (`id`, `image_url`) VALUES 41 | (8, 'IMG-5f8954bd209a92.78214246.jpg'), 42 | (9, 'IMG-5f8954caa02539.76436861.jpg'); 43 | 44 | -- 45 | -- Indexes for dumped tables 46 | -- 47 | 48 | -- 49 | -- Indexes for table `images` 50 | -- 51 | ALTER TABLE `images` 52 | ADD PRIMARY KEY (`id`); 53 | 54 | -- 55 | -- AUTO_INCREMENT for dumped tables 56 | -- 57 | 58 | -- 59 | -- AUTO_INCREMENT for table `images` 60 | -- 61 | ALTER TABLE `images` 62 | MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=10; 63 | COMMIT; 64 | 65 | /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; 66 | /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; 67 | /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */; 68 | -------------------------------------------------------------------------------- /upload.php: -------------------------------------------------------------------------------- 1 | "; 7 | print_r($_FILES['my_image']); 8 | echo ""; 9 | 10 | $img_name = $_FILES['my_image']['name']; 11 | $img_size = $_FILES['my_image']['size']; 12 | $tmp_name = $_FILES['my_image']['tmp_name']; 13 | $error = $_FILES['my_image']['error']; 14 | 15 | if ($error === 0) { 16 | if ($img_size > 125000) { 17 | $em = "Sorry, your file is too large."; 18 | header("Location: index.php?error=$em"); 19 | }else { 20 | $img_ex = pathinfo($img_name, PATHINFO_EXTENSION); 21 | $img_ex_lc = strtolower($img_ex); 22 | 23 | $allowed_exs = array("jpg", "jpeg", "png"); 24 | 25 | if (in_array($img_ex_lc, $allowed_exs)) { 26 | $new_img_name = uniqid("IMG-", true).'.'.$img_ex_lc; 27 | $img_upload_path = 'uploads/'.$new_img_name; 28 | move_uploaded_file($tmp_name, $img_upload_path); 29 | 30 | // Insert into Database 31 | $sql = "INSERT INTO images(image_url) 32 | VALUES('$new_img_name')"; 33 | mysqli_query($conn, $sql); 34 | header("Location: view.php"); 35 | }else { 36 | $em = "You can't upload files of this type"; 37 | header("Location: index.php?error=$em"); 38 | } 39 | } 40 | }else { 41 | $em = "unknown error occurred!"; 42 | header("Location: index.php?error=$em"); 43 | } 44 | 45 | }else { 46 | header("Location: index.php"); 47 | } -------------------------------------------------------------------------------- /uploads/IMG-5f8954bd209a92.78214246.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codingWithElias/image-upload-php-and-mysql/6b747ae413c096eea0e26ce59ab2441a1f827078/uploads/IMG-5f8954bd209a92.78214246.jpg -------------------------------------------------------------------------------- /uploads/IMG-5f8954caa02539.76436861.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/codingWithElias/image-upload-php-and-mysql/6b747ae413c096eea0e26ce59ab2441a1f827078/uploads/IMG-5f8954caa02539.76436861.jpg -------------------------------------------------------------------------------- /view.php: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | View 6 | 28 | 29 | 30 | 31 | 0) { 36 | while ($images = mysqli_fetch_assoc($res)) { ?> 37 | 38 |
39 | 40 |
41 | 42 | 43 | 44 | --------------------------------------------------------------------------------