上传文件到upload/下且修改文件名

  • A+
所属分类:PHP技术

<?php
//获取'http://www.hao.com/doc.html?id=1'url地址中的文件后缀名的函数,结果:html
function getExt($url)
{
$path=parse_url($url);
$str=explode('.',$path['path']);
return end($str);//获取该数组最后一个元素值即html
}

header('Content-Type:text/html;charset=utf8');
if ($_FILES["file"]["error"] > 0){
echo "错误: " . $_FILES["file"]["error"] . "<br />";
}else{
echo "文件名: " . $_FILES["file"]["name"] . "<br />";
echo "类型: " . $_FILES["file"]["type"] . "<br />";
echo "大小: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
}
if (file_exists("upload/" . $_FILES["file"]["name"])){
echo $_FILES["file"]["name"] . " 文件已经存在. ";
}else{
$_FILES["file"]["name"]=time().'.'.getExt($_FILES["file"]["name"]);
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
echo "文件已经被存储到: " . "upload/" . $_FILES["file"]["name"];
}
?>
<!doctype html>
<html>
<head>
<meta charset="UTF-8" />
</head>
<body>
<form action="upload-file.php" method="post"
enctype="multipart/form-data">
<label for="file">文件名:</label>
<input type="file" name="file" id="file" />
<br />
<input type="submit" name="submit" value="提交" />
</form>
</body>
</html>

发表评论

您必须登录才能发表评论!