Symfony How To Remove A File

2019-05-26 14:37发布

问题:

Why cant I use unlink() in Symfony?

I have tried this:

unlink(/Applications/XAMPP/xamppfiles/htdocs/symfonydev/web/account_assets/data/suppliers/file.txt)

I keep getting the same reponse: Warning: unlink(/Applications/XAMPP/xamppfiles/htdocs/symfonydev/web/account_assets/data/suppliers/wordpress.txt): Permission denied in...

What Do I need to do???

I have set the permissions to 777 on this file.

回答1:

Note that you can use the remove function of the filesystem component. If you don't want to use the filesystem component, that's fine, you can use unlink(), there is a great example in this remove function of the doc:

public function removeUpload()
{
    if (isset($this->file)) {
        unlink($this->file);
    }
}

Now, the main problem is that you don't have permission to delete this file. You'd have to configure the directory like this in your virtual host:

<VirtualHost *:80>
    DocumentRoot "/Applications/XAMPP/xamppfiles/htdocs/symfonydev/web/"
    ServerName yoursite.dev
    <Directory "/Applications/XAMPP/xamppfiles/htdocs/symfonydev/web/account_assets/data/">
    AllowOverride None
    Allow from All
    </Directory>
</VirtualHost>


回答2:

It was a permissions issue on main directory that contained the files. Once I changed the owner and permissions, all worked well. The filesystem component works awesome!