Jump to content

Recommended Posts

Hello,

 

I am trying to create a function that creates a directory tree given a path. The function needs to determine the parent directory that currently exists (such as /public_html/images/) in the given path and then create the subdirectories below it (such as /public_html/images/bears/beats/battle_star_galactica/).

 

function ftp_newdir($dir)
{
	require ''.$_SERVER['DOCUMENT_ROOT'].'/includes/config.php';

	$dirs_desc = array();
	$this_dir = $dir;
	$preamble = "/home/danstewa";

	if(!$conn_id = ftp_connect($ftp_server))
	{
		$errors[] = "Could not connect to ".$ftp_server;
	}

	if(!$login_result = ftp_login($conn_id, $ftp_user, $ftp_password))
	{
		$errors[] = "Could not connection to ".$ftp_server." with the supplied user name and password.";
	}

	while(!file_exists($preamble.$this_dir))
	{
		$dirs_desc[] = $this_dir;
		$this_dir = dirname($this_dir);
	}

	$dirs_asc = array_reverse($dirs_desc);

	ftp_site($conn_id, 'CHMOD 0777 '.dirname($dirs_asc[0]));

	for($i = 0; $dirs_asc; $i++)
	{
		mkdir($dirs_asc[$i]);
		ftp_site($conn_id, 'CHMOD 0777 '.$dirs_asc[$i]);
	}
}

 

The trouble is with the following snippet of code:

 

while(!file_exists($preamble.$this_dir))
	{
		$dirs_desc[] = $this_dir;
		$this_dir = dirname($this_dir);
	}

 

My goal with this snippet was to populate an array with a list of sub-dirs that needed to be created, but apparently it's an infinite loop.

 

NOTE: I'm on a shared server, so I can't use mkdir since it would require that I use chmod to change perms before directory creation; chmod isn't permitted on the shared server that I am on. That's why I'm using FTP chmod'ing.

 

Any help is appreciated!

 

- Jason

Link to comment
https://forums.phpfreaks.com/topic/67622-solved-create-directory-tree/
Share on other sites

SOLVED. Used the following code to create directory tree using php ftp functions because chmod function was restricted:

 

function ftp_newdir($dir)
{
	require ''.$_SERVER['DOCUMENT_ROOT'].'/includes/config.php';

	$dir_not_found = TRUE;
	$dirs_desc = array();
	$this_dir = $dir;
	$preamble = "/home/danstewa";

	if(!$conn_id = ftp_connect($ftp_server))
	{
		$errors[] = "Could not connect to ".$ftp_server;
	}

	if(!$login_result = ftp_login($conn_id, $ftp_user, $ftp_password))
	{
		$errors[] = "Could not connection to ".$ftp_server." with the supplied user name and password.";
	}

	while($dir_not_found)
	{
		if(!file_exists($preamble.$this_dir))
		{
			$dirs_desc[] = $this_dir;
			$this_dir = dirname($this_dir);
		}
		else
		{
			$dirs_desc[] = $this_dir;
			$dir_not_found = FALSE;
		}
	}

	$dirs_asc = array_reverse($dirs_desc);

	for($i = 0; count($dirs_asc) > $i; $i++)
	{
		if(!file_exists($preamble.$dirs_asc[$i]))
		{
			ftp_mkdir($conn_id, $dirs_asc[$i]);
			ftp_site($conn_id, 'CHMOD 0777 '.$dirs_asc[$i]);
		}
	}

	for($i = 0; count($dirs_asc) > $i; $i++)
	{
		if(file_exists($preamble.$dirs_asc[$i]))
		{
			ftp_site($conn_id, 'CHMOD 0755 '.$dirs_asc[$i]);
		}
	}
}

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.