Page 1 of 1

User Defined Directory

Posted: Mon Feb 21, 2011 8:10 am
by noodlesouper
Hi
I am developing an app for my final year project that will collate movie data relating to movies stored in a directory on a users machine.
I was wondering if there was a way server side or otherwise to allow a user to select a relevant directory without having to manually enter the absolute path.
I posted on a forum yesterday and was told that this wasn't possible but I thought it had to be.
Any pointers would be great.
thanks in advance

Re: User Defined Directory

Posted: Mon Feb 21, 2011 3:36 pm
by crazycoders
It could be done. The paradigm of client/server code is not that distant when you use Java applets or Flash. I know flash can upload files and thus browse files on the user's computer, but a directory selector, i don't think that already exists, you'd have to code it yourself.

Note that it wouldnt be possible for you to read the data on the user's hard drive using PHP. That is not possible because your code runs at the other end of the web on another server. BUT, you could use a java applet to build a list of files in a user selected directory and then send those file names to php for processing. Any information that you would need to transfer to php should be coded into the applet and then sent to PHP server using some form of communication such as XML. Then, PHP analyses this information, stores it in a database for later processing.

Re: User Defined Directory

Posted: Tue Feb 22, 2011 1:36 am
by noodlesouper
Thanks crazycoders for your time.
I have solved the issue of reading the contents of the directory using Glob as shown below. The function takes in a var $path

Code: Select all

$dir = "C:\\xampp\htdocs\sampleMovieFolder";//this should be user defined
$title=get_dirs2($dir);
and does the necessary computations to return the movie title. Its getting the base path of the root movie folder that I am having issue with. I would like to be able to do it in a way that didn't result in it being hard coded or manually entered by a user.

Code: Select all

function get_dirs2($path)
{
	
	// get the length of the users' movie directory path
	$path_len = strlen($path);
	// add trailing slash if missing
    if(substr($path, -1) != "'\'") $path .= "\\";
	
	//read titles of subdirectories to an array called $dirs
	$dirs = glob($path.'*', GLOB_ONLYDIR); 
	
	//loop through array getting length of new array ($dir)
	//extract the directory name from the path 
	foreach($dirs as $dir)
	{ 
		$dir_len = strlen($dir);
		//add 1 to path_len to account for the appended trailing backslash
		//get the difference of path length and dir_len
		$title_len = ($path_len+1)-$dir_len;
		//convert from neg to pos number
		$title_len = abs($title_len);
		//get starting position of title within original string
		$start = $dir_len-$title_len;
		//using substr extract title name and store 
		$titles[] = substr($dir , $start ,$title_len);
		//echo "\n<br/>$titles<br />\n";
		
	} 
	return $titles;
I'm new to PHP so I hope this make sense.
regards noodle