从服务器删除图像文件 [英] Delete Image Files From Server

查看:89
本文介绍了从服务器删除图像文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有人可以帮助我.

I wonder whether someone may be able to help me please.

我整理了页面,该页面使用户能够查看它们以图库格式上传的图像.

I've put together this page which allows users to view their uploaded images in a gallery format.

我现在想为每个图像添加删除功能.我已经创建了按钮及其背后的Javascript,但是我真的不确定如何将按钮单击"与文件的实际物理删除链接起来.

I'm now wanting to add the delete functionality to each image. I've created the button and the Javascript behind it, but I'm really not sure how to link the 'button click' with the actual physical deletion of the file.

这些图像没有存储在数据库中,而是存储在服务器上的两个文件夹中,结构如下:

The images aren't stored in a database but are in two folder locations on my server, in the following structure:

UploadedFiles/username/locationid/imagename

UploadedFiles/username/locationid/Thumbnails/imagename

我刚接触PHP,现在达到了我的知识极限,但当然愿意学习.从我阅读的文档中,我认为unlink方法是使用的正确命令是正确的吗?

I'm relatively new to PHP and I'm now reaching the limits of my knowledge, but certainly willing to learn. From the documentation I've read I think I'm correct in saying that the unlink method is the correct command to use?

但是我发现真正困难的是告诉代码查找具有与当前usernamelocationid匹配的usernamelocationid文件夹的文件夹,然后删除与名称变量.

But what I'm finding really difficult is telling the code to find the folders with the username and locationid folders which match the current username and locationid and then delete the files which match the $source name variable.

我只是想知道是否有人可以为我提供一些指导.

I just wondered whether someone could perhaps provide some guidance please on how I may go about this.

非常感谢和问候

推荐答案

如果可以将用户名,locationid和imagename变量传递给脚本,则可以使用unlink()删除文件:

If you can pass the username, locationid, and imagename variables to your script, you can delete the files using unlink():

$path = 'UploadedFiles/' . $username . '/' . $locationid . '/';

unlink( $path . $imagename );
unlink( $path . 'Thumbnails/' . $imagename );

因为您正在与文件系统进行交互,所以您需要确定并清理变量(防止他人使用../../../来访问文件系统中不需要的部分).

Because you are interacting with your file system, you'll want to be sure and sanitize the variables (prevent someone from using ../../../ to get to unwanted parts of your file system).

$username = str_replace( array( '..', '/', '\\', ':' ), '', $username );
$imagename = str_replace( array( '..', '/', '\\', ':' ), '', $imagename );
$locationid= str_replace( array( '..', '/', '\\', ':' ), '', $locationid );

这篇关于从服务器删除图像文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆