Home  >  Article  >  php教程  >  PHP study notes: File upload (including setting the file upload size limit)

PHP study notes: File upload (including setting the file upload size limit)

WBOY
WBOYOriginal
2016-09-12 17:27:311156browse

Today I am writing about file upload. I forgot the correct rules I learned by myself a few days ago. I used a very stupid way to judge the format and directly entered the code:

<?<span style="color: #000000;">php
</span><span style="color: #008000;">/*</span><span style="color: #008000;">*
 * Created by PhpStorm.
 * User: Administrator
 * Date: 16-9-12
 * Time: 上午10:01
 *
 * 文件上传
 *
 </span><span style="color: #008000;">*/</span>




?>


    <form action="upload.php" method="post" enctype="multipart/form-data"><span style="color: #000000;">
        上传文件:</span><input type="file" name="file" id="file">
        <br/>
        <input type="submit" name="subbtn" value="上传">
    </form>


<?<span style="color: #000000;">php

</span><span style="color: #0000ff;">if</span>(<span style="color: #0000ff;">isset</span>(<span style="color: #800080;">$_POST</span>['subbtn'<span style="color: #000000;">])){

    </span><span style="color: #0000ff;">echo</span> <span style="color: #800080;">$_FILES</span>['file']['type']."<br/>"<span style="color: #000000;">;

    </span><span style="color: #008000;">/*</span><span style="color: #008000;">
     * JPG image/jpeg
     * GIF image/gif
     * PNG image/png
     *
     </span><span style="color: #008000;">*/</span>



    <span style="color: #008000;">//</span><span style="color: #008000;">文件上传处理程序:
    //$_FILES['file']['type']  上传文件的类型
    //$_FILES['file']['size']  上传文件的大小

    //$_FILES['file']['error']  上传错误代码
    //$_FILES['file']['name']   上传文件名

    //$_FILES['file']['tmp_name'] 临时文件名
//$fileName="a.TXT";
//$pos = strrpos($fileName,".");
//$ext = strtolower(substr($fileName,$pos));
//</span>

    <span style="color: #800080;">$file</span> = <span style="color: #800080;">$_FILES</span>['file'<span style="color: #000000;">];
    </span><span style="color: #800080;">$fileName</span>=<span style="color: #800080;">$file</span>['name'<span style="color: #000000;">];
    </span><span style="color: #0000ff;">echo</span> '后缀切割前:'.  <span style="color: #800080;">$file</span>['name']."<br/>"<span style="color: #000000;">;
    </span><span style="color: #008000;">//</span><span style="color: #008000;">确定最后一个.出现的位置</span>
    <span style="color: #800080;">$pos</span> = <span style="color: #008080;">strrpos</span>(<span style="color: #800080;">$file</span>['name'],'.'<span style="color: #000000;">);
    </span><span style="color: #008000;">//</span><span style="color: #008000;">转换文件名为小写</span>
    <span style="color: #800080;">$ext</span> = <span style="color: #008080;">strtolower</span>(" <span style="color: #800080;">$fileName</span>"<span style="color: #000000;">);
    </span><span style="color: #008000;">//</span><span style="color: #008000;">提取文件名后缀</span>
    <span style="color: #800080;">$fileExten</span>=<span style="color: #008080;">substr</span>(<span style="color: #800080;">$ext</span>,<span style="color: #800080;">$pos</span>+2<span style="color: #000000;">);

    </span><span style="color: #008000;">//</span><span style="color: #008000;">判断文件后缀时候符合特定要求,这里设置为:jpg jpeg doc</span>
    <span style="color: #0000ff;">if</span>(<span style="color: #800080;">$fileExten</span>=='jpg'||<span style="color: #800080;">$fileExten</span>=='jpeg'||<span style="color: #800080;">$fileExten</span>=='doc'<span style="color: #000000;"> ){

        </span><span style="color: #0000ff;">echo</span> "文件后缀:". <span style="color: #800080;">$fileExten</span>. "<br/>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> "文件类型:". <span style="color: #800080;">$file</span>['type']."<br/>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> "文件大小:". <span style="color: #800080;">$file</span>['size']."<br/>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> "错误代码:". <span style="color: #800080;">$file</span>['error']."<br/>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> "文件名:". <span style="color: #800080;">$file</span>['name']."<br/>"<span style="color: #000000;">;
        </span><span style="color: #0000ff;">echo</span> "临时文件名:". <span style="color: #800080;">$file</span>['tmp_name']."<br/>"<span style="color: #000000;">;

        </span><span style="color: #008080;">move_uploaded_file</span>(<span style="color: #800080;">$file</span>['tmp_name'], "files/".<span style="color: #800080;">$file</span>['name'<span style="color: #000000;">]);

    }</span><span style="color: #0000ff;">else</span>
        <span style="color: #0000ff;">echo</span> "文件格式不对"<span style="color: #000000;">;
}

</span>?>

If the format is incorrect, it will be intercepted directly.

If you want to modify the uploaded file size limit, you can refer to the following methods:

1. General file upload, unless the file is very small. Like a 5M file, it may take more than a minute to upload.
But in php, the default maximum execution time of the page is 30 seconds. That is to say If it exceeds 30 seconds, the script will stop executing.
This will result in the inability to open the web page. At this time, we can modify max_execution_time
Look in php.ini
max_execution_time
The default is 30 seconds. Change to
max_execution_time = 0
0 Indicates no limit
2. Modify post_max_size to set the maximum size allowed for POST data. This setting also affects file uploads.
php default post_max_size is 2M. If the POST data size is greater than post_max_size $_POST and $_FILES superglobals will be empty.
Look for post_max_size. Change to
post_max_size = 150M
3. Many people will change the second step. But upload the file The maximum size is still 8M.
Why? We also need to change a parameter upload_max_filesize to indicate the maximum size of the uploaded file.
Look for upload_max_filesize, the default is 8M and change it to
upload_max_filesize = 100M
In addition, it should be noted that post_max_size is better than upload_max_filesize.

Statement:
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
Previous article:PHP namespace (namespace)Next article:PHP namespace (namespace)