unlink()如何找到要删除的文件?

发布于 2024-08-29 11:28:25 字数 1989 浏览 2 评论 0原文

我的应用程序有一个“照片”字段来存储 URL。它使用 sfWidgetFormInputFileEditable 作为小部件架构。要在上传新图像时删除旧图像,我在重写设置器中设置值之前使用 unlink ,它有效!

if (file_exists($this->_get('photo')))
    unlink($this->_get('photo'));
  1. 照片存储在 uploads/photos 中,保存“Photo”时,仅保存文件名 xxx-yyy.zzz(而不是完整路径)。但是,我想知道symfony/php如何知道要删除的文件的完整路径?

第 2 部分: 我正在使用 sfThumbnailPlugin 来生成缩略图。所以实际的代码如下所示:

public function setPhoto($value)
    {   
        if(!empty($value))
            {
                Contact::generateThumbnail($value); // delete current Photo & create thumbnail
                $this->_set('photo',$value);    // setting new value after deleting old one
            }
    }   

    public function generateThumbnail($value)
    {
                $uploadDir = sfConfig::get('app_photo_upload'); // path to upload folder
                if (file_exists($this->_get('photo')))
                {
                  unlink($this->_get('photo')); // delete full-size image
                  // path to thumbnail
                  $thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo'); 
                  // read a blog, tried setting dir manually, doesn't work :(
                  //chdir('/thumbnails/');
                  // tried closing the file too, doesn't work! :(
                  //fclose($thumbpath) or die("can't close file"); 
                  //unlink($this->_get('photo'));   // doesn't work; no error :( 
                  unlink($thumbpath);   // doesn't work, no error :(
                }

                 $thumbnail = new sfThumbnail(150, 150);
                 $thumbnail->loadFile($uploadDir.'/'.$value);
                 $thumbnail->save($uploadDir.'/thumbnails/'.$value, 'image/png');
    }
  1. 为什么不能使用 unlink() 删除缩略图?操作顺序是否不正确? 是否是因为 sfWidgetFormInputFileEditable 小部件中显示了旧的缩略图?

我花了几个小时试图解决这个问题,但无法确定真正的原因。 提前致谢。

My app has a 'Photo' field to store URL. It uses sfWidgetFormInputFileEditable for the widget schema. To delete the old image when a new image is uploaded, I use unlink before setting the value in the over-ridden setter and it works!!!

if (file_exists($this->_get('photo')))
    unlink($this->_get('photo'));
  1. Photos are stored in uploads/photos and when saving 'Photo' only the file name xxx-yyy.zzz is saved (and not the full path). However, I wish to know how symfony/php knows the full path of the file to be deleted?

Part 2:
I am using sfThumbnailPlugin to generate thumbnails. So the actual code looks like this:

public function setPhoto($value)
    {   
        if(!empty($value))
            {
                Contact::generateThumbnail($value); // delete current Photo & create thumbnail
                $this->_set('photo',$value);    // setting new value after deleting old one
            }
    }   

    public function generateThumbnail($value)
    {
                $uploadDir = sfConfig::get('app_photo_upload'); // path to upload folder
                if (file_exists($this->_get('photo')))
                {
                  unlink($this->_get('photo')); // delete full-size image
                  // path to thumbnail
                  $thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo'); 
                  // read a blog, tried setting dir manually, doesn't work :(
                  //chdir('/thumbnails/');
                  // tried closing the file too, doesn't work! :(
                  //fclose($thumbpath) or die("can't close file"); 
                  //unlink($this->_get('photo'));   // doesn't work; no error :( 
                  unlink($thumbpath);   // doesn't work, no error :(
                }

                 $thumbnail = new sfThumbnail(150, 150);
                 $thumbnail->loadFile($uploadDir.'/'.$value);
                 $thumbnail->save($uploadDir.'/thumbnails/'.$value, 'image/png');
    }
  1. Why can't the thumbnail be deleted using unlink()? is the sequence of ops incorrect?
    Is it because the old thumbnail is displayed in the sfWidgetFormInputFileEditable widget?

I've spent hours trying to figure this out, but unable to nail down the real cause.
Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

雨轻弹 2024-09-05 11:28:25

$path = "上传/照片";
$image = "图像名称";

unlink($path.$image);

$path = "uploads/photos";
$image = "Name of image";

unlink($path.$image);
天煞孤星 2024-09-05 11:28:25

已解决&令人惊讶的

是,这是添加一些回声后起作用的代码...

public function generateThumbnail($value)
    {
             $uploadDir = sfConfig::get('app_photo_upload'); // path to upload folder
                // path to thumbnail
             $thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo'); 
             if (file_exists($uploadDir.$this->_get('photo')))
             {
            >>    unlink($uploadDir.$this->_get('photo')); // delete full-size image
            >>    unlink($thumbpath); // delete thumn
             }

    //thumbnail generation code
    }

据说, unlink($this->_get('photo')) 从未起作用。事实上,if(fileExists) 块从未被输入,但文件已被删除。
我认为 sfWidgetFormInputFileEditable 在上传新图像时会自动删除完整图像。

谢谢马丁和卡纳克

Solved & Surprised

OK, here is the code that worked after adding some echoes...

public function generateThumbnail($value)
    {
             $uploadDir = sfConfig::get('app_photo_upload'); // path to upload folder
                // path to thumbnail
             $thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo'); 
             if (file_exists($uploadDir.$this->_get('photo')))
             {
            >>    unlink($uploadDir.$this->_get('photo')); // delete full-size image
            >>    unlink($thumbpath); // delete thumn
             }

    //thumbnail generation code
    }

Supposedly, the unlink($this->_get('photo')) never worked. Infact, the if(fileExists) block was never entered and yet the file was deleted.
I think sfWidgetFormInputFileEditable was deleting the full-image automatically when a new one was being uploaded.

Thanks Martin & Kanak

婴鹅 2024-09-05 11:28:25

你确定这条线吗?

$thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo');

我认为你应该使用 $this->_get('photo'); 而不是 $this->get('photo');

也许尝试转储 <代码>$thumbpath 变量。

var_dump($thumbpath); exit;

Are you sure about this line?

$thumbpath = $uploadDir.'/thumbnails/'.$this->get('photo');

I think you should use $this->_get('photo'); instead of $this->get('photo');

Maybe try to dump $thumbpath variable.

var_dump($thumbpath); exit;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文