My company

Ads by Google

File Browser in CodeIgniter

This is a quick post. I’m going to show the code necessary to build a file browser in PHP using CodeIgniter. It allows file browsing in the server and opening files without leaving your app. Text files are displayed directly while other files are send to download.

Introduction

During a PHP project development cycle, we usually work locally, easily accessing our files. When we upload our work to an external server we need a way to verify our uploads, changes made by the app itself or check if an specific file has the correct version. Common ways of doing this are FTP or a file explorer like cPanels. But they need external tools. Other servers like PagodaBox, which is a Git server, have files updated through a ‘git push’.

To speed things up you can use the code presented in this post to check filed directly in the browser. There’s an obvious risk associated, so you need to guarantee the only the correct users access this functionality. This security layer is on your own. I’m not going to show any user control in this post.

There are other ways to integrate similar solutions in your application, but this is a simple working solution that can be improved according to your needs.

The Method

Considering that you have a controller only to administrative access, the following method is able to access your files.

This code only work when the controller is inside /controllers. If you put it in a sub-folder you need to change the segments section. As it’s a simple logic, I think the comments are enough to explain it. Let’s got to the view.