Laravel不会删除目录 [英] Laravel doesn't delete directory

查看:99
本文介绍了Laravel不会删除目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在删除目录时遇到问题.

I have a problem deleting a directory.

我正在创建一个临时目录,我在其中解压缩文件,并在完成后想删除它.

I'm creating a temporary directory where I unzip a file and I want to delete it when I'm done with it.

我的代码看起来很像这样:

My code looks pretty much like this:

$tempPath = storage_path('temp/'.$filemd5.'/');
Storage::makeDirectory($tempPath);
$success = Storage::deleteDirectory($tempPath);
return $success;

它不返回任何内容,也不删除目录.

It doesn't return anything and the directory doesn't get deleted.

推荐答案

也许您对storage/app目录(我认为这是默认目录)没有足够的权限.检查一下,如果那行不通,您可以随时使用PHP exec函数(假设您使用的是Linux):

Maybe you don't have enough permissions on your storage/app directory (which I think is the default one). Check that, and if that doesn't work, you can always appeal to the PHP exec function (assuming that you're on linux):

exec('rm -r ' . storage_path() . '/' . $tempPath)

更新:问题是makeDirectory具有第二个int参数,该参数默认将权限设置为511,即-r-x--x--x(即读取和执行,但不写入).解决方法是将第二个参数设置为711,即-rwx--x--x,您将能够删除创建的目录.将另外两个参数作为true传递很有用,因为它将递归地应用权限并强制执行操作.

UPDATE: the problem was that makeDirectory has a second int parameter that sets the permissions by default to 511 which is -r-x--x--x (i.e. read and execute, but not write). The workaround is to set the second parameter to 711 which is -rwx--x--x and you'll be able to delete the created directory. It's useful to pass the other two parameter as true because it will apply permissions recursively, and force the operation.

正如亚历克斯所说,您必须使用File而不是Storage.因此,用于创建目录的最终代码(以后可以删除)是:

As Alex says, you have to use File instead of Storage. So the final code for creating directory that later you will be able to delete is:

File::makeDirectory($tempPath, 0711, true, true); 

这篇关于Laravel不会删除目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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