> 백엔드 개발 > PHP 튜토리얼 > PHP 업로드 기능 세트 접미사 이름 판단 및 임의 이름 지정

PHP 업로드 기능 세트 접미사 이름 판단 및 임의 이름 지정

WBOY
풀어 주다: 2016-07-29 09:15:41
원래의
735명이 탐색했습니다.

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>
로그인 후 복사

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>
로그인 후 복사

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>?>
로그인 후 복사

세 개의 파일을 하나로 통합:

<?<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>
로그인 후 복사

위 내용을 포함하여 PHP 업로드 기능 세트의 접미사 이름 판단 및 임의 이름 지정에 대해 소개합니다. PHP 튜토리얼에 관심이 있는 친구들에게 도움이 되기를 바랍니다.

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿