Jump to content
xisto Community
lnljamat

Mkdir() Not Working In Php Help with php function mkdir()

Recommended Posts

Hi,

Ive got a freehosting site at qupis.com,
and ive made a php site on my computer (running apache with php -installed using xampp)
in this site the user has the option to create directories and files
it was working fine on my local computer, but now that ive put it online on qupis, the mkdir() funciton doesnt work, i.e. it doesnt create a directory.
this is the actual code :

$folderpath = "../../content/"."$_POST[section_name]";mkdir($folderpath);
do u need to have some parameters with mkdir? or is it switched off in phpinfo(), so that u cant make a directory directly from the server?
any help on this would be greatly appreciated!!

Share this post


Link to post
Share on other sites

I have not tested your code but ../../ is redundant. ../ goes to your root directory (public_html) by default. You're calling it twice.

And I'm not sure if you should combine POST with your variable assignment. Again, I have not tested out.

What I do know is

$section_name = $_POST['section_name'];$folderpath = "../content/$section_name";mkdir($foltherpath);
Should work because I use it that way in my script.

Share this post


Link to post
Share on other sites

I have not tested your code but ../../ is redundant. ../ goes to your root directory (public_html) by default. You're calling it twice.

 

And I'm not sure if you should combine POST with your variable assignment. Again, I have not tested out.

 

What I do know is

 

$section_name = $_POST['section_name']; $folderpath = "../content/$section_name"; mkdir($foltherpath);
Should work because I use it that way in my script.

Thanks for ur reply

 

however, the directory where im executing this function is at an even lower level, and doesn't "../" take u up one level?

anyways i get this error:

Warning: mkdir() [function.mkdir]: Permission denied in /home/lnljamat/public_html/admin/pagemanagement/sectionedit2.php on line 86

and when i try to do it with ur code i get an error that no such file or directory exists

 

Thanks

Share this post


Link to post
Share on other sites

Hi!The error message clearly indicates that it is a problem with the permissions on the directory. If you use an FTP client to set permissions on your files and directories, you might want to try going to the directory where you want to create the folder and assign permissions using the CHMOD command or the equivalent of a graphical tool.If you do not have an FTP interface, the file manager web interface that your hosting provider presents you with for uploading files should have some way of setting permissions.If all else fails, you ought to contact your hosting provider - their support staff should be experienced in dealing with such problems on a day-to-day basis as it is fairly common.

Share this post


Link to post
Share on other sites

The permission you want to CHMOD the directory to is 0777 (i.e. give full rights to everyone). The reason why it worked on your local computer is probably because you are running Windows (as someone from a Unix background shouldn't of had this problem).

Share this post


Link to post
Share on other sites

Hi!
The error message clearly indicates that it is a problem with the permissions on the directory. If you use an FTP client to set permissions on your files and directories, you might want to try going to the directory where you want to create the folder and assign permissions using the CHMOD command or the equivalent of a graphical tool.

If you do not have an FTP interface, the file manager web interface that your hosting provider presents you with for uploading files should have some way of setting permissions.

If all else fails, you ought to contact your hosting provider - their support staff should be experienced in dealing with such problems on a day-to-day basis as it is fairly common.


Thank you Very much!!!

that was the problem, i needed to change the permissions and then was able to upload!!
thanks again-

now im facing another problem- when i try to delete files from ftp it says that permission is not granted, so i have to make a php script unlink() to delete files from the server- could u please explain why that happens? i thought when i log in with ftp i am the super user, and through php script that should be the normal user?

Thanks again

Share this post


Link to post
Share on other sites

now im facing another problem- when i try to delete files from ftp it says that permission is not granted, so i have to make a php script unlink() to delete files from the server- could u please explain why that happens? i thought when i log in with ftp i am the super user, and through php script that should be the normal user?

When you create a file or directory through FTP or through a control panel like cPanel, the owner (or perhaps group) of the files or directories are the same as the owner running the FTP server and the control panel, so they are both able to modify the files freely. When a user runs a script remotely that does similar tasks, the owner of the script being executed is different than that of the FTP server or control panel. Under an operating system such as Linux, unless other users are given permission to modify these files, then only the same owner as those files can do anything with them.

Share this post


Link to post
Share on other sites

When playing with folders and files using PHP, it's best to create files and directories with PHP, in that way you won't have problems with permissions, but I remember times, when you need to move a backup to another server or something like that, usually it's done with an FTP client and when using PHP you get the permissions error and would need to chmod to 777 almost everything..When using MySQL, usually you have less problems, just because you're using less of such functions which require permissions, still even if using PHP, you usually need to have permissions in that folder you'll run install.php so it's always a problem, I remember I used to have a script which would recreate the structure to PHP, it made a copy of files, and created it using PHP, later I just removed the original data using ftp and renamed the folder and had all the files with owner group being PHP.By the way, to fix the mistake above and make it clear:/ - root directory./ - current directory../ - parent directory../../ - two parent directories../../../ - three parent directoriesand so on.. :angel:

Share this post


Link to post
Share on other sites

When you create a file or directory through FTP or through a control panel like cPanel, the owner (or perhaps group) of the files or directories are the same as the owner running the FTP server and the control panel, so they are both able to modify the files freely. When a user runs a script remotely that does similar tasks, the owner of the script being executed is different than that of the FTP server or control panel. Under an operating system such as Linux, unless other users are given permission to modify these files, then only the same owner as those files can do anything with them.

I tried to change the permissions from the cpanel web based ftp program, and it doesnt allow me to change the permissions, so i cant delete any folders or files.
Is there anyway to create permission when i create files and folders from php?

Share this post


Link to post
Share on other sites

I tried to change the permissions from the cpanel web based ftp program, and it doesnt allow me to change the permissions, so i cant delete any folders or files.Is there anyway to create permission when i create files and folders from php?

Permissions deal with three things: read, write and execute. This does not mean you can delete the files. However, if you chmod a folder to 0777, you should be able to create and delete things in that folder from PHP freely—but so will everyone else. You can only change permissions to a file or directory from PHP if and only if the directory these files or directories are stored in allows you to.

Share this post


Link to post
Share on other sites

This is common on directories created with PHP. The default permissions will only allow the user that PHP is running in full control. You should make it a habit to CHMOD() (PHP has a built in function specifically for this http://de2.php.net/chmod/ every time you create a folder or upload a file. Keep in mind, unless you CHMOD 0777 your site's ROOT directory, it's still very difficult for other users on the system to access your files. You should also try your best NOT to use '../../' when locating files. I find it good practice to use $_SERVER['DOCUMENT_ROOT'] whenever possible. I have a variables.php I include with my script, in that I have the path to my script in relation to the document root, so I can call files as such mkdir( $_SERVER['DOCUMENT_ROOT'] . $_script_path . $foldername, 0777 )

Share this post


Link to post
Share on other sites

Sorry for the double post, but this had to be saidNEVER, EVER, EVER, EVER take user input and use it in your script without cleaning it.$_POST['whatever'] can contain whatever the user wants it to, and using it without stripslashing (in this case) is just begging to have someone abuse it

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.