一步一步教你用PHP+MySql搭建网站 No.5 图片上传、故事删除

上篇文章中讲到,story.php中的表单提交之后的页面是story_submit.php,我们就看一下story_submit.php是如何完成文章的发表的

老样子,先上代码:

<?php
	# add / modify story record
	include_once('include_fns.php');

	$handle = db_connect();

	$headline = $_REQUEST['headline'];
	$page = $_REQUEST['page'];
	$time = time();

	if ((isset($_FILES['html']['name']) && 
		(dirname($_FILES['html']['type']) == 'text') &&
		is_uploaded_file($_FILES['html']['tmp_name']) )) {
		// if user upload some files, then set the content of the files as the story_text
		$story_text = file_get_contents($_FILES['html']['tmp_name']);
	}else{
		$story_text = $_REQUEST['story_text'];
	}

	$story_text = addslashes($story_text);

	if (isset($_REQUEST['story']) && $_REQUEST['story']!='') {
		# it's an update
		$story = $_REQUEST['story'];

		$query = "update stories 
				  set headline = '$headline',
				  	  story_text = '$story_text',
				  	  page = '$page',
				  	  modified = $time
				  where id = $story";
	}else{
		// it's a new story
		$query = "insert into stories
				  (headline,story_text,page,writer,created,modified)
				  values
				  ('$headline','$story_text','$page','".$_SESSION['auth_user']."',
				  	$time,$time)";
	}

	$result = mysql_query($query);

	if (!$result) {
		# code...
		echo "There was a database error when executing <pre>$query</pre>";
		echo mysql_error();
		exit; 
	}

	if ((isset($_FILES['picture']['name']) && 
		is_uploaded_file($_FILES['picture']['tmp_name']))) {
		# there is uploaded picture
		if (!isset($_REQUEST['story']) || $_REQUEST['story']=='') {
			$story = mysql_insert_id($handle);
			// mysql_insert_id  return the auto generated id used in the last query
		}
		$type = basename($_FILES['picture']['type']);

		switch ($type) {
			case 'jpeg':
			case 'pjpeg':
			case 'png':
			case 'jpg':
				$filename = "images/$story.jpg";
				move_uploaded_file($_FILES['picture']['tmp_name'], '../'.$filename);
				$query = "update stories 
						  set picture = '$filename'
						  where id = $story";
				$result = mysql_query($query);
				break;
			
			default:
				echo 'Invalid picture format:'.$_FILES['picture']['type'];
				break;
		}
	}else{
		// there is no image file to upload or didn't get the file's info
		echo 'Possible file upload attack:';
		echo "filename '".$_FILES['picture']['tmp_name']."'.";
	}
	
	header('Location: '.$_REQUEST['destination']);
?>

我们还是先从整体捋一遍代码:


第7,8行

这两个变量都是从上一个页面story.php提交表单中获取的参数


第9行

time函数返回的是时间戳


11-18行

这部分代码返回的是上传的html文件的内容


第20行

这里用到了php中发送text内容到数据库的一个函数:addslashes,作用是在一些特定的符号前面加上/ 符号,特定的符号有', '' , nul, \等,

例如:


然后我在搜索这个函数是,发现了另外的方法mysql_escape_string,



22-39行

根据传入的参数中有没有story来判断是更新还是新添加的story,这里之前我们也有提到了。


50-75行

是标准的php上传文件的步骤,可以试着记一下

注意第54行,是得到自增序列的下一个字段

最后第82行

我们上一篇blog里面有提到过,在form提交了两个hidden的参数,其中一个是destination,其实就是writer.php页面了。


好了,基本上这个页面没有什么特别难的地方。


我们在来看更简单的 delete_story.php



通过check_permission函数来确定当前用户是否有修改的权限,如果有,就把当前的文章删除。

check_permission是在user_auth_fns.php文件中



好了,文章的修改和新建部分我们都全部介绍完了,下一篇blog,我们来介绍publish相关的3个文件







  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值