Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 仅将取消链接限制为一个_Php - Fatal编程技术网

Php 仅将取消链接限制为一个

Php 仅将取消链接限制为一个,php,Php,阿罗哈,我有一个下载按钮,当图像被下载时,它会使用会话(路径)进行显示。然后我删除了以前使用过的图片,但是当图片的名称相同时,“unlink($\u SESSION['picture']);”会同时删除这两个图片-我希望每次只删除一个。。。像这样的“取消链接($_会话['picture']

阿罗哈,我有一个下载按钮,当图像被下载时,它会使用会话(路径)进行显示。然后我删除了以前使用过的图片,但是当图片的名称相同时,“unlink($\u SESSION['picture']);”会同时删除这两个图片-我希望每次只删除一个。。。像这样的“取消链接($_会话['picture']<1);”是不起作用的。 请帮忙:),阿罗哈

这是保存图片的代码

session_start();

$filename    = $_FILES["picture"]["tmp_name"];
$destination = "upload/" . $_FILES["picture"]["name"]; 
move_uploaded_file($filename, $destination); //save uploaded picture in your directory
$_SESSION['picture'] = $destination;
我们已经说过了。 从上传文件开始

session_start();

$filename    = $_FILES["picture"]["tmp_name"];
//Cutted the file name from the $destination
$destination = "upload/"; 

//Insert Into DB the info.
//Query Similar To
$sql = "INSERT INTO mytable (filename) VALUES (".$filename.")";

//Get from DB the ID generated.
$sql2 = "SELECT id FROM mytable WHERE filename = '".$filename."'";
//Fetch the data into a $filenameNew var

move_uploaded_file($filename, $destination.$filenameNew); //save uploaded picture in your directory with the db id

$_SESSION['picture'] = $destination.$filenameNew;
改为删除

 $sql2 = "SELECT id FROM mytable WHERE ..."; //Get the id name of the file
//put id on a var - $toUnlink
unlink($toUnlink);

$sqlDelete = "DELETE FROM mytable WHERE id = '".$toUnlink."'";

这是一条工作链。您需要做一些工作,以使其根据您的应用程序正确工作。

正是由于单一性,最好给出记录的名称,并在数据库中存储文件的“真实”名称。通过这种方式,您可以轻松地取消设置一个映像。好的,我将尝试:)例如,如果您使用db,您可以使用一个字段作为实际文件名,并使用db记录的唯一性(例如一个自动递增字段)来命名文件系统=)上的文件,为此,我是否只需要将“move_ipload_file”更改为“mysql insert”?否,您需要为一个文件创建一个记录,并从dbHi mate获取该文件,将数据//提取到$filenamew var中是什么?好的,从$destination中//剪切文件名是什么?以在上载时更改该文件。您可以使用$destination=“upload/”$_文件[“图片”][“名称”]。如果您确实删除了字符串(“upload/”)之后的内容,您将文件夹放在一个var中,然后我将其与(new)$filenameNew连接起来,在上传时为其指定一个新名称。请查看我的JSFIDLE,这是链接:请更正我:)