简体   繁体   English

如果用户删除图片,如何上传默认图片?

[英]How to upload default picture if user delete the picture?

So, i want to make if user click the delete button the picture profile will be user.jpg (this is default picture).所以,如果用户单击删除按钮,我想制作图片配置文件将是 user.jpg(这是默认图片)。

enter image description here在此处输入图像描述

here is my controller:这是我的 controller:

public function update()
{
    $fileimg = $this->request->getFile('img');
    $oldimg= $this->request->getVar('oldimage');
    $defaultimg= $this->request->getVar('defaultimage');
    
    if ($fileimg == 'user.jpg') {
        $uploadImg = $defaultimg;
    }
    if ($fileimg->getError() == 4) {
        $uploadImg= $oldimg;
    } else {
       
        $uploadImg = $fileimg->getRandomName();
        
        $fileimg->move('img/', $uploadImg);
        

        if ($oldimg != 'user.jpg') {

            unlink('img/' . $oldimg);
        }
    }

When i click delete button image the image will be user.jpg and then i click button upload but the image wont change to user.jpg, but always showing the old picture.当我单击删除按钮图像时,图像将是 user.jpg 然后我单击按钮上传但图像不会更改为 user.jpg,但始终显示旧图片。 sorry for bad english.对不起英语不好。

Write the image name in a database somewhere, usually in the user profile table.将图像名称写入数据库中的某处,通常是在用户配置文件表中。 It will make your life much easier, and you'll be able to access it all the time.它将使您的生活更轻松,并且您可以随时访问它。 Then in your model, you'll be able to see if there is no image and return the default image.然后在你的 model 中,你将能够看到是否没有图像并返回默认图像。

I am not sure what your getRandomName() is doing, but some hash function (sha1, md5...) are good enough for creating a file name to avoid possible name collisions.我不确定你的getRandomName()在做什么,但一些 hash function (sha1,md5 ...)足以创建文件名以避免可能的名称冲突。

How about think like this:不妨这样想:

Default picture is not a data which user is willingly save to database.默认图片不是用户愿意保存到数据库中的数据。 It's rather a placeholder.它更像是一个占位符。 Thinking further, it can be handled in the views.进一步思考,可以在视图中处理。 If user profile picture is not set or exists, show default picture.如果用户个人资料图片未设置或存在,则显示默认图片。 To prevent typing manually you can make it a global data or a config value or a constant.为了防止手动输入,您可以将其设为全局数据或配置值或常量。 But the key is it is a placeholder, not a data.但关键是它是占位符,而不是数据。

Happy coding...快乐的编码...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM