List Dropbox folder content in a website - php snippet

This is a quick and dirty PHP script, to embed the contents of a publicly accessible Dropbox folder as a list of files within your website. Use at your own risk!

/*
To use this script,call it with the following command:    
  $dropboxURL = 'SpecifyApubliclyAccessibleDropboxUrl';
  print get_data($dropboxURL);    
*/
 
function get_data($url) {  
  // Grab the dropbox page 
  $ch = curl_init();
  $timeout = 10;
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);   
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  $result = curl_exec($ch);
  curl_close($ch);  
 
  // Parse the links out of the HTML
  $string = "";       
  $regex='|<a.*?href="(.*?)"|';
  preg_match_all($regex,$result,$parts);
  $links=array_reverse($parts[1]); 
  $uniqelinks = array(); 
  // For some reason the are duplicate links so lets make then unique
  foreach($links as $link) $uniqelinks[$link] = $link; 
  // Loop through the links and build and HTML list
  foreach($uniqelinks as $link){   
      $char = strlen($link);
  	  $pref = $link[$char-4].$link[$char-3].$link[$char-2].$link[$char-1];
  	  $class = str_replace('.','',$pref);   
  	  $title = explode('/', $link);  
  	  $title[6] = str_replace('%20', '&nbsp;', $title[6] ); 
  	  if($pref=='.pdf' || $pref=='.jpg' || $pref=='.gif' || $pref=='.zip'){
  		  $string .= "<div class=\"file\"><a href=\"$link\" class=\"icon $class\">".$title[6]."</a></div>"; 
  		}
  }
  return $string;
}

Comments

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>. Beside the tag style "<foo>" it is also possible to use "[foo]". PHP source code can also be enclosed in <?php ... ?> or <% ... %>.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <h2> <h4> <img> <div> <table> <tr> <td> <pre>
  • Lines and paragraphs break automatically.
  • Insert image: [img:node_id align=(left|right) size=(|fullLA|fullPreview|fullSQ|halfLA|halfPO|halfSQ|thirdSQ) action=(popup|link|frame) url=url style="margin:10px;" title=text description=text]

More information about formatting options

 
info@theanthillsocial.co.uk XHTML 1.0 Strict CSS2 WAI