记录:文件域注意事项

function upload () { if (!isset($_FILES['avatar'])) {

$GLOBALS['message'] = '请提交文件'; // 客户端提交的表单内容中根本没有文件域 return;

}

$avatar = $FILES['avatar'];

// $avatar => array(5) {

// ["name"]=> // string(11) "icon-02.png" // ["type"]=>

// string(9) "image/png"

// ["tmpname"]=>

// string(27) "C:\Windows\Temp\php1138.tmp"

// ["error"]=> // int(0) /

/ ["size"]=> // int(4398)

// }

echo $avatar['error'];

if ($avatar['error'] !== UPLOADERROK) { // 服务端没有接收到上传的文件

$GLOBALS['message'] = '上传失败'; return;

}

// 接收到了文件

// 将文件从临时目录移动到网站范围之内

$source = $avatar['tmpname'];

// 源文件在哪 // => 'C:\Windows\Temp\php1138.tmp' $

target = './uploads/' . $avatar['name'];

// 目标放在哪 // => './uploads/icon-02.png'

// 移动的目标路径中文件夹一定是一个已经存在的目录

$moved = moveuploaded_file($source, $target);

if (!$moved) { $GLOBALS['message'] = '上传失败'; return; }

// 移动成功(上传整个过程OK)

}

if ($SERVER['REQUESTMETHOD'] === 'POST') { // 接收文件 使用一个 叫做 $FILES 超全局成员 // vardump($_FILES); upload(); }

?> 文件上传

上传

转载于:https://my.oschina.net/u/3848851/blog/1831020

你可能感兴趣的:(php)