thinkphp 删除图片文件 unlink函数 路径如何获取 绝对 还是相对
写法一:if(isset($id)){$condition=array($pk=>array('in',explode(',',$id)));$arr=$model->w...
写法一:
if (isset ( $id )) {
$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
$arr= $model->where($condition)->find();
if(!empty($arr['image'])){
$path = $_SERVER['DOCUMENT_ROOT'].'botao/Public/Uploads/'.$arr['image'];
$fopens =fopen($path);
echo $path;
if(unlink($path)){
echo "1";
}else{
echo "2";
}exit;
}
if (false !== $model->where ( $condition )->delete ()) {
//echo $model->getlastsql();
$this->success ('删除成功!');
} else {
$this->error ('删除失败!');
}
}
if (isset ( $id )) {
$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
$arr= $model->where($condition)->find();
if(!empty($arr['image'])){
$path = __ROOT__.'/Public/Uploads/'.$arr['image'];
echo $path;
if(unlink($path)){
echo "1";
}else{
echo "2";
}exit;
}
写法2:
if (false !== $model->where ( $condition )->delete ()) {
//echo $model->getlastsql();
$this->success ('删除成功!');
} else {
$this->error ('删除失败!');
}
} 展开
if (isset ( $id )) {
$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
$arr= $model->where($condition)->find();
if(!empty($arr['image'])){
$path = $_SERVER['DOCUMENT_ROOT'].'botao/Public/Uploads/'.$arr['image'];
$fopens =fopen($path);
echo $path;
if(unlink($path)){
echo "1";
}else{
echo "2";
}exit;
}
if (false !== $model->where ( $condition )->delete ()) {
//echo $model->getlastsql();
$this->success ('删除成功!');
} else {
$this->error ('删除失败!');
}
}
if (isset ( $id )) {
$condition = array ($pk => array ('in', explode ( ',', $id ) ) );
$arr= $model->where($condition)->find();
if(!empty($arr['image'])){
$path = __ROOT__.'/Public/Uploads/'.$arr['image'];
echo $path;
if(unlink($path)){
echo "1";
}else{
echo "2";
}exit;
}
写法2:
if (false !== $model->where ( $condition )->delete ()) {
//echo $model->getlastsql();
$this->success ('删除成功!');
} else {
$this->error ('删除失败!');
}
} 展开
1个回答
展开全部
绝对路径,用配置参数的方式组织路径吧,这样以后环境变更后也不影响使用
追问
$path = $_SERVER['DOCUMENT_ROOT'].'botao/Public/Uploads/'.$arr['image'];
这就是配置参数的方式吧???
追答
我的意思是在实际应用中根据业务需要组织图片路径,你在保存图片路径到数据库的时候只需存储图片名字就可以了,至于你这个($path = $_SERVER['DOCUMENT_ROOT'].'botao/Public/Uploads/'.$arr['image'];
)也算是哈。在做东西的时候,多考虑一下以后的扩展,让自己的代码更容易移植
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询