下面的代码演示了php中如何获取用户上传的文件,并限制文件类型的一般图片文件,最后保存到服务器

HTML源码:

<html>
<head>
     < >Upload Form</ >
</head>
<body>
<form action=\"UploadSingle.php\" method=\"post\" enctype=\"multipart/form-data\">
    Upload a file: <input type=\"file\" name=\"thefile\"><br><br>
    <input type=\"submit\" name=\"Submit\" value=\"Submit\">
</form>
</body>
</html>

 

php上传并判断文件类型源码

<?php
    $aErrors = \"\";
    if ( !empty( $thefile_name ) ) // no file selected
    {
        $$thefile_type=$_FILES[\"file\"][\"type\"];
        if ( ( $thefile_type == \"image/gif\" ) ||
             ( $thefile_type == \"image/pjpeg\" ) ||
             ( $thefile_type == \"image/jpeg\" ) ){
            if ( $thefile_size < ( 1024 * 100 ) ){
                $aCur Path = dirname( $PATH_TRANSLATED );
                $aNewName = $aCur Path . $thefile_name;
                copy( $thefile, $aNewName );
            } else {
                $aErrors .= \"超过最大上传文件限制\";
            }
        } else {
            $aErrors .= \"文件不是图片\";
        }
    } else{
        $aErrors .= \"没有选中任何文件上传\";
    }
?>
收藏 打印