使用数据库表从上传路径中删除文件
我使用以下代码从数据库中删除了图像网址,但没有从上传路径中删除文件图像,如何使用数据库中的网址(两者一起)从上传路径中删除文件?
此 php 仅用于从数据库中删除 url:
$delete = $this -> input -> post('checked');
foreach($delete as $val) {
$this - > db - > delete($this - > _table, array('id' = > $val));
}
更新:
我在使用以下代码时出错。
$id = '166';
$query = $this - > db - > get_where('hotel_image', array('id' = > $id));
if ($query - > num_rows() > 0) {
$res = $query - > row();
unlink(base_url(uploads/$res - > images));//This is line 161
}
$this - > db - > delete('hotel_image', array('id' = > $id));
错误:(我的图像位于此pach中:http://example.com/uploads/
)
遇到 PHP 错误
严重性:通知
消息:使用 未定义的常量上传 - 假定为“上传”
文件名:user/dence.php
行号:161遇到 PHP 错误
严重性:警告
消息: unlink() [function.unlink]: http 不允许取消链接
文件名:user/dence.php
行号:161
I done delete image url from database with following code, but is not delete file image from upload path, how can delete file from upload path with url it in database (both together)?
This php is just for delete url from database:
$delete = $this -> input -> post('checked');
foreach($delete as $val) {
$this - > db - > delete($this - > _table, array('id' = > $val));
}
Update:
I have error in use of following code.
$id = '166';
$query = $this - > db - > get_where('hotel_image', array('id' = > $id));
if ($query - > num_rows() > 0) {
$res = $query - > row();
unlink(base_url(uploads/$res - > images));//This is line 161
}
$this - > db - > delete('hotel_image', array('id' = > $id));
Error:(my images are in this pach: http://example.com/uploads/
)
A PHP Error was encountered
Severity: Notice
Message: Use of
undefined constant uploads - assumed 'uploads'
Filename: user/dence.php
Line Number: 161A PHP Error was encountered
Severity: Warning
Message:
unlink() [function.unlink]: http does not allow unlinking
Filename: user/dence.php
Line Number: 161
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您需要使用 unlink() 来从磁盘删除文件,只需传递文件名作为参数即可。
类似的东西(这是简化的代码。您可能想检查数据库的路径是否完整,或者添加其余部分。):
You need to use unlink() to delete a file from disk, just pass the filename as parameter.
Something like (this is simplified code. You might want to check the path from DB is complete or else add the rest.):