PHP:删除具有任何扩展名的文件吗? [英] PHP: Delete a file with any extension?

查看:94
本文介绍了PHP:删除具有任何扩展名的文件吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户上传照片时,它会检查是否已经有照片;如果他们愿意,我希望它删除旧的(可以有任何扩展名),然后放入新的.有没有一种方法可以在不从数据库中获取旧扩展名的情况下进行?目前的代码:

When a user uploads a photo, it checks to see if they already have one; and if they do I want it to delete the old one (which could have any extension) and then put the new one. Is there a way to do that without getting the old extension from the database? Code at the moment:

        $del = $members->prepare("insert into profiles(userid, path, width, height, type, rtu, ext) values(?, ?, ?, ?, ?, ?, ?) 
                                    ON DUPLICATE KEY UPDATE path = ?, width = ?, height = ?, type = ?, rtu = ?, ext = ?, time = NOW()");
        $del->bind_param('sssssssssssss', $_SESSION['token'], $title, $file_info[0], $file_info[1], $file_info[2], $rh, $extension, 
        $title, $file_info[0], $file_info[1], $file_info[2], $rh, $extension);

        $del->execute();

        $new_file_name = "bb-x".$_SESSION['token'].".".$extension;

        if ($del->affected_rows > 0) {

            unlink('profiles/bb-x62'<any extension>);
        }

        $move_file = move_uploaded_file($file['tmp_name'], $upload_image_to_folder.$new_file_name);

推荐答案

而不是unlink('profiles/bb-x62'<any extension>);

使用这两行:

$file_pattern = "profiles/bb-x62.*" // Assuming your files are named like profiles/bb-x62.foo, profiles/bb-x62.bar, etc.
array_map( "unlink", glob( $file_pattern ) );

应该可以解决问题.

这篇关于PHP:删除具有任何扩展名的文件吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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