原生PHP文件上传及修改编辑上传

新增:

<form method="post" action="news_add.php" enctype="multipart/form-data">
	新闻标题: <input name="title" type="text" />
	新闻图片: <input name="pic" type="file" /> 
	新闻内容: <textarea name="content" cols="60" rows="9"></textarea>
	<input name="ok" type="submit"  id="ok" value="提 交" class="input">
</form>
<?php
if($_POST){									//判断是否获取数据,避免打开该页面即自动添加
  $mysql_server_name='localhost'; 			//服务器
  $mysql_username='username'; 				//用户名
  $mysql_password='password'; 				//数据库密码
  $mysql_database='dbname'; 				//数据库名
  //连接数据库并查询数据表
  $conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; 		//连接数据库
  mysql_query("set names 'utf8'"); 			//数据库输出编码.
  mysql_select_db($mysql_database); 		//打开数据库
  @$title=$_POST['title'];
  @$content=$_POST['content'];
  $dat=date('Y-m-d H:i:s');
  @$file=$_FILES['pic'];
	if($file['name'] <> ""){				//如果未上传图片请先上传
	  if($file["size"] < 307200){			//上传文件需小于300KB
	    if($file['error']>0){				//上传错误数大于0,即有错误
	      echo "Error:".$file['error']."<br/>";
	    }else{								//上传成功
	      //获取扩展名,end()获取数组最后一个值,strtolower()将字符串转换成小写
	      $kz=strtolower(end(explode('.',$file['name'])));
	      $sui=mt_rand(1000,9999);			//随机获取4位数
	      $filename=date('YmdHis').$sui.'.'.$kz;//文件重命名
	      $pic="upload/news/".$filename;	//指定上传路径,注意:后台文件上传时必须在后台目录下,否则可能会报文件目录不存在的错误,移动不成功时可改成绝对路径尝试。
	      move_uploaded_file($file['tmp_name'],$pic);//将保存在服务器中的临时上传文件转移到指定位置
	    }
	  $str="insert into new(title,pic,content,dat) values('$title','$pic','$content','$dat')";
	  }
	}else{
	   echo "<script>alert('请先上传图片!');window.location.href=history.go(-1);</script>";
	}
  if(@mysql_query($str)){
    echo "<script>alert('添加成功!');window.location.href='news.php';</script>";
  }else{
    echo "<script>alert('上传图片过大!');window.location.href= history.go(-1);</script>";
  }
  mysql_close($conn);
}
?>

修改:

<?php
$mysql_server_name='localhost'; 			//服务器
$mysql_username='username'; 				//用户名
$mysql_password='password'; 				//数据库密码
$mysql_database='dbname'; 					//数据库名
//连接数据库并查询数据表
$conn=mysql_connect($mysql_server_name,$mysql_username,$mysql_password) or die("error connecting") ; 		//连接数据库
mysql_query("set names 'utf8'"); 			//数据库输出编码.
mysql_select_db($mysql_database); 			//打开数据库
if(@$_GET['Id']){//确定是否获取到需要编辑的id
	$sql ="select * from nqlnew where Id=".$_GET['Id']; //SQL语句
	$result = mysql_query($sql,$conn); 		//查询
	//输出需修改的内容
	while($row = mysql_fetch_array($result)){
?>
<form method="post" action="news_modi.php" enctype="multipart/form-data">
新闻标题:
<input name="title" type="text" value="<?php echo $row['title']; ?>" />
新闻图片:
  <input name="pic" type="file"  /> 
  <input name="id" type="hidden" value="<?php echo $row['Id']; ?>"  />
  <input name="pic1" type="hidden" value="<?php echo $row['pic']; ?>"  />
新闻内容: 
<textarea name="content"><?php echo $row['content']; ?></textarea>
<input type="submit" value="提 交" class="input">
</form>
<?php
	}
}
if($_POST){									//修改编辑内容
	$id=$_POST['id'];
	@$title=$_POST['title'];
	@$content=$_POST['content'];
	$dat=date('Y-m-d H:i:s');
	@$file=$_FILES['pic'];
	if($file['name'] <> ""){				//判断是否上传图片
		if($file["size"] < 307200){			//上传文件需小于300KB
			if($file['error']>0){			//上传错误数大于0,即有错误
			  echo "Error:".$file['error']."<br/>";
			}else{							//上传成功
			$tu=$_POST['pic1'];
			  unlink($tu);
			  //获取扩展名,end()获取数组最后一个值,strtolower()将字符串转换成小写
			  $kz=strtolower(end(explode('.',$file['name'])));
			  $sui=mt_rand(1000,9999);		//随机获取4位数
			  $filename=date('YmdHis').$sui.'.'.$kz;//文件重命名
			  $pic="upload/news/".$filename;//指定上传路径
			  move_uploaded_file($file['tmp_name'],$pic);//将保存在服务器中的临时上传文件转移到指定位置
			}
			$str="update nqlnew set title='$title',pic='$pic',content='$content',dat='$dat' where Id=".$id;
		}
	}else{
	  echo "<script>alert('请先上传图片!');window.location.href=history.go(-1);</script>";
	}
	if(@mysql_query($str)){
	  echo "<script>alert('修改成功!');window.location.href='news.php';</script>";
	}else{
	  echo "<script>alert('上传图片过大!');window.location.href=history.go(-1);</script>";
	}
}
mysql_close($conn);
?>

猜你喜欢

转载自blog.csdn.net/qq_38882327/article/details/88988654