dongzhuohan7085 2016-05-29 21:07
浏览 25

PHP重命名/更改特色图片

I'm trying to make a PHP script that changes the featured picture of some item. The general principle is that it when $_POST['featured'] is set, it renames that picture to featured.jpg and renames all other pictures to their hash_file value. However, it works well the first time when there is no featured picture set, but when I try to change it from one featured picture to another, the previous featured picture gets removed and the new one doesn't get renamed to featured.jpg. All pictures are in the same folder.

Here is the relevant code:

if (isset($_POST['featured'])) {
    $id = $_POST['id'];
    $slike = glob('../img/uploads/'.$id.'/*.{jpg,png}', GLOB_BRACE);
    if ($slike != null) {
        foreach ($slike as $slika) {
            $path = realpath($slika);
            $name = basename($path);
            if($name == basename($_POST['featured'])){
                if(!file_exists(dirname($path) . '/featured.jpg')){
                    rename($path, dirname($path) . '/featured.jpg');
                }else{
                    rename(dirname($path) . '/featured.jpg', dirname($path) . '/' . hash_file('md5', $path) . '.jpg');
                    rename($path, dirname($path) . '/featured.jpg');
                }
            }else{
                rename ($path, dirname($path) . '/' . hash_file('md5', $path) . '.jpg');
            }
        }
    }
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题
    • ¥15 matlab不知道怎么改,求解答!!
    • ¥15 永磁直线电机的电流环pi调不出来
    • ¥15 用stata实现聚类的代码
    • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
    • ¥20 docker里部署springboot项目,访问不到扬声器
    • ¥15 netty整合springboot之后自动重连失效