Home Backend Development PHP Tutorial php资料的上传与删除方法

php资料的上传与删除方法

Jun 13, 2016 pm 12:49 PM
gt lt name quot script

php文件的上传与删除方法

1.php文件的上传

先简单布局一个html操作界面:(图片上传为例)

Copy after login



submit提交之后,然后判断上传的文件是否为空。同时可以进行文件大小的控制,获取文件名之后,上传文件。

<?php if($_POST["submit"])
{       //通过字符串截取函数explode()截取出文件后缀名
	$name = $_FILES['photo']['name'];    <span style="font-family: Arial, Helvetica, sans-serif;">//获取上传文件的文件名
	$string = explode(".",$name);
	$i = count($string);
	$substring = $string[$i-1];
	
	//判断文件大小,名称
	if($_FILES['photo']['size'] &gt; 0 &amp;&amp; $_FILES['photo']['name'])
	{
		$dir = 'upfiles/';                     //设置保存目录
		if(!is_dir($dir))                      //如果没有该目录
		{
			mkdir($dir);                   //则创建该目录
		}
		
		$format = "Yndhis";
		$date = date($format);     //可以通过设置当前时间来重命名文件名


		$name = $date.".".$substring;  //重新组合文件名    当前时间.文件后缀名
		$path = 'upfiles/'.$name;             //组合成完整的保存路径(目录+文件名)
		
		$i = move_uploaded_file($_FILES['photo']['tmp_name'],$path);   //保存文件到创建的目录下
		if($i == false)
		{
			echo "<script>alert('文件保存失败!');</script>";
			//echo $path;
		}
		//保存到数据库中,保存链接信息(文件地址)到数据库中,即例中的p_url  
		$str = "INSERT INTO picture(s_id,p_url,p_info)VALUES($sid,'$path','$info')";
		$result = mysql_query($str);
	
		if($result)
		{
			echo "<script>alert('图片添加成功!');</script>";
		}
		else
		{
			//echo $str;
			echo "<script>alert('图片添加失败!');</script>";
		}
	}
}
	
?&gt;
Copy after login

文件上传的主要php函数就是move_upload_file("文件名","文件路径"),注意不要写错了。


2.php文件删除

php文件删除只需要使用unlink()函数即可。

<?php /* 图片删除处理页 */
if($_GET["p_url"])
{
	$purl = $_GET["p_url"];    //获取文件保存路径
	$file_delete = "../".$purl;    //根据自己的文件目录设置路径信息
	
	$str = "DELETE FROM picture WHERE p_url='".$purl."'";    //从数据库中删除图片文件
	$delete = mysql_query($str);
	if($delete)
	{
		unlink($file_delete);    //从自己写入的路径删除图片文件
		echo "<script>alert('图片信息删除成功!');window.location.href='picture_manage.php'";
	}
	else
	{
		//echo $str;
		echo "<script>alert('图片信息删除失败!');window.location.href='picture_manage.php'</script>";
	}
}
else
{
	echo "<script>alert('请选择要删除的图片信息!');window.location.href='picture_manage.php'</script>";
}
?&gt;
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the differences between Huawei GT3 Pro and GT4? What are the differences between Huawei GT3 Pro and GT4? Dec 29, 2023 pm 02:27 PM

What are the differences between Huawei GT3 Pro and GT4?

Fix: Snipping tool not working in Windows 11 Fix: Snipping tool not working in Windows 11 Aug 24, 2023 am 09:48 AM

Fix: Snipping tool not working in Windows 11

How to Fix Can't Connect to App Store Error on iPhone How to Fix Can't Connect to App Store Error on iPhone Jul 29, 2023 am 08:22 AM

How to Fix Can't Connect to App Store Error on iPhone

What does script mean? What does script mean? Aug 29, 2023 pm 02:00 PM

What does script mean?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

what is script what is script Oct 12, 2023 am 10:04 AM

what is script

How to solve scripterror How to solve scripterror Oct 18, 2023 am 09:44 AM

How to solve scripterror

Is watch4pro better or gt? Is watch4pro better or gt? Sep 26, 2023 pm 02:45 PM

Is watch4pro better or gt?

See all articles