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.
Does this automatically create a new folder if it doesn't already exist?
How can I give permission to a newly created folder so that I avoid this problem?
[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.
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
No, it won't create the folder dynamically.
Use chmod() to change permissions.
For checking the existence of some folder you can use is_dir() too.
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.
Related
Does anyone know how to create a folder in a network using php? I saw many comments and discussions about creating folder. But most of them talking about creating a folder in the root folder using mkdir().
What I need is to create a folder outside that. Lets say I want to create a folder in the ip location \\192.11.11.111\TEMP\Folder. But when I tried the following code
$structure = '\\192.11.11.111\TEMP\Folder';
mkdir ($structure);
It is producing an error Warning: mkdir(): No such file or directory in C:\MAMP\htdocs\
I also tried by using a letter instead of ip.
For Eg: z:\TEMP\Folder
But also not able to create folder. Mentioned location has full right to read and write. So issue is not due to permission also.
Can someone help?
I am able to create a folder by doing following way
$structure="//192.11.11.111/TEMP/Folder";
mkdir($structure, 0, true);
So the issue was due to the usage of wrong slashes.
I am trying to create a temporary directory but I am getting following error
Warning: mkdir(): File exists
However when i checked the directory actually doesn't exist.A typical value for $tmp is /tmp/testKanfEt
$tmp = tempnam(sys_get_temp_dir() , 'test');
echo $tmp;
mkdir($tmp);
What am i missing here ?
The function tempnam() creates a file in given path and returns the full path if created successfully. You are attempting to make a directory with the same path as the file.
But in this case, tempnam() is being used wrongly in my opinion.
It should be used when trying to make tmp files in any other directory than the o.s. tmp folder.
Why? Because making files in the tmp directory you should not care about the file name because once the lock has been released on a file (with fclose() or end of script execution for example) you cannot guarantee the file still being there.
Instead, use tmpfile() as it returns a file handle directly creating a file in the tmp directory.
And if you really need the file name, you still could use tempnam() or retreive it from the handle like so:
echo stream_get_meta_data(($fh=tempfile()))["uri"];
Well my PHP script generated an error with a hyperlink in it.
Does anyone know what's wrong?
PHP Warning: rename(./uploads/temp/00013/,./uploads/orders/39/) [<a href='function.rename'>function.rename</a>]: No such file or directory
update:
actual code in PHP
if(!file_exists('uploads/orders/')) {
mkdir('uploads/orders/'); // ensuring the orders folder exist
}
rename('uploads/temp/' . $u . '/', 'uploads/orders/' . $i . '/');
update:
Sorry, my fault. I coded to delete previous temp folder before this code execute. Thanks!
It seems that one (or both) of these directories don't exist:
uploads/temp/00013
uploads/orders/39
Have you checked that:
these directories exist?
Apache/PHP has permission to read/write in these directories?
Your current directory is really the parent directory of your "upload" directory?
When a computer tells you
No such file or directory
the first thing you should check is if the file/directory exists. This is not a random error message, it's given only in the specific situation when a file or directory you try to use does not exist.
In this case in particular, both ./uploads/temp/00013/ and ./uploads/orders/ have to exist. If orders doesn't exist it's not created for you.
I am trying to create folder like this
$destination = "../folder_name/_pcode/../_compile";
mkdir($destination);
But this gives error
UPDATE
What i am doing is, taking ../folder_name/_pcode as input from user and I want to create a folder outside the _pcode Directory
Here is my error
Warning: mkdir() [function.mkdir]: No such file or directory in G:\wamp\www\tools\compile.php on line 57
../folder_name/_pcode/../_compile
It is likely that you're trying to create a subdirectory within a non-existing directory, in this case, if you want to create the whole directory path, try setting mkdir's $recursive parameter to true:
mkdir($destination, 0777, true);
I have a folder named "repository" in my admin folders. This folder holds 2 files: index.html and content.php. When a user creates a new page the php creates a new folder specified by the user then will need to copy the two files into that folder while leaving them in the repository.
copy(file,dest) does not work.
rename(file,dest) moves the files to the new folder but I lose them in the repository.
How do I copy the files in one folder to the new folder without losing the files in the original folder?
$dest = '../'.$menuLocation.'/'.$pageName;
$file1= "repository/index.html";
$file2= "repository/content.php";
mkdir($dest,0777);
rename($file1,$dest.'/index.html');
rename($file2,$dest.'/content.php');
$menuLocation and $pageName are supplied by the user. The files are there, file_exists returns back true. Also, the directory is created with no issues. rename() also works I just lose the files in repository.
For anyone hunting for the solution to this:
when using copy() in php you also need to include the file name.
copy(orginalfile,destination_with_filename);
for example:
wrong:
copy('/temp/sports/basketball.jpg','/images/sports/')
Correct:
copy('/temp/sports/basketball.jpg','/images/sports/basketball.jpg')
Use copy(). Make sure you capture the return value so that your program knows if it worked or not. Also check permission of the files and directory to confirm that the user executing the PHP script is able to create the new file in the place you specified. You might want use is_writable() on the directory to confirm these assumptions.