Jump to content

mkDir and a String =D


icez

Recommended Posts

Hey,

 

I'm currently making a little script which I often get a string like = "images/folder/something/maybeanotherone/file.ext"

 

so I'm interresting to know how could I create every directory in the correct order and the numbers of sub-directory isn't fix, so if anybody can give me an hint, I would appreciate =D

 

Btw, tell me if I'm you don't understand what I mean, English isn't my primary language.

Link to comment
Share on other sites

<?php
$path = "images/folder/something/maybeanotherone/file.ext";

$parts = explode('/',$path);

$last = count($parts) - 1; // the index starts at zero

$filename = $parts[$last]; // you can also use end($parts);

unset($parts[$last]); // remove last element (filename)

foreach($parts as $part){
if(!is_dir($part)){
	mkdir($part);
	echo "Made: $part<br />";
}
chdir($part); // make current working directory be the current part of the path
}
?>

Link to comment
Share on other sites

<?php
$path = "images/folder/something/maybeanotherone/file.ext";

$parts = explode('/',$path);

$last = count($parts) - 1; // the index starts at zero

$filename = $parts[$last]; // you can also use end($parts);

unset($parts[$last]); // remove last element (filename)

foreach($parts as $part){
if(!is_dir($part)){
	mkdir($part);
	echo "Made: $part<br />";
}
chdir($part); // make current working directory be the current part of the path
}
?>

 

It's not working, I tried a few things, and still not working, the problem is that the script doesn't create the sub-directory at the right place...

Link to comment
Share on other sites

	$parts = explode('/', $element->src);

	$last = count($parts) - 1;

	unset($parts[$last]);

	for($i = 0; $i < $last; $i++)
	{
		$download .= $parts[$i].'/';
		mkdir($download);
		echo 'Created:'.$download;
	}

 

it echo 'Created: (RIGHT PATH GO HERE)' and create every folder perfectly, but when I try to add something to the folder right after this loop, I get ...

Warning: file_put_contents(extract/1288399680/images/) [function.file-put-contents]: failed to open stream: No such file or directory in C:\wamp\www\themeforest\index.php on line 26

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

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.