PHP创建文件夹,如果它不存在 [英] PHP create folder if it does not exist

查看:147
本文介绍了PHP创建文件夹,如果它不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个中型应用程序。

I am creating a medium size application.

此应用程序包含大量产品。

This application consists of a lot of products.

现在这些产品有许多图片(一个产品可以有5 - 6张图片)

Now these products have many images (one product can have 5 - 6 images)

要尝试做一些排序我想为每个产品创建一个文件夹这个文件夹

To try and make some sort of ordering I want to create one folder for each product this folder contains all images that is bound to the product.

到目前为止,我已经尝试过以下操作:

Now so far I have tried the following:

move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name']);

但是,当我尝试这个时,我得到以下错误:

However when I try this I get the following error:

Warning (2): move_uploaded_file(/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png): failed to open stream: No such file or directory [APP/Controller/ImagesController.php, line 56]

Warning (2): move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/php472ci6' to '/var/www/udlejnings-priser/cake/app/product_images/22/afterClick.png' [APP/Controller/ImagesController.php, line 56]

现在我不是一个完整的noob,知道这意味着我失去了该文件夹的权限。

Now I am not a complete noob and know that this means that I am missing permissions to the folder.

但是问题是,如果文件夹不存在(即这是第一次上传该产品的图像),那么应该创建一个新的文件夹。

However the problem is that if the folder does not exist (i.e this is the first time an image for that product is uploaded) then a new folder should be created.

我的


  1. 如果尚未存在,是否自动创建一个新文件夹?

  2. 如何向新创建的文件夹授予权限,以避免此问题?


推荐答案


[I]知道这意味着我对该文件夹的权限丢失。

[I] know that this means that i am missing permission to the folder.

no =)。错误讯息如下:

Actually no =). The error message reads:


无法开启讯息串:没有这类档案或目录

failed to open stream: No such file or directory

其中没有提到权限的问题是:您尝试写入的包含文件夹不存在。

Which makes no reference to permissions the problrm is: the containing-folder you're trying to write to doesn't exist.

否。

没有必要这样做 - 创建的任何内容都将具有允许Web服务器用户读取文件的正确权限。

It's not necessary to do so - anything created will have the correct permissions to permit the webserver user to read the files. However first it's necessary to try and create a folder, which in the question isn't the case.

使用CakePHP, =http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html =nofollow>文件夹类可用于:

Using CakePHP, the Folder class can be used to do that:

App::uses('Folder', 'Utility');
$dir = new Folder('/path/to/folder', 2);

第二个参数使用创建新文件夹(如果不存在)。在这个问题的上下文中,代码将看起来像这样:

The second parameter is used to create a new folder if it doesn't exist. In the context of the question that means the code would look something like this:

function whatever() {

    if ($this->request->data) {
        ...

        $unused = new Folder(APP.'product_images/'.$product_id, true);
        if (move_uploaded_file($file, APP.'product_images/'.$product_id.'/'.$image['name'])) {
            ...
        } else {
            ...
        }
    }
}

文件夹 APP / product_images 应该已存在,必须具有权限,以便网络服务器用户(例如apache)写入,否则将无法创建子文件夹/上传文件。假设 APP / product_images 存在,并且webserver用户具有写入权限,则不需要修改上传文件的权限 - 用户创建的文件默认可读该用户。

The folder APP/product_images should already exist, and must have permissions such that the webserver user (e.g. apache) can write to it otherwise it will not be possible to create the sub-folders/upload files. Assuming APP/product_images exists and the webserver user has permissions to write to it, there is no need to modify permissions of uploaded files - files created by a user are by default readable by that user.

这篇关于PHP创建文件夹,如果它不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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