Home >Backend Development >PHP Tutorial >Teach you step by step how to use php to implement the image upload function

Teach you step by step how to use php to implement the image upload function

烟雨青岚
烟雨青岚forward
2020-07-16 13:39:285989browse

Teach you step by step how to use php to implement the image upload function

In order to test the image upload function and save the image path to the database, we must first create a new test table test_img.

Teach you step by step how to use php to implement the image upload function

CREATE TABLE test_img (
	id int(4) UNSIGNED NOT NULL AUTO_INCREMENT,
	path varchar(100) default NULL,
	upload_time  timestamp default CURRENT_TIMESTAMP,
	PRIMARY KEY(id)
)engine=myisam DEFAULT charset=utf8

sql command: Generate a unique number when inserting into the table. For example, if there is too much test data, the id will continue to increase by itself. If you want to Returning to step 1, you can try the following commands.

alter table test_img auto_increment = 1

Second, create a newimg.html file for selecting uploaded images

<!DOCTYPE html><html lang="utf-8"><head>
    <meta charset="UTF-8">
    <title>图片上传</title></head><body><form action="img.php" method="post" enctype="multipart/form-data">
    选择上传的图片: <input type="file" name="file" accept="image/*">
    <br><br>
    <input type="submit" value="上传"></form>

&lt ;form> The enctype in the tag controls whether to encode and send the form data. The default is application/x-www-form-urlencoded, which means all characters are encoded before sending.

##application/x-www-form-urlencoded Encode all characters before sending (default) multipart/form-dataDo not encode characters. This value must be used when using a form that contains a file upload controltext/plainSpaces are converted to " " plus signs, but special characters are not encoded
Value Description
##

accept in the tag limits the upload format.

三新

img.php Used to accept and process images

$_FILES

Get the image file and add the specific file name to the data table test_img, move_uploaded_file Store the image file in the target folder, iconv perform character encoding to prevent garbled characters after uploading images with Chinese names. <pre class="brush:php;toolbar:false;">&lt;?php header(&quot;Content-Type: text/html;charset=utf-8&quot;); $conn = new mysqli(&amp;#39;localhost&amp;#39;, &amp;#39;root&amp;#39;, &amp;#39;&amp;#39;, &amp;#39;test&amp;#39;); if (!$conn) { die(&quot;Connection failed: &quot; . mysqli_connect_error()); } $destination = &amp;#39;../upload/image/&amp;#39;; $file = $_FILES[&amp;#39;file&amp;#39;]; // 获取上传的图片 $filename = $file[&amp;#39;name&amp;#39;]; $insert = &quot;INSERT INTO test_img (path) VALUES (&amp;#39;$filename&amp;#39;)&quot;; $test = move_uploaded_file($file[&amp;#39;tmp_name&amp;#39;], $destination . iconv(&quot;UTF-8&quot;, &quot;gb2312&quot;, $filename)); if ($insert &amp;&amp; $test) { $conn-&gt;query($insert); } else { echo &amp;#39;上传失败&amp;#39; . &amp;#39;&lt;br&gt;&amp;#39;; } $select = &amp;#39;SELECT path FROM test_img&amp;#39;; $result = $conn-&gt;query($select); while ($row = $result-&gt;fetch_assoc()) { echo &quot;&lt;img src=&quot; . $destination . $row[&amp;#39;path&amp;#39;] . &quot; alt=&quot;Teach you step by step how to use php to implement the image upload function&quot; &gt;&quot;; }</pre>

print_r( $_FILES['file']); // Output the received uploaded image and get the following information

After the image is successfully uploaded, pass the data table The picture information matches the pictures under

upload/image

and is displayed in a loop. The effect is as follows.

Write four to the end

The above is just a rough version of how to upload pictures in PHP. You can try to modify and improve some details yourself. If you want to learn well, you must learn it by yourself. Cloud learning can only scratch the surface. If my sharing can help If you have some inspiration, why not give me a like and encourage me? Of course, you don’t have to give it, I will also drive myself to learn~

Thank you everyone for reading, I hope you can gain something

Recommended tutorial: "

php tutorial

"

The above is the detailed content of Teach you step by step how to use php to implement the image upload function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete