codeigniter creating a directory - php

How do I create a directory using codeigniter? Basically what I am trying to do is allow user to upload files and then create a directory on the fly for storing these file could anyone point me on how could I create a directory using codeigniter
Thanks
All efforts will be appreciated

I have run the following codes in windows which works perfect for me-
<?php
$path = "uploads/product";
if(!is_dir($path)) //create the folder if it's not already exists
{
mkdir($path,0755,TRUE);
}
?>
Here 0755 is permission of the folder to be created. 755 means you can do anything with the file or directory, and other users can read and execute it but not alter it. Suitable for programs and directories you want to make publicly available.

Use mkdir
mkdir("/path/to/my/dir");

Related

Create a folder in the /root directory

I've tried everything online but I couldn't find a real solution, I have a php file that in local on my computer works perfectly, instead when I load it on my seerver it doesn't create the folder.
the php contains this string to create a new folder:
shell_exec("mkdir /root/users/$username");
I guess that maybe it's a problem of permission, I can just create folders in the var/www folder, dI tried a lot of different way to set my permission but I can't figure it out.
You should use mkdir to create folder or chmod to change permissions functions.
for example:
mkdir('newFolder', 775);
or you want to create on some path. Currently you are in /home/username/public_html/project/
mkdir('subproject', 775); // it will create in `project` folder.
if you want to create with some root like you asked in your question.
mkdir('/root/users/'.$username, 775);
I am sure it will be fix your issue and also a standard way to create.

Create folder outside public_html to upload files

I creating script and I need to let users upload files (images) . and for security reasons I want to make file folder outside public_html.
But my problem is maybe my potential customers may have different folders structure in their servers.
So my question is : There is a way in php to go back one folder before public_html ?
Another possible case if my client put script file in directory like that public_html/demo/ , so in that case I need to go 2 folders back.
I try to use $_SERVER["DOCUMENT_ROOT"] but it give the public server directory.
any help or suggestion will be helpfull. thank you
If the php user has the permission to do so, you should be able to include/write files to a directory using a relative path for example:
include("../test.php");
Likewise if you had a directory which was writable by the php user you should be able to create files in that directory also using a relative path:
$ cat test1.php
<?php
$file = fopen("../test.php", "w");
fwrite($file, "Hello World. Testing!\n");
fclose($file);
include("../test.php");
$ php test1.php
Hello World. Testing!

How to create a folder in a network different from root using php

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.

mkdir(folder_name) with 755 permission in php

I have my web application hosted in /var/www folder. I am creating a folder from one of the PHP scripts of the web application. The default permission of the created folder is drwx------, i.e. 700. But I want that folder to have at least 755 permission.
Up to now I tried: mkdir($path, 0755) and chmod($path, 0755) PHP functions but without any success.
Does anybody know how to solve my problem please?
Millions of thanks beforehand.
Have you tried changing the umask ?
Have a look here: http://nl3.php.net/manual/en/function.umask.php
The easiest way it to:
$oldmask = umask(0);
chmod($path, 0755);
umask($oldmask)
Since you have default permission of 700, which means the parent directory (the directory in which you are trying to create the folder) do not have rw permission for group owner or other users. Most often the running demon(httpd) is not the owner of the parent folder and hence cannot modify the directory.
In simple terms, the php script do not have access to modify or add new directory. You need to change the permission of the parent folder to at least drwxrw-rw- (or 0755).
Use ssh, cpanel or ftp client to do this. If you do it using php script you will end with the same problem again, as parent of parent will have again 0700. ;)

php mkdir() chmod and permissions

i was using this basic script:
$folderPath = "../path/to/$folder/";
mkdir("$folderPath");
i create this directory and then upload photos to it. I've been doing this for a good 4-5 months now and suddenly i start getting 'FORBIDDEN' errors when I attempt to view the contents of the folder via web browser
The directory is being created the same and the photos are still uploading without a problem, but I cannot access the photos
I tried rewriting the script and using chmod to change the permissions but I'm having no luck at all
All the older folders were being created with: -w- rwx r-x r-x
and I can't get this recreated
I've tried adding a chmod line into my script:
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
but I can't recreate the same permissions, I'm trying to understand how chmod works, but I can't figure out how to get this very basic function working properly again
Try looking out for a HTAccess file, where the "Options -Indexes" option will be mentioned, as this is mostly used for not showing the contents of a folder in a web browser. The file needs to be searched in the following manner:-
In the folder "root_folder/sales/inventory/$folder/", where "$folder" is as mentioned in your code.
If not found, try in the folder "root_folder/sales/inventory/".
If not found, try in the folder "root_folder/sales/".
If not found, try in the folder "root_folder/".
When you get the code of "Options -Indexes" written in the HTAccess file, you can remove / comment that line of code from there, or you can also write another HTAccess file in your required folder of "$folder", where the code will be "Options Indexes".
Also in the PHP page, the logic must be like this:-
<?php
$folderPath = "../sales/inventory/$folder/";
mkdir("$folderPath");
chmod("$folderPath", 0755);
// Use of "copy()" / "move_uploaded_file()" function here, using some "$targetFile" variable.
chmod($targetFile, 0755);
?>
This will help you when you will be unlinking / deleting the uploaded files from the "$folder" folder.
Hope it helps.
If your $folder variable includes some sub-directories your parent directories are maybe not being chmoded to the right permissions. This was the problem I was having on a hired OVH Gentoo server.
Imagine that $folder = '/store1/ally23/shelf42'; so your final directory structure is
../sales/inventory/store1/ally23/shelf42, and you want 0777 permisions.
You do:
mkdir($folderPath, 0777, true) || chmod($folderPath, 0777);
Only the final directory shelf42 is chmoded to 0777. The intermediary directories are created with default permissions (in my case 0744).
There is no recursive option in PHP's chmod command, so you have to loop over the intermediary directories and chmod them individually.
If you're in a shared environment, you may also want to chown after upload, to be on the safe side. Especially if you're running your web server under a user other than your virtual host has permission to access (EG: "nobody" vs "mysite".) This is common with cPanel servers, FWIW.
Simply umask means the default permissions for new files/directories:
<?php
umask(022);
?>
This sets the default permissions for user, groups, and others respectively:
0 - read, write and execute
1 - read and write
2 - read and execute
3 - read only
4 - write and execute
5 - write only
6 - execute only
7 - no permissions

Categories