mkdir fails in every way - php

Got this error: mkdir(): No such file or directory.
This is strange because ofcourse the directory doesn't exists because i want to create it. I checked the rights and its 0777.
The folder I tried to create is in: http://www.mysite/uploads/images/
so after folder creation it should look like: http://www.mysite/uploads/images/1
Anyone who can help me?
if (file_exists($upload_dir) == false)
{
mkdir($upload_dir, 0777);
}

mkdir function returns 'No such file or directory' in case there are not all parent directories exists. Please refer to third mkdir argument if you need recursive creation
mkdir($upload_dir, 0777, true);

Instead of given url path http://www.mysite/uploads/images/1 you need to given relative path of folder like
$upload_dir="/var/www/html/your_folder";// path of your folder
mkdir($upload_dir, 0777);
mkdir only works on The directory path

Related

php mkdir will only work when creating a folder inside a 777 folder

I have a folder called 'folders'. I want to create a folder inside 'folders' called 'folder' with mkdir()
However the folder fails to create when 'folders' is set to 755. The only may I can get mkdir to work is making 'folders' 777. Is this typical or is there something else wrong? Shouldn't be able to do that on 755, isn't 777 a security risk?
A 777 permission on the directory means that everyone has access to read/write/execute (execute on a directory means that you can do a ls of the directory).
Try this one below if you fails to create folder inside the folder.
<?php
// Desired folder structure
$structure = './folders/folder/';
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
if (!mkdir($structure, 0755, true)) {
die('Failed to create folders...');
}
?>

PHP mkdir permissions - wrong path

I have an problem and hope you can give me a hint for that:
I have an XAMPP running under OSx.
My APP is in htdocs/app.
Inside /app there is a folder /scripts and there is my PHP File:
app/scripts/file.php
Inside the /app folder there is a second folder /stuff that have 777 permissions.
from the script in app/scripts/file.php I want to create a folder
in app/stuff
But I got permission denied when I try to create a folder like:
mkdir('../scripts/newfolder', 0777, true);
What do I have to do?
Note:
When I test like this:
mkdir('newfolder', 0777, true);
Then it will work, but the new folder is under the wrong location:
app/scripts/newfolder
Give directory path as follows inside file.php.
mkdir('../stuff/newfolder', 0777, true);
If I understand you correctly you want to make a folder in app/stuff from the app/scripts folder, but you are telling the computer to go back into the apps folder (..), then back into the scripts folder (../scripts/) and then to make the newfolder.
mkdir('../stuff/newfolder', 0777, true);
should work then.
Your trying to create a directory from a dynamic place. You should prefer to use DIR which is the absolute directory of the current file being processed.
so you would use
mkdir(__DIR__ . '/../stuff/newfolder', 0777, true);
assume this line from your question is correct
from the script in app/scripts/file.php I want to create a folder in
app/stuff

php mkdir via symlink warning

I want to make directory recursively by use of symlinks (softlinks) but I encouter with warning :
Warning: mkdir(): File exists in ...Path to php code... on line 21
The directory that i want to create is /vagrant/resources/page.
In the /var/www path I create a symlink named resources that links to /vagrant/resources directory and the php code is like below:
$directory = '/var/www/resources/page';
if(!file_exists($directory)){
mkdir($directory,0777,true);
}
The permissions to all directories inside /vagrant is set to 777.
Thanks.
As #arkascha mentioned your problem is not with symlinks but with the existence of the directory you are trying to make. which is a bit strange considering you have a fair condition around your mkdir command.
have try with is_dir() instead of file_exists()
I solved a similar problem with readlink. Also checking before if it is a link or not. After than the path can be used as expected.
$path = '/var/www/resources'
if (is_link($path)) {
$path = readlink($path);
}

How to create folder in parent directory?

My php script is located in /var/www/html/users/dev. I need to create a folder in /var/www/images/ - something like /var/www/images/test/test/ and store here some images.
But when i trying is with mkdir($file_dir, 0777); where $file_dir is /var/www/images/test/test/ i receive an error:
Warning: mkdir(): No such file or directory in /var/www/html/users/dev/classes/sites.class.php...
Because "/var/www/images/test" does not exists, so you can not mkdir("/var/www/images/test/test")
You can specify the "$recursive" to TRUE, and it will work, like this:
mkdir($file_dir, 0777, TRUE);
Try
mkdir($file_dir, 0777, true);
The third parameter ('recursive') allows you to specify a path of which all directories will be created. If you don't, only the last directory ('test') will be created, and the whole path before that must exist.
The PHP documentation is quite clear about that.
if it is Linux you have set permissions to your parent directory 1st.
sudo chmod -R 777 /path of ur directory.

php create directory

I am creating a directory and I am getting the error
Warning: chmod() [function.chmod]: No such file or directory in /home/www/public_html/console/pubs-add.php on line 104
The php file that is running the code is in www/console and the directory that I am trying to create is in www/images/gallery.
I have tried many variations of setting the path such as ../images/gallery or home/www but nothing seesm to work
define("PATH", "/www/images/gallery");
$dir = 'a1234';
$targetfilename = PATH . '/' . $dir;
if (!is_file($dir) && !is_dir($dir)) {
mkdir($dir); //create the directory
chmod($targetfilename, 0777); //make it writable
}
You mkdir command just uses $dir, which is just 'a1234' (in the current working directory). This will fail (and make the chmod fail too).
The solution: you probably want to prefix $dir with PATH...
Dear chmod() create some time problem. So i will suggest u that use this
mkdir("/path/to/my/dir", 0700);
if u want the created directory should be ready and wirtable then use this
mkdir("/path/to/my/dir", 0777);
The problem is that you cannot chmod a file that you haven't made. For that reason, I've changed the line
$task = chmod($targetfilename, 0777); //make it writable
to
$task = chmod($dir, 0755); //make folder writable
Tip: If you want a folder to be writable, chmod it to 755 and not 777. 777 is for files.
It makes the $dir in your current working directory, however, it doesn't mean that this equals your $targetfilename. I would say that you have to do mkdir($targetfilename) rather than mkdir($dir).

Categories