4

I am creating a medium size application.

This application consists of a lot of products.

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]

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.

My question is two parted.

  1. Does this automatically create a new folder if it doesn't already exist?
  2. How can I give permission to a newly created folder so that I avoid this problem?
0

5 Answers 5

6

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

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.

Does this automatically create a new folder if it doesn't already exist?

No.

How can i give permission to a newly created folder?

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.

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 {
            ...
        }
    }
}

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.

Sign up to request clarification or add additional context in comments.

Comments

5

Try this:

if (!file_exists('path/to/directory')) {
    mkdir('path/to/directory', 0777, true);
}

1) Does this automaticly create a new folder if it doesnt already exist. => file_exists and mkdir

2) how can i give permission to a newly created folder so that i avoid this problem => 0777

1 Comment

recommending 0777 permissions, especially to solve a permission problem that doesn't exist, is bad advice.
0
  1. No, it won't create the folder dynamically.
  2. Use chmod() to change permissions.

For checking the existence of some folder you can use is_dir() too.

Comments

0

A more bulletproof way to get the permission you want:

$path = 'path/to/directory';

if (! is_dir($path)) {
    $oldmask = umask(0);
    mkdir($path, 0755, true);
    umask($oldmask);
    clearstatcache();
}

The third argument to mkdir() will also create any directories above your directory that don't exist.

The permission, 0755, is generally recommended for PHP directories (the leading 0 is necessary). Directories set to 0777 are inaccessible on most hosts.

The clearstatcache() call is only necessary if you will be making multiple uses of the directory, which it appears you are.

Comments

-1

It's always a good idea too add the magic constant __DIR__ to the file or directory path. ( __DIR__ gives out the path to the directory in which your script is located). In the errormessages "APP" is highlighted in a different colour than the path name. This could be a hint that the path cannot be located.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.