php-upload上传文件练习

发布于 2011-04-14 作者 [重庆SEO]

本文来自百度空间博客搬家

2007年开始使用的是 hi.baidu.com/udjy
后来百度空间强制升级为轻博客 hi.baidu.com/imnoco
2015年百度关闭了百度空间

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>upload</title>
</head>

<body>

<form method="post" action="index_.php" enctype="multipart/form-data">
<input type="file" name="file" />
<input type="submit" value="上传" />
</form>
<div>

<?php
$name=$_FILES['file']['name'];
$type=$_FILES['file']['type'];
$size=$_FILES['file']['size'];
$size_kb=ceil($_FILES['file']['size']/1024).'kb';//ceil进一法取整;floor舍去法取整;round四舍五入

$path_tmp=$_FILES['file']['tmp_name'];
$path='upload_file/'.$name;
$path_iconv=iconv('utf-8','gb2312',$path);
$path_copy='upload_file/copy-'.$name;
$path_copy_iconv=iconv('utf-8','gb2312',$path_copy);

$size_exists=filesize($path_iconv);
if($name==''){
 echo '请选择上传文件';
 }
 
else{
 if(!is_dir(upload_file)){
  mkdir('upload_file',0777);
  echo '创建上传目录成功<br>'."\n";
  }
 else{
  echo '上传目录已存在<br>'."\n";
  }
 
 echo "$name<br>\n$type<br>\n$size_kb<br>\n";
// echo $size."<br>\n".$size_exists."<br>\n";
 
 if(file_exists($path_iconv)){
 // echo "文件存在了";
  if($size!=$size_exists){
   move_uploaded_file($path_tmp,$path_copy_iconv);
   echo '文件名被占用,新文件已重命名<a target="_blank" href="'.$path_copy.'">copy-'.$name.'</a>';
   }
  else{
   echo '文件已存在<a target="_blank" href="'.$path.'">'.$path.'</a>';
   }
  }
 else{
  move_uploaded_file($path_tmp,$path_iconv);
  echo '上传文件成功<a target="_blank" href="'.$path.'">'.$name.'</a>';
  }
 }
?>

</div>
</body>
</html>