php create directory - php

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).

Related

mkdir fails in every way

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

Create a File and set Directory Permissions

I'm using this PHP code below to create dynamic xml files.
...
$file_name = "";
$rss_feed_dir = $_SERVER['DOCUMENT_ROOT'] . '/xml/';
chmod($rss_feed_dir, 0777);
$file = $rss_feed_dir . $file_name . '.xml';
$file_handle = fopen($file, "w");
fwrite($file_handle, $xml);
fclose($file_handle);
The file will be created if the directory permissions are set 0777, and seems to fail at 0755. I've read on many sites that world execute permissions can pose a security risk.
Should I chmod back to 0755 at the end of this script?
Is there a better way to set the directory permissions?
I suspect it is because whichever directory your rss_feed_dir points to, is a directory owned by another user - other than the user that is being used to run your script.
So 755 permissions would exclude you from write access to the directory if your script is not running with the specified group or user. Which explains why you cannot do a write file command successfully when permissions were 755, but successful when 777.
If this php file is on a server and being called from the web, try modifying your directory ownership itself:
chown -R YOUR_USER:www-data YOUR_RSS_DIRECTORY_HERE
And then chmod to 775
Hope that helps!

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 with 777 permission in Windows

I have read this:-
Why can't PHP create a directory with 777 permissions?
and I can see a new folder being created by applying the following:-
// Desired folder structure
$structure = "../../../".$flash_dir."HELLO";
// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.
$oldmask = umask(0);
mkdir($structure, 0777);
umask($oldmask);
when viewing the file permission of HELLO with DreamWeaver, it is 777. However, I suspect it is a Linux 0777 rather than a Windows 777, therefore I still cannot upload things to HELLO.
Will there be any alternative method to create a directory with windows 777? Thanks!
PS. when I manual create a new directory and right click it to set 777, it works perfectly, so I really think it's related to Linux vs Windows~
0777 is exactly the same thing as 777
But I still can't say what the problem is. I would try to chmod it again after you've created it.
$oldmask = umask(0);
chmod($structure, 0777);
umask($oldmask);

Permission denied on mkdir()

I'm getting the following error when trying to call mkdir() on a server...
Warning: mkdir() [function.mkdir]:
Permission denied in
/home/server/public_html/wp-content/themes/mytheme/catimages/cat-images.php
on line 373
The function is below. Its attempting to create a folder under the site's "wp-content/uploads folder". I've verified that the PHP Version is 5.2.15 and that the files inside the theme folder are writable, but that does not necessarily means the uploads folder is writable I suppose.
How can I find out if the uploads folder is writable?
protected function category_images_base_dir()
{
// Where should the dir be? Get the base WP uploads dir
$wp_upload_dir = wp_upload_dir();
$base_dir = $wp_upload_dir[ 'basedir' ];
// Append our subdir
$dir = $base_dir . '/cat-images';
// Does the dir exist? (If not, then make it)
if ( ! file_exists( $dir ) ) {
mkdir( $dir ); //THIS IS LINE 373
}
// Now return it
return $dir;
}
is_writable() is probably the function you're looking for.
http://cz.php.net/manual/en/function.is-writable.php says:
Returns TRUE if the filename exists and is writable. The filename argument may be a directory name allowing you to check if a directory is writable.
Also, the directly next line is relevant here:
Keep in mind that PHP may be accessing the file as the user id that the web server runs as (often 'nobody').
In other words, check if your directory is writable by the user id of the web server - this may be a different user id than yours! Set the appropriate permissions - e.g. set the usergroup of the folder to that of the server's user, and grant read, write, and execute to group. (chgrp somegroup uploads; chmod g+r uploads; chmod g+w uploads; chmod g+x uploads)
Make sure the parent folder is writable to the process that the web server runs as.
Edit: Oops, premature reply. Does your host give you a GUI file browser thingy?

Categories