本文实例为大家分享了PHP上传文件功能的具体代码,供大家参考,具体内容如下
html:
PHP:
// 允许上传的图片后缀
$allowedExts = array("gif","jpeg","jpg","png");
$temp = explode(".",$_FILES["file"]["name"]);
echo $_FILES["file"]["size"];
$extension = end($temp); // 获取文件后缀名
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/jpg")
|| ($_FILES["file"]["type"] == "image/pjpeg")
|| ($_FILES["file"]["type"] == "image/x-png")
|| ($_FILES["file"]["type"] == "image/png"))
&& ($_FILES["file"]["size"] < 204800) // 小于 200 kb
&& in_array($extension,$allowedExts))
{
if ($_FILES["file"]["error"] > 0)
{
echo "err";
}
else
{
echo "上传文件名: " . $_FILES["file"]["name"] . "
";
echo "文件类型: " . $_FILES["file"]["type"] . "
";
echo "文件大小: " . ($_FILES["file"]["size"] / 1024) . " kB
";
echo "文件临时存储的位置: " . $_FILES["file"]["tmp_name"] . "
";
// 判断当期目录下的 upload 目录是否存在该<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>
// 如果没有 upload 目录,你需要创建它,upload 目录权限为 777
if (file_exists("../../../Content/UploadFiles/00/00/00/" . $_FILES["file"]["name"]))
{
echo "err";
}
else
{
// 如果 upload 目录不存在该<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a>则将<a href="https://www.f2er.com/tag/wenjian/" target="_blank" class="keywords">文件</a><a href="https://www.f2er.com/tag/shangchuan/" target="_blank" class="keywords">上传</a>到 upload 目录下
move_uploaded_file($_FILES["file"]["tmp_name"],"../../../Content/UploadFiles/00/00/00/" . $_FILES["file"]["name"]);
}
}
}
else
{
echo "err";
}
?>