Home > Backend Development > PHP Tutorial > PHP upload function set suffix name judgment and random naming

PHP upload function set suffix name judgment and random naming

WBOY
Release: 2016-07-29 09:15:41
Original
735 people have browsed it

form.php

<span><</span><span>html</span><span>></span><span><</span><span>head</span><span>></span><span><</span><span>meta </span><span>http-equiv</span><span>="content-type"</span><span> content</span><span>="text/html"</span><span> charset</span><span>="utf-8"</span><span>></span><span><</span><span>title</span><span>></span>Upload Image<span></</span><span>title</span><span>></span><span></</span><span>head</span><span>></span><span><</span><span>body</span><span>></span><span><</span><span>form </span><span>method</span><span>="post"</span><span> action</span><span>="upload.php"</span><span> enctype</span><span>="multipart/form-data"</span><span>></span><span><</span><span>input </span><span>type</span><span>="hidden"</span><span> name</span><span>="MAX_FILE_SEZE"</span><span> value</span><span>="2000000"</span><span>></span><span><</span><span>input </span><span>type</span><span>="file"</span><span> name</span><span>="file"</span><span> value</span><span>="view"</span><span>></span><span><</span><span>input </span><span>type</span><span>="submit"</span><span> value</span><span>="upload"</span><span> name</span><span>="B1"</span><span>></span><span></</span><span>form</span><span>></span><span></</span><span>body</span><span>></span><span></</span><span>html</span><span>></span>
Copy after login

upload.php

<?<span>php
</span><span>include</span>("check.php"); <span>//</span><span> 引入自定义函数文件</span><span>$type</span> = <span>array</span>("jpg", "gif", "bmp", "jpeg", "png"<span>);
</span><span>//</span><span> 判断上传文件类型</span><span>$fileext</span> = <span>strtolower</span>(fileext(<span>$_FILES</span>['file']['name'<span>]));
</span><span>$uploadfilename</span> = random(8<span>);
</span><span>if</span>(<span>in_array</span>(<span>$fileext</span>, <span>$type</span><span>)){
    </span><span>$filename</span> = <span>explode</span>(".", <span>$_FILES</span>['file']['name'<span>]);

        </span><span>if</span>(<span>is_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'<span>])){
</span><span>//</span><span>        echo $_FILES['file']['tmp_name'];</span><span>$flag</span> = <span>move_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span>$uploadfilename</span>.".".<span>$fileext</span><span>);
        </span><span>if</span>(<span>$flag</span><span>){
            </span><span>echo</span> "上传成功!"<span>;
        }</span><span>else</span><span>{
            </span><span>echo</span> "Error."<span>;
        }
        </span><span>echo</span> "<a >Back</a>"<span>;
    }
}</span>
Copy after login

check.php

<?<span>php
</span><span>header</span>("Content-type:text/html;charset=utf8"<span>);
</span><span>//</span><span> 获取文件后缀名函数</span><span>function</span> fileext(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>strrchr</span>(<span>$filename</span>, "."<span>);
    </span><span>return</span><span>substr</span>(<span>$sTemp</span>, 1<span>);
}
</span><span>function</span> fileext2(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>explode</span>(".", <span>$filename</span><span>);
    </span><span>return</span><span>$sTemp</span>[<span>count</span>(<span>$sTemp</span>)-1<span>];

}
</span><span>//</span><span> 生成随机文件名函数</span><span>function</span> random(<span>$length</span><span>){
    </span><span>$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"<span>;

    </span><span>$captchaResult</span> = "2015"; <span>//</span><span> 随机数返回值</span><span>$captchaSentry</span> = ""; <span>//</span><span> 随机数中间变量</span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$length</span>;<span>$i</span>++<span>){
        </span><span>$n</span> = <span>rand</span>(0, 35); <span>#</span><span>strlen($captchaSource));</span><span>if</span>(<span>$n</span> >= 36<span>){
            </span><span>$n</span> = 36 + <span>ceil</span>((<span>$n</span>-36)/3) * 3<span>;
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 3<span>);
        }</span><span>else</span><span>{
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 1<span>);
        }
    }
    </span><span>return</span><span>$captchaResult</span><span>;
}
</span>?>
Copy after login

Consolidate three files into one:

<?<span>php
</span><span>//</span><span> 获取文件后缀名函数</span><span>function</span> fileext(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>strrchr</span>(<span>$filename</span>, "."<span>);
    </span><span>return</span><span>substr</span>(<span>$sTemp</span>, 1<span>);
}
</span><span>function</span> fileext2(<span>$filename</span><span>){
    </span><span>$sTemp</span> = <span>explode</span>(".", <span>$filename</span><span>);
    </span><span>return</span><span>$sTemp</span>[<span>count</span>(<span>$sTemp</span>)-1<span>];

}
</span><span>//</span><span> 生成随机文件名函数</span><span>function</span> random(<span>$length</span><span>){
    </span><span>$captchaSource</span> = "0123456789abcdefghijklmnopqrstuvwxyz这是一个随机打印输出字符串的例子"<span>;

    </span><span>$captchaResult</span> = "2015"; <span>//</span><span> 随机数返回值</span><span>$captchaSentry</span> = ""; <span>//</span><span> 随机数中间变量</span><span>for</span>(<span>$i</span>=0;<span>$i</span><<span>$length</span>;<span>$i</span>++<span>){
        </span><span>$n</span> = <span>rand</span>(0, 35); <span>#</span><span>strlen($captchaSource));</span><span>if</span>(<span>$n</span> >= 36<span>){
            </span><span>$n</span> = 36 + <span>ceil</span>((<span>$n</span>-36)/3) * 3<span>;
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 3<span>);
        }</span><span>else</span><span>{
            </span><span>$captchaResult</span> .= <span>substr</span>(<span>$captchaSource</span>, <span>$n</span>, 1<span>);
        }
    }
    </span><span>return</span><span>$captchaResult</span><span>;
}

</span><span>$type</span> = <span>array</span>("jpg", "gif", "bmp", "jpeg", "png"<span>);
</span><span>//</span><span> 判断上传文件类型</span><span>$fileext</span> = <span>strtolower</span>(fileext(<span>$_FILES</span>['file']['name'<span>]));
</span><span>$uploadfilename</span> = random(8<span>);
</span><span>if</span>(<span>in_array</span>(<span>$fileext</span>, <span>$type</span><span>)){
    </span><span>$filename</span> = <span>explode</span>(".", <span>$_FILES</span>['file']['name'<span>]);

    </span><span>if</span>(<span>is_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'<span>])){
</span><span>//</span><span>        echo $_FILES['file']['tmp_name'];</span><span>$flag</span> = <span>move_uploaded_file</span>(<span>$_FILES</span>['file']['tmp_name'], "/Library/WebServer/Documents/test/".<span>$uploadfilename</span>.".".<span>$fileext</span><span>);
        </span><span>if</span>(<span>$flag</span><span>){
            </span><span>echo</span> "上传成功!"<span>;
        }</span><span>else</span><span>{
            </span><span>echo</span> "Error."<span>;
        }
        </span><span>echo</span> "<a >Back</a>"<span>;
    }
}

</span>?>

<html>
<head>
    <meta http-equiv="content-type" c charset="utf-8">
    <title>Upload Image</title>
</head>
<body>
<form method="post" action="" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SEZE" value="2000000">
    <input type="file" name="file" value="view">
    <input type="submit" value="upload" name="B1">
</form>
</body>
</html>
Copy after login

The above introduces the suffix name judgment and random naming of the PHP upload function set, including the content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
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