Home  >  Article  >  Backend Development  >  php 删除记录同时删除图片文件的实现代码_PHP

php 删除记录同时删除图片文件的实现代码_PHP

WBOY
WBOYOriginal
2016-06-01 12:18:12885browse
复制代码 代码如下:
$cn = mysql_connect('127.0.0.1','root','root') or die('database connect fail');
mysql_select_db('test',$cn);
mysql_query("set names 'gbk'");
/*
创建数据库教程
CREATE DATABASE `test` ;
创建数据表 test1
CREATE TABLE `test`.`test1` (
`id` INT( 4 ) NOT NULL AUTO_INCREMENT ,
`title` VARCHAR( 40 ) NULL DEFAULT '0',
`litpic` VARCHAR( 100 ) NULL DEFAULT '0',
PRIMARY KEY ( `id` )
) ENGINE = MYISAM
*/
$sql = "Select * from test1 ";
$query = mysql_query( $sql ) or die($sql);
if( mysql_num_rows( $query ) )
{
while( $rs = mysql_fetch_array( $query ) )
{
$sql = "Delete from test1 where id=".$rs['id'];
if( mysql_query( $sql ) )
{
if( defile( $rs['litpic'] ))
{
echo '删除图片文件失败';
}
else
{
echo '记录删除成功,并且删除了记录与相对应的图片或文件';
}
}
else
{
echo '记录不存删除数据与图片失败';
}
}
}
else
{
echo '没有记录';
}
/*
@pare return
#$url string
删除文件
*/
function delfile($url)
{
if( file_exists($url) && is_file( $url ) )
{
if( unlink( $url ) )
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}

Statement:
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