Php create directory index

How to create directory in php?

so now images should be upload into means i want to first create directory with name into folder and then into directory images will upload. My current code that’s not creating directory into car_images folder.

How to create directory in php?

my current folder path is $path = car_images; Its working for me. i can upload my images into this folder directly. But i want to create separate folders for each user. so now images should be upload into $path = car_images/$emp_code;

means i want to first create directory with name $emp_code into car_images folder and then into $emp_code directory images will upload.

My current code that’s not creating directory into car_Images folder.

 $path = "car_images/".$emp_code; if (!is_dir($path.'/'.$emp_code)) < mkdir($path.'/'.$emp_code, 0777, true); >$name = $_FILES['car_images']['name'][$i]; $size = $_FILES['car_images']['size'][$i]; list($txt, $ext) = explode(".", $name); $file= time().substr(str_replace(" ", "_", $txt), 0); $info = pathinfo($file); $filename = $file.".".$ext; if(move_uploaded_file($_FILES['car_images']['tmp_name'][$i], $path.$filename))
This is what you want : Folder A | |--Folder_001---All files uploaded/has to be sent here by 001(user 1) |--Folder_002---All files uploaded/has to be sent here by 002(user 2) |--Folder_003---All files uploaded/has to be sent here by 003(user 3) |--Folder_004---All files uploaded/has to be sent here by 004(user 4) |-- |-- |--so on. 

Inside directory: folder_A(which you already be having), New folders will be generated according to your need.(example: user1_folder, user2_folder..)

For example : when user uploads files, folders can be created according to user_id, Later when user uploads files, files go into:

folder_A —> User_id_folder —> User_id uploaded files will/can be present/sent here.

You Can change the below code according to your need, So below I have answered for the asked question.

Here, $name and $tmp_name required ‘Later’ for the function move_uploaded_file($parameter1,$parameter2).

if(isset($_FILES) & !empty($_FILES)) < $name = $_FILES['file']['name']; /* $size = $_FILES['file']['size']; $type = $_FILES['file']['type']; */ $tmp_name = $_FILES['file']['tmp_name']; >

To set it, I have assumed it as $_SESSION. But you can modify it according to your code like $_POST or $_GET. file_exists() checks whether file/dir is present or not.

if (isset($_SESSION['emp_code'])) < $emp_code= $_SESSION['emp_code']; if (!file_exists('car_images/'.$emp_code)) < mkdir('car_images/'.$emp_code, 0777, true); >$location = 'car_images/'.$emp_code.'/'; > 

Now the directory as $emp_code will be in the folder $car_images, then you move the file to $emp_code directory.

 if(isset($name) & !empty($name)) < if(move_uploaded_file($tmp_name, $location.$name))< // do the code here. // In the sql query use it as '$location$name' /* FOR EXAMPLE: $sql = "UPDATE `table_name` SET `image`='$location$name' WHERE `email`='$email'"; */ >else < echo "failed to upload"; >> 
  • Now you have car_images folder:
  • In that folder, you have all emp_codes generated folders,
  • In that folder, you have the files uploaded by them (emp_codes).

Later if this works, then you can modify code to limited IF statements. Hope it helped. 🙂

Php how to create file in specific directory, Teams. Q&A for work. Connect and share knowledge within a single location that is structured and easy to search. Learn more

Читайте также:  Условный оператор вложенный условный оператор питон

How to make directory within directory by php loop?

How to make directory within directory by php loop?
Example: http://site_name/a/b/c/d
First create a then b within a then c within b then .
Problem is here a,b,c,d all the folders created in root directory not one within one. Here is my code —

else < $path_init = 1; >$paths = explode("/", $details1['path']); for ($i = $path_init; $i < count($paths); $i++) < $new_dir = ''; $base_url = $base_url . $paths[$i] . "/"; $new_dir = $base_url; if (FALSE === ($new_dir = folder_exist($paths[$i]))) < umask(0777); mkdir($new_dir . $paths[$i], 0777, TRUE); >> function folder_exist($folder) < // Get canonicalized absolute pathname $path = realpath($folder); // If it exist, check if it's a directory return ($path !== false AND is_dir($path)) ? $path : false; >?> 

please check this code. it will create nested folder if not exit

You can actually create nested folders with the mkdir PHP function

mkdir($path, 0777, true); // the true value here = recursively 

Dear friends the following answer is tested and used in my script —

Create nested directory in PHP, Find centralized, trusted content and collaborate around the technologies you use most. Learn more

PHP create multiple directories

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

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

All the intermediary directories have to already exist. You can trigger this behaviour by using the optional third argument:

in normal cPanel the folder permissions should be 0755 so the command in this case will be:

mkdir('tst/tst2/tst3', 0755, true); 

Php create directory, 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 …

Why I can’t create directory with php mkdir()?

When I create new directory with mkdir($path, 0755); I have error:

Warning: mkdir() [function.mkdir]: Permission denied in /home/u235555603/public_html/vacancies/index.php on line 51

In directory «vacancies» I have 755 rights.

As others have said, make sure your directory is writable by the user your webserver runs under. For apache under *nix, you can do:

ps aux | egrep 'apache|http|nginx' 

And look for the username(s) in the first column.

As for creating a directory in PHP safely, check the result of is_writable($parentDir) before attempting to create the directory. See is_writable.

Create a Folder if It Doesn’t Exist in PHP, mkdir () in PHP. It is possible to create a folder and set the proper permission using PHP, specifically using mkdir () function. The default permission mode is 0777 …

Читайте также:  Python convert column to float

Источник

mkdir

Attempts to create the directory specified by directory .

Parameters

A URL can be used as a filename with this function if the fopen wrappers have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

The permissions are 0777 by default, which means the widest possible access. For more information on permissions, read the details on the chmod() page.

Note:

permissions is ignored on Windows.

Note that you probably want to specify the permissions as an octal number, which means it should have a leading zero. The permissions is also modified by the current umask, which you can change using umask() .

If true , then any parent directories to the directory specified will also be created, with the same permissions.

Return Values

Returns true on success or false on failure.

Note:

If the directory to be created already exists, that is considered an error and false will still be returned. Use is_dir() or file_exists() to check if the directory already exists before trying to create it.

Errors/Exceptions

Emits an E_WARNING level error if the directory already exists.

Emits an E_WARNING level error if the relevant permissions prevent creating the directory.

Examples

Example #1 mkdir() example

Example #2 mkdir() using the recursive parameter

// Desired directory structure
$structure = ‘./depth1/depth2/depth3/’ ;

// To create the nested structure, the $recursive parameter
// to mkdir() must be specified.

if (! mkdir ( $structure , 0777 , true )) die( ‘Failed to create directories. ‘ );
>

See Also

  • is_dir() — Tells whether the filename is a directory
  • rmdir() — Removes directory
  • umask() — Changes the current umask

User Contributed Notes 5 notes

When using the recursive parameter bear in mind that if you’re using chmod() after mkdir() to set the mode without it being modified by the value of uchar() you need to call chmod() on all created directories. ie:

mkdir ( ‘/test1/test2’ , 0777 , true );
chmod ( ‘/test1/test2’ , 0777 );
?>

May result in «/test1/test2» having a mode of 0777 but «/test1» still having a mode of 0755 from the mkdir() call. You’d need to do:

mkdir ( ‘/test1/test2’ , 0777 , true );
chmod ( ‘/test1’ , 0777 );
chmod ( ‘/test1/test2’ , 0777 );
?>

This is an annotation from Stig Bakken:

The mode on your directory is affected by your current umask. It will end
up having ( and (not )). If you want to create one
that is publicly readable, do something like this:

$oldumask = umask ( 0 );
mkdir ( ‘mydir’ , 0777 ); // or even 01777 so you get the sticky bit set
umask ( $oldumask );
?>

mkdir, file rw, permission related notes for Fedora 3////
If you are using Fedora 3 and are facing permission problems, better check if SElinux is enabled on ur system. It add an additional layer of security and as a result PHP cant write to the folder eventhough it has 777 permissions. It took me almost a week to deal with this!

Читайте также:  Document

If you are not sure google for SElinux or ‘disabling SELinux’ and it may be the cure! Best of luck!

Remember to use clearstatcache()

. when working with filesystem functions.

Otherwise, as an example, you can get an error creating a folder (using mkdir) just after deleting it (using rmdir).

When creating a file using mkdir() the default root will be the DocumentRoot (in XAMPP) itself.

If you use mkdir(«myfile») in something.php, instead of creating the folder in includes, php will create it in the project folder

Источник

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Chansig / DirectoryIndex Public archive

Directory Index — The simplest PHP directory Index

License

Chansig/DirectoryIndex

This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?

Sign In Required

Please sign in to use Codespaces.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching GitHub Desktop

If nothing happens, download GitHub Desktop and try again.

Launching Xcode

If nothing happens, download Xcode and try again.

Launching Visual Studio Code

Your codespace will open once ready.

There was a problem preparing your codespace, please try again.

Latest commit

Git stats

Files

Failed to load latest commit information.

README.md

Directory Index — The simplest PHP directory Index

Directory Index is a simple PHP script that lists the contents of any web-accessable directory and allows navigating there within. Simply upload index.php file to any directory and get immediate access to all files and sub-direcories under that directory. Directory Index is written in PHP 5.3+ and distributed under the MIT License.

  • Extremely simple installation
  • Creates on-the-fly listing of any web-accessable directory
  • Custimizable sort order of files/folders
  • Easily define hidden files or file type to be excluded from the listing

Directory Index requires PHP 5.3+ to work properly. For more information on PHP, please visit http://www.php.net.

  • Upload src/index.php to the folder you want listed. That’s all!
  • For more settings:
    • copy src/index.php or execute src/generate.php with the destination in argument and rename index.php.dist in index.php. ex:
      php src/generate.php /var/www/mysite
    • Edit the new index.php and change values in Setting class
    • Upload index.php to the folder you want listed.

    Ensure you have the latest version of Directory Index installed.

    Verify that you have PHP 5.3 or later installed. You can verify your PHP version by running:

    Find a problem or bug with Directory Index? Open an issue on GitHub.

    Directory Index is distributed under the terms of the MIT License.

    About

    Directory Index — The simplest PHP directory Index

    Источник

Оцените статью