· 6 years ago · Oct 09, 2019, 10:02 AM
1<?php
2// Check if a file has been uploaded
3if(isset($_FILES['uploaded_file'])) {
4 // Make sure the file was sent without errors
5 if($_FILES['uploaded_file']['error'] == 0) {
6 // Connect to the database
7 $dbLink = new mysqli('127.0.0.1', 'root', '', 'pt');
8 if(mysqli_connect_errno()) {
9 die("MySQL connection failed: ". mysqli_connect_error());
10 }
11
12 // Gather all required data
13 $name = $dbLink->real_escape_string($_FILES['uploaded_file']['name']);
14 $mime = $dbLink->real_escape_string($_FILES['uploaded_file']['type']);
15 $data = $dbLink->real_escape_string(file_get_contents($_FILES ['uploaded_file']['tmp_name']));
16 $size = intval($_FILES['uploaded_file']['size']);
17
18 // Create the SQL query
19 $query = "
20 INSERT INTO `file` (
21 `name`, `mime`, `size`, `data`, `created`
22 )
23 VALUES (
24 '{$name}', '{$mime}', {$size}, '{$data}', NOW()
25 )";
26
27 // Execute the query
28 $result = $dbLink->query($query);
29
30 // Check if it was successfull
31 if($result) {
32 echo 'Success! Your file was successfully added!';
33 }
34 else {
35 echo 'Error! Failed to insert the file'
36 . "<pre>{$dbLink->error}</pre>";
37 }
38 }
39 else {
40 echo 'An error accured while the file was being uploaded. '
41 . 'Error code: '. intval($_FILES['uploaded_file']['error']);
42 }
43
44 // Close the mysql connection
45 $dbLink->close();
46}
47else {
48 echo 'Error! A file was not sent!';
49}
50
51// Echo a link back to the main page
52echo '<p>Click <a href="index.html">here</a> to go back</p>';
53?>
54 __________________________________________________________________________________
55 //interested image
56
57<!DOCTYPE html>
58<head>
59 <title>MySQL file upload example</title>
60 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
61</head>
62<body>
63 <form action="" method="post" enctype="multipart/form-data">
64 <input type="file" name="uploaded_file"><br>
65 <input type="submit" value="Upload file">
66 </form>
67 <p>
68 <a href="list_files.php">See all files</a>
69 </p>
70</body>
71</html>
72___________________________________________________________________________________________
73<?php
74// Connect to the database
75$dbLink = new mysqli('127.0.0.1', 'root', '', 'pt');
76if(mysqli_connect_errno()) {
77 die("MySQL connection failed: ". mysqli_connect_error());
78}
79
80// Query for a list of all existing files
81$sql = 'SELECT `id`, `name`, `mime`, `size`, `created` FROM `file`';
82$result = $dbLink->query($sql);
83
84// Check if it was successfull
85if($result) {
86 // Make sure there are some files in there
87 if($result->num_rows == 0) {
88 echo '<p>There are no files in the database</p>';
89 }
90 else {
91 // Print the top of a table
92 echo '<table width="100%">
93 <tr>
94 <td><b>Name</b></td>
95 <td><b>Mime</b></td>
96 <td><b>Size (bytes)</b></td>
97 <td><b>Created</b></td>
98 <td><b> </b></td>
99 </tr>';
100
101 // Print each file
102 while($row = $result->fetch_assoc()) {
103 echo "
104 <tr>
105 <td>{$row['name']}</td>
106 <td>{$row['mime']}</td>
107 <td>{$row['size']}</td>
108 <td>{$row['created']}</td>
109 <td><img src='{$row['name']}'/></td>
110 <td><a href='s.php?id={$row['id']}'>Download</a></td>
111 </tr>";
112 }
113
114 // Close table
115 echo '</table>';
116 }
117
118 // Free the result
119 $result->free();
120}
121else
122{
123 echo 'Error! SQL query failed:';
124 echo "<pre>{$dbLink->error}</pre>";
125}
126
127// Close the mysql connection
128$dbLink->close();
129?>
130__________________________________________________________
131// show
132
133<?php
134// Make sure an ID was passed
135if(isset($_GET['id'])) {
136// Get the ID
137 $id = intval($_GET['id']);
138
139 // Make sure the ID is in fact a valid ID
140 if($id <= 0) {
141 die('The ID is invalid!');
142 }
143 else {
144 // Connect to the database
145 $dbLink = new mysqli('127.0.0.1', 'root', '', 'pt');
146 if(mysqli_connect_errno()) {
147 die("MySQL connection failed: ". mysqli_connect_error());
148 }
149
150 // Fetch the file information
151 $query = "
152 SELECT `mime`, `name`, `size`, `data`
153 FROM `file`
154 WHERE `id` = {$id}";
155 $result = $dbLink->query($query);
156
157 if($result) {
158 // Make sure the result is valid
159 if($result->num_rows == 1) {
160 // Get the row
161 $row = mysqli_fetch_assoc($result);
162
163 // Print headers
164 header("Content-Type: ". $row['mime']);
165 header("Content-Length: ". $row['size']);
166 header("Content-Disposition: attachment; filename=". $row['name']);
167
168 // Print data
169 echo $row['data'];
170 }
171 else {
172 echo 'Error! No image exists with that ID.';
173 }
174
175 // Free the mysqli resources
176 @mysqli_free_result($result);
177 }
178 else {
179 echo "Error! Query failed: <pre>{$dbLink->error}</pre>";
180 }
181 @mysqli_close($dbLink);
182 }
183}
184else {
185 echo 'Error! No ID was passed.';
186}
187?>
188
189///dbd
190
191
192CREATE TABLE `file` (
193 `id` Int Unsigned Not Null Auto_Increment,
194 `name` VarChar(255) Not Null Default 'Untitled.txt',
195 `mime` VarChar(50) Not Null Default 'text/plain',
196 `size` BigInt Unsigned Not Null Default 0,
197 `data` MediumBlob Not Null,
198 `created` DateTime Not Null,
199 PRIMARY KEY (`id`)
200)