【小白笔记】PHP学习之路 (26) --文件上传与下载、配置

先补充一下几个函数:

ceil()    向上取最小的整数。

floor()    向下取最大整数。

round()    四舍五入取整。

parse_url()    解析URL,返回关联数组(host、port、path、query等)。如果某部分不存在,则不创建对应键值对。

is_uploaded_file()   判断是否为上传的文件。防止对其他文件进行处理。

move_uploaded_file()    将上传的文件移动到指定位置

mt_rand()    产生指定范围的随机数。


要进行文件上传首先要配置php.ini:

file_uploads = On   默认是打开的

upload_tmp_dir = "目录"

up_max_filesize = "大小"

max_execution_time = 时间(秒 )    set_time_limit()从该语句开始计时,只对所在脚本有效

memory_limit = xxxM    默认是128M,单个脚本最大运行内存

表单可以这样设置:

enctype=multipart/form-data

获取上传文件:

$_FILES   获取通过post方式上传文件的集合

    参数errors:0 没有任何错误   1 文件大小超过了PHP.INI的配置  2 超过了前台表单指定的文件大小 3 只上传了文件的一部分 4 没有上传任何文件

多个文件上传处理:

过滤掉空数据:

      使用array_filter()函数过滤数组:将数组每个元素值传递到回调函数中,如果回调函数返回真则将其包含。

配置上传目录:

      is_dir()、file_exists()判断目录,mkdir()创建目录

多文件上传处理:

      is_uploaded_file()

      move_uploaded_file()

      mt_rand()

     date()

     filectime()

     file_put_contents()


下载文件:

      1.通过超链接下载

      2.通过发送header信息实现下载。

header("Content-type:image/png");   //如果不知道类型,弹出窗口将会默认以浏览器打开
header("Content-Disposition:attachment;filename=a.png");


在header中也可以指定一些其他信息:

header("Accept-ranges:bytes");
header("Accept-length:".filesize('abc.doc'));

也可以指定为任意类型的二进制文件:

header("Content-type:application/ocet-stream");


这时浏览器将会自动识别后缀名来决定打开方式。

简单练习:

	$file = $_GET['file'];
	if(file_exists($file)){
		header("Contnet-type:application/ocet-stream");
		$fileName = basename($file);
		header("Content-Disposition:attachment;filename={$fileName}");
		header("Accept-ranges:bytes");
		header("Accept-length:".filesize($file));
		readfile($file);
	}else{
		echo "文件错误";
	}


如果没有指定header信息,则浏览器默认作为超文本文件处理。

 


 

如果表单指定了上传文件名,则每个文件及其相关信息会成为$_FILES数组的一项:

	<form action="82.php" method="post" enctype="multipart/form-data">
		文件1:<input type="file" name="file1"/><br/>
		文件2:<input type="file" name="file2"/><br/>
		文件3:<input type="file" name="file3"/><br/>
		文件4:<input type="file" name="file4"/><br/>
		<input type="submit" value="上传文件"/><br/>
	</form>

<?php
	echo "<pre>";
	print_r($_FILES);
?>

结果:

Array
(
    [file1] => Array
        (
            [name] => a.txt
            [type] => text/plain
            [tmp_name] => D:\wamp\tmp\phpA4C7.tmp
            [error] => 0
            [size] => 1265
        )

    [file2] => Array
        (
            [name] => b.txt
            [type] => text/plain
            [tmp_name] => D:\wamp\tmp\phpA4C8.tmp
            [error] => 0
            [size] => 140
        )

    [file3] => Array
        (
            [name] => c.txt
            [type] => text/plain
            [tmp_name] => D:\wamp\tmp\phpA4D8.tmp
            [error] => 0
            [size] => 5108
        )

    [file4] => Array
        (            
            [name] => d.txt
            [type] => text/html
            [tmp_name] => D:\wamp\tmp\phpA4E9.tmp
            [error] => 0
            [size] => 1689
        )

)

如果这样指定:

	<form action="82.php" method="post" enctype="multipart/form-data">
		文件1:<input type="file" name="file[]"/><br/>
		文件2:<input type="file" name="file[]"/><br/>
		文件3:<input type="file" name="file[]"/><br/>
		文件4:<input type="file" name="file[]"/><br/>
		<input type="submit" value="上传文件"/><br/>
	</form>

那么结果就会分类显示:

Array
(
    [file] => Array
        (
            [name] => Array
                (
                    [0] => a.txt
                    [1] => b.txt
                    [2] => c.txt
                    [3] => d.txt
                )

            [type] => Array
                (
                    [0] => text/plain
                    [1] => text/plain
                    [2] => text/plain
                    [3] => text/plain
                )

            [tmp_name] => Array
                (
                    [0] => D:\wamp\tmp\php6791.tmp
                    [1] => D:\wamp\tmp\php6792.tmp
                    [2] => D:\wamp\tmp\php6793.tmp
                    [3] => D:\wamp\tmp\php6794.tmp
                )

            [error] => Array
                (
                    [0] => 0
                    [1] => 0
                    [2] => 0
                    [3] => 0
                )

            [size] => Array
                (
                    [0] => 1265
                    [1] => 140
                    [2] => 5108
                    [3] => 1689
                )

        )

)



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值