PHP 文件操作2 上传下载

1 PHP中上传可以使用全局变量: $_FILES

2 将上传的文件转移到指定的文件夹中:move_uploaded_file

3 多文件上传,需要在html配置input时将名字后面加[],参考下面实例

4 过滤:大小,类型

5 下载:通过html格式的超级链接标签<a>来实现文件的下载,但这不是自动下载,而是调用浏览器的下载工具。

 

几个attention:

1 上传给出的文件夹必须存在,刚才做实验时,还需chmod o+w 新建的文件夹 ,否则就会failed.

2 $_FILES['upload']['name']等中间的参数名词必须写准确

 

 

自己实验所用例子:

*************************

<html>

<head>

    <title>file upload</title>

    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

 

<body>

    <form enctype="multipart/form-data" action="upload.php" method="POST">

                                                     <!--upload table

    <input name="upload[]" type="file"><br>

    <input name="upload[]" type="file"><br>

    <input name="upload[]" type="file"><br>

    <input type="submit" value="Upload"> -->

 

<!-- for the filter , here shut the multi-files uploading, add single file uploading-->

    <input name="upload" type="file"><Br>

    <input type="submit" value="upload">

 

    </form>

 

<?php

/*

$fname=$_FILES['upload']['name'];

echo "the upload filename is:".$fname."<Br>";

 

$ftype=$_FILES['upload']['type'];

echo "the upload filetype is".$ftype."<Br>";

 

$fsize=$_FILES['upload']['size'];

echo "the upload filesize is".$fsize."<br>";

 

$ferror=$_FILES['upload']['error'];

echo "the upload fileerror is".$ferror."<Br>";

 

if(move_uploaded_file($_FILES['upload']['tmp_name'],"/var/www/".$fname))

{

    echo "<br>upload succes! <br>";

}

else

{

    echo "<br>failed!<br>";

}

 

//multi-files uploading

foreach ($_FILES['upload']['name'] as $index=>$name)

{

    if(move_uploaded_file($_FILES['upload']['tmp_name'][$index],"/var/www/myupload/".$name))

    {

        echo "file".$name."uploading success<Br>";

    }

    else

    {

        echo "file".$name."uploading failed<br>";

    }

}

 

// filter pictures:

$uploadfile="/var/www/myupload/img/".$_FILES['upload']['name'];

$message='';

if($_FILES['upload']['type']!='image/bmp')

{

    echo '<p> the uploapfile type can only be : bmp</p>';

    exit;

}

 

 

if($_FILES['upload']['size']>80480)

{

    echo '<p>the file size canot beyongd 80kb </p>';

    exit;

}

 

if(move_uploaded_file($_FILES['upload']['tmp_name'],$uploadfile))

{

    echo "upload success!";

    exit;

}

 

echo "failed";

 

 */

 

 

//downloading:

$path="/var/www/myupload/";

if(is_dir($path))

{

    $openHandle=opendir($path);

    while(false !== ($file = readdir($openHandle)))

    {

        echo "<a href='$file'>".$file."</a>";

        echo "<br>";

    }

 

    closedir($openHandle);

}

else

{

    echo " the folder isnot exist!";

}

 

?>

 

</body>

</html>

*************************

你可能感兴趣的:(PHP,upload,File,filter,input,下载工具)