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

烟雨青岚
Release: 2023-04-09 08:34:01
forward
5964 people have browsed it

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
Copy after login

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
Copy after login

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>
Copy after login

&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
ValueDescription
##

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. <div class="code" style="position:relative; padding:0px; margin:0px;"><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><div class="contentsignin">Copy after login</div></div>print_r( $_FILES['file']); // Output the received uploaded image and get the following information <blockquote><p></p></blockquote> <p><img src="https://img.php.cn/upload/article/000/000/053/744e6b0d5394a13101135b3e6038965e-0.jpg" alt="">After the image is successfully uploaded, pass the data table The picture information matches the pictures under </p>upload/image<p> and is displayed in a loop. The effect is as follows.<code>

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!

Related labels:
php
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template