php上傳功能集后綴名判斷跟隨機(jī)命名
Jun 13, 2016 pm 12:24 PM
php上傳功能集后綴名判斷和隨機(jī)命名
form.php
<span style="color: #0000ff;"><span style="color: #800000;">html</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">head</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">meta </span><span style="color: #ff0000;">http-equiv</span><span style="color: #0000ff;">="content-type"</span><span style="color: #ff0000;"> content</span><span style="color: #0000ff;">="text/html"</span><span style="color: #ff0000;"> charset</span><span style="color: #0000ff;">="utf-8"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">title</span><span style="color: #0000ff;">></span>Upload Image<span style="color: #0000ff;"></span><span style="color: #800000;">title</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">head</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"><span style="color: #800000;">form </span><span style="color: #ff0000;">method</span><span style="color: #0000ff;">="post"</span><span style="color: #ff0000;"> action</span><span style="color: #0000ff;">="upload.php"</span><span style="color: #ff0000;"> enctype</span><span style="color: #0000ff;">="multipart/form-data"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="hidden"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="MAX_FILE_SEZE"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="2000000"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="file"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="view"</span><span style="color: #0000ff;">></span> <span style="color: #0000ff;"><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="upload"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="B1"</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">form</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">body</span><span style="color: #0000ff;">></span><span style="color: #0000ff;"></span><span style="color: #800000;">html</span><span style="color: #0000ff;">></span></span></span></span></span></span></span></span></span></span>
upload.php
<span style="color: #000000;">php</span><span style="color: #0000ff;">include</span>("check.php"); <span style="color: #008000;">//</span><span style="color: #008000;"> 引入自定義函數(shù)文件</span><span style="color: #800080;">$type</span> = <span style="color: #0000ff;">array</span>("jpg", "gif", "bmp", "jpeg", "png"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 判斷上傳文件類型</span><span style="color: #800080;">$fileext</span> = <span style="color: #008080;">strtolower</span>(fileext(<span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]));</span><span style="color: #800080;">$uploadfilename</span> = random(8<span style="color: #000000;">);</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$fileext</span>, <span style="color: #800080;">$type</span><span style="color: #000000;">)){ </span><span style="color: #800080;">$filename</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]); </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'<span style="color: #000000;">])){</span><span style="color: #008000;">//</span><span style="color: #008000;"> echo $_FILES['file']['tmp_name'];</span> <span style="color: #800080;">$flag</span> = <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span style="color: #800080;">$uploadfilename</span>.".".<span style="color: #800080;">$fileext</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$flag</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "上傳成功!"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "Error."<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> "<a href="javascript:history.go(-1)">Back</a>"<span style="color: #000000;">; }}</span>
check.php
<span style="color: #000000;">php</span><span style="color: #008080;">header</span>("Content-type:text/html;charset=utf8"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 獲取文件后綴名函數(shù)</span><span style="color: #0000ff;">function</span> fileext(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">strrchr</span>(<span style="color: #800080;">$filename</span>, "."<span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">substr</span>(<span style="color: #800080;">$sTemp</span>, 1<span style="color: #000000;">);}</span><span style="color: #0000ff;">function</span> fileext2(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$sTemp</span>[<span style="color: #008080;">count</span>(<span style="color: #800080;">$sTemp</span>)-1<span style="color: #000000;">];}</span><span style="color: #008000;">//</span><span style="color: #008000;"> 生成隨機(jī)文件名函數(shù)</span><span style="color: #0000ff;">function</span> random(<span style="color: #800080;">$length</span><span style="color: #000000;">){ </span><span style="color: #800080;">$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz這是一個(gè)隨機(jī)打印輸出字符串的例子"<span style="color: #000000;">; </span><span style="color: #800080;">$captchaResult</span> = "2015"; <span style="color: #008000;">//</span><span style="color: #008000;"> 隨機(jī)數(shù)返回值</span> <span style="color: #800080;">$captchaSentry</span> = ""; <span style="color: #008000;">//</span><span style="color: #008000;"> 隨機(jī)數(shù)中間變量</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$length;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #800080;">$n</span> = <span style="color: #008080;">rand</span>(0, 35); <span style="color: #008000;">#</span><span style="color: #008000;">strlen($captchaSource));</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$n</span> >= 36<span style="color: #000000;">){ </span><span style="color: #800080;">$n</span> = 36 + <span style="color: #008080;">ceil</span>((<span style="color: #800080;">$n</span>-36)/3) * 3<span style="color: #000000;">; </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 3<span style="color: #000000;">); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 1<span style="color: #000000;">); } } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$captchaResult</span><span style="color: #000000;">;}</span>?>
將三個(gè)文件整合成一個(gè):
<span style="color: #000000;">php</span><span style="color: #008000;">//</span><span style="color: #008000;"> 獲取文件后綴名函數(shù)</span><span style="color: #0000ff;">function</span> fileext(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">strrchr</span>(<span style="color: #800080;">$filename</span>, "."<span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #008080;">substr</span>(<span style="color: #800080;">$sTemp</span>, 1<span style="color: #000000;">);}</span><span style="color: #0000ff;">function</span> fileext2(<span style="color: #800080;">$filename</span><span style="color: #000000;">){ </span><span style="color: #800080;">$sTemp</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$filename</span><span style="color: #000000;">); </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$sTemp</span>[<span style="color: #008080;">count</span>(<span style="color: #800080;">$sTemp</span>)-1<span style="color: #000000;">];}</span><span style="color: #008000;">//</span><span style="color: #008000;"> 生成隨機(jī)文件名函數(shù)</span><span style="color: #0000ff;">function</span> random(<span style="color: #800080;">$length</span><span style="color: #000000;">){ </span><span style="color: #800080;">$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz這是一個(gè)隨機(jī)打印輸出字符串的例子"<span style="color: #000000;">; </span><span style="color: #800080;">$captchaResult</span> = "2015"; <span style="color: #008000;">//</span><span style="color: #008000;"> 隨機(jī)數(shù)返回值</span> <span style="color: #800080;">$captchaSentry</span> = ""; <span style="color: #008000;">//</span><span style="color: #008000;"> 隨機(jī)數(shù)中間變量</span> <span style="color: #0000ff;">for</span>(<span style="color: #800080;">$i</span>=0;<span style="color: #800080;">$i</span>$length;<span style="color: #800080;">$i</span>++<span style="color: #000000;">){ </span><span style="color: #800080;">$n</span> = <span style="color: #008080;">rand</span>(0, 35); <span style="color: #008000;">#</span><span style="color: #008000;">strlen($captchaSource));</span> <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$n</span> >= 36<span style="color: #000000;">){ </span><span style="color: #800080;">$n</span> = 36 + <span style="color: #008080;">ceil</span>((<span style="color: #800080;">$n</span>-36)/3) * 3<span style="color: #000000;">; </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 3<span style="color: #000000;">); }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #800080;">$captchaResult</span> .= <span style="color: #008080;">substr</span>(<span style="color: #800080;">$captchaSource</span>, <span style="color: #800080;">$n</span>, 1<span style="color: #000000;">); } } </span><span style="color: #0000ff;">return</span> <span style="color: #800080;">$captchaResult</span><span style="color: #000000;">;}</span><span style="color: #800080;">$type</span> = <span style="color: #0000ff;">array</span>("jpg", "gif", "bmp", "jpeg", "png"<span style="color: #000000;">);</span><span style="color: #008000;">//</span><span style="color: #008000;"> 判斷上傳文件類型</span><span style="color: #800080;">$fileext</span> = <span style="color: #008080;">strtolower</span>(fileext(<span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]));</span><span style="color: #800080;">$uploadfilename</span> = random(8<span style="color: #000000;">);</span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">in_array</span>(<span style="color: #800080;">$fileext</span>, <span style="color: #800080;">$type</span><span style="color: #000000;">)){ </span><span style="color: #800080;">$filename</span> = <span style="color: #008080;">explode</span>(".", <span style="color: #800080;">$_FILES</span>['file']['name'<span style="color: #000000;">]); </span><span style="color: #0000ff;">if</span>(<span style="color: #008080;">is_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'<span style="color: #000000;">])){</span><span style="color: #008000;">//</span><span style="color: #008000;"> echo $_FILES['file']['tmp_name'];</span> <span style="color: #800080;">$flag</span> = <span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span style="color: #800080;">$uploadfilename</span>.".".<span style="color: #800080;">$fileext</span><span style="color: #000000;">); </span><span style="color: #0000ff;">if</span>(<span style="color: #800080;">$flag</span><span style="color: #000000;">){ </span><span style="color: #0000ff;">echo</span> "上傳成功!"<span style="color: #000000;">; }</span><span style="color: #0000ff;">else</span><span style="color: #000000;">{ </span><span style="color: #0000ff;">echo</span> "Error."<span style="color: #000000;">; } </span><span style="color: #0000ff;">echo</span> "<a href="javascript:history.go(-1)">Back</a>"<span style="color: #000000;">; }}</span>?> <meta http-equiv="content-type" content="text/html" charset="utf-8"> <title>Upload Image</title>
?

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

How to convert php blob to file: 1. Create a php sample file; 2. Through "function blobToFile(blob) {return new File([blob], 'screenshot.png', { type: 'image/jpeg' })} ” method can be used to convert Blob to File.

Use Java's File.length() function to get the size of a file. File size is a very common requirement when dealing with file operations. Java provides a very convenient way to get the size of a file, that is, using the length() method of the File class. . This article will introduce how to use this method to get the size of a file and give corresponding code examples. First, we need to create a File object to represent the file we want to get the size of. Here is how to create a File object: Filef

To learn more about open source, please visit: 51CTO Hongmeng Developer Community https://ost.51cto.com Running environment DAYU200:4.0.10.16SDK: 4.0.10.15IDE: 4.0.600 1. To create an application, click File- >newFile->CreateProgect. Select template: [OpenHarmony] EmptyAbility: Fill in the project name, shici, application package name com.nut.shici, and application storage location XXX (no Chinese, special characters, or spaces). CompileSDK10, Model: Stage. Device

Use Java's File.renameTo() function to rename files. In Java programming, we often need to rename files. Java provides the File class to handle file operations, and its renameTo() function can easily rename files. This article will introduce how to use Java's File.renameTo() function to rename files and provide corresponding code examples. The File.renameTo() function is a method of the File class.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Use java's File.getParent() function to get the parent path of a file. In Java programming, we often need to operate files and folders. Sometimes, we need to get the parent path of a file, which is the path of the folder where the file is located. Java's File class provides the getParent() method to obtain the parent path of a file or folder. The File class is Java's abstract representation of files and folders. It provides a series of methods for operating files and folders. Among them, get

Use java's File.getParentFile() function to get the parent directory of a file. In Java programming, we often need to operate files and folders. When we need to get the parent directory of a file, we can use the File.getParentFile() function provided by Java. This article explains how to use this function and provides code examples. File class in Java is the main class used to operate files and folders. It provides many methods to obtain and manipulate file properties
