Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check if the folder already exists #144

Open
tranvanba1992 opened this issue Mar 28, 2019 · 3 comments
Open

Check if the folder already exists #144

tranvanba1992 opened this issue Mar 28, 2019 · 3 comments

Comments

@tranvanba1992
Copy link

How to check if the folder already exists. Thanks

@jonpaulh
Copy link

jonpaulh commented Jul 12, 2019

Just do a try catch and your script will add the folder if necessary

try
{
$dropbox->createFolder($path);
}
catch (Exception $e)
{

}

@wetmarble
Copy link

wetmarble commented Apr 2, 2020

I'm working in the symfony framework and can't do a simple try/catch, so I wrote a function to handle this:

`function doesItemExist( $path, $depth = 0 ){
// this isn't actually in my code, as I'm using a an object based approach, but you could define your dropbox object here if needed
$dropbox = new Dropbox($app);

$depth++;
if( !is_array( $path ) ){
	$path = explode( '/', $path );
}

$pathDepth = count( $path ) - 1; // the max depth of the exploded path
$currentPath = join( '/', array_slice( $path, 0, $depth ) ); // the joined path at the current depth

if( $depth <= $pathDepth ){
	// get the dropbox items
	$listFolder = $dropbox->listFolder( $currentPath, ['recursive' => false] );
	$items = $listFolder->getItems()->all();

	// loop through the items looking for the current path
	foreach( $items as $item ){
		// match case insensitive
		if(
			"$currentPath/$path[$depth]" === $item->getDataProperty( 'path_display' ) ||
			"$currentPath/$path[$depth]" === $item->getDataProperty( 'path_lower' )
	 	){
			// found our current depth item
			if( $depth === $pathDepth ){
				// if we are at the max depth then we are done
				return true;
				break;
			} else {
				// otherwise dive deeper
				return doesItemExist( $path, $depth );
				break;
			}
		}
	}

	// our item wasn't found
	return false;
}

}`

@wetmarble
Copy link

After doing A/B testing, dropbox's search function is faster than my function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants