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);
Related
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"];
I have seen another question with the same problem, but with just 1 answer, which didn't worked for me.
The question: PHP scandir - multiple directories
I'm using Joomla! to create my site
(I create everything with templates, most that I don't understand creating an extension).
So, I created folder pbfData in main folder.
In it I created folder users. But scandir does not detects any of those.
Errors (without if (is_dir))
Warning: scandir(/pbfData/) [function.scandir]: failed to open dir: No such file or directory in [...]/pbf/index.php on line 61
Warning: scandir() [function.scandir]: (errno 2): No such file or directory in [...]/pbf/index.php on line 61
Fatal error: Cannot access empty property in [...]/pbf/index.php on line 61
The code looks like this:
class users {
var $users;
function refUsers() {
$dir = '/pbfData/';
if (is_dir($dir)) {
$this->$users = scandir($dir);
} else {
echo "<b>CRITICAL ERROR: </b>No access to users directory.";
}
}
[...] }
It outputs
CRITICAL ERROR: No access to users directory.
(Note: adding is_dir to solve it was answer to the other question, but as I have written it doesn't work for me.)
You are checking if the folder /pdfData/ exists, and since the path starts with / what you are really checking is if you have a folder with the name pdfData inside your ROOT folder (C:\ or D:\ [etc] in windows, and / in linux/unix).
That means that if you run your script /var/www/html/pdf/index.php, event if you have the folder /var/www/html/pdf/pdfData - the function is_dir('/pdfData/') will return false, because you don't have that folder in your ROOT/
Folder structure
----------------
/pdfData/ <-- This is the folder that your code check (which doesn't exists)
/var/
/var/www/
/var/www/pdf/
/var/www/pdf/pdfData/ <-- This is the folder you want to check for
If you want to check the folder pdfData inside the current folder that you are running your code from, you should use $dir = 'pbfData/'; (without the starting slash)
Another options is to use the __DIR__ Magic constant:
$dir = __DIR__ . '/pbfData/';
is_dir is not a solution to get directory path, it just checks if the file is a directory or not. To get the path you can use JPATH_SITE or JPATH_BASE etc as you are inside Joomla. Joomla already comes with built in constants. have a look here https://docs.joomla.org/Constants .
Your code will be
$dir = JPATH_BASE."/pbfData",
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.
I'm trying to execute a mkdir from my PHP and am getting the error:
Warning: mkdir() [function.mkdir]: No such file or directory in /home3/mysite/public_html/register.php on line 220
The offending line is:
mkdir($filePath, 0777);
Does anyone know what's going on? Shouldn't mkdir be in every PHP installation?
Thanks
mkdir() is in your PHP installation and is working; the error actually shows that you're trying to create a directory inside a directory that doesn't exist.
You may need to pass true as the third parameter to make it work recursively, i.e. mkdir($path, 0777, true)
This means that the folder you are trying to create the new folder in, does not exist.
For instance, mkdir("non/existant/path/newdir") will fail.
You need to pass the optional recursive parameter to mkdir().
Let's say, in PHP, I am trying to put an image in a specific directory that is on root.
I want to put it on /images/afc/esp/stadium/ directory. Images folder, Federation folder, Country ISO3 folder, content folder.
$folder_full = "images/".$getFed."/".$country_folder."/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full);
Before you ask, yes $getFed and $country_folder work and output text. So, then I get this error: Warning: mkdir(): No such file or directory
I don't get it?
Some of your subdirectories don't exist so you either need to iteratively create them or set the 3rd argument to mkdir() to true. Note that the second argument are the directory permissions (ignored on Windows) which default to 0777.
You also need to set $folder_full to the root using /.
$folder_full = "/images/{$getFed}/{$country_folder}/stadiums";
if (!is_dir($folder_full)) mkdir($folder_full, 0777, true);
You need to use the recursive parameter to add directories that don't exist in the path you provide: mkdir($folder_full, 0777, true)
See the PHP docs here
All the intermediary directories have to already exist. You can trigger this behaviour by using the optional third argument:
mkdir($folder_full,0777,true);
in normal cPanel the folder permissions should be 0755
so the command in this case will be:
mkdir('tst/tst2/tst3', 0755, true);