Working with Files

CodeIgniter provides a File class that wraps the SplFileInfo classand provides some additional convenience methods. This class is the base class for uploaded filesand images.

Getting a File instance

You create a new File instance by passing in the path to the file in the constructor.By default, the file does not need to exist. However, you can pass an additional argument of “true”to check that the file exists and throw FileNotFoundException() if it does not.

  1. $file = new \CodeIgniter\Files\File($path);

Taking Advantage of Spl

Once you have an instance, you have the full power of the SplFileInfo class at the ready, including:

  1. // Get the file's basename
  2. echo $file->getBasename();
  3. // Get last modified time
  4. echo $file->getMTime();
  5. // Get the true real path
  6. echo $file->getRealPath();
  7. // Get the file permissions
  8. echo $file->getPerms();
  9.  
  10. // Write CSV rows to it.
  11. if ($file->isWritable())
  12. {
  13. $csv = $file->openFile('w');
  14.  
  15. foreach ($rows as $row)
  16. {
  17. $csv->fputcsv($row);
  18. }
  19. }

New Features

In addition to all of the methods in the SplFileInfo class, you get some new tools.

getRandomName()

You can generate a cryptographically secure random filename, with the current timestamp prepended, with the getRandomName()method. This is especially useful to rename files when moving it so that the filename is unguessable:

  1. // Generates something like: 1465965676_385e33f741.jpg
  2. $newName = $file->getRandomName();

getSize()

Returns the size of the uploaded file in bytes. You can pass in either ‘kb’ or ‘mb’ as the first parameter to getthe results in kilobytes or megabytes, respectively:

  1. $bytes = $file->getSize(); // 256901
  2. $kilobytes = $file->getSize('kb'); // 250.880
  3. $megabytes = $file->getSize('mb'); // 0.245

getSizeByUnit()

Returns the size of the uploaded file default in bytes. You can pass in either ‘kb’ or ‘mb’ as the first parameter to getthe results in kilobytes or megabytes, respectively:

  1. $bytes = $file->getSizeByUnit(); // 256901
  2. $kilobytes = $file->getSizeByUnit('kb'); // 250.880
  3. $megabytes = $file->getSizeByUnit('mb'); // 0.245

getMimeType()

Retrieve the media type (mime type) of the file. Uses methods that are considered as secure as possible when determiningthe type of file:

  1. $type = $file->getMimeType();
  2.  
  3. echo $type; // image/png

guessExtension()

Attempts to determine the file extension based on the trusted getMimeType() method. If the mime type is unknown,will return null. This is often a more trusted source than simply using the extension provided by the filename. Usesthe values in app/Config/Mimes.php to determine extension:

  1. // Returns 'jpg' (WITHOUT the period)
  2. $ext = $file->guessExtension();

Moving Files

Each file can be moved to its new location with the aptly named move() method. This takes the directory to movethe file to as the first parameter:

  1. $file->move(WRITEPATH.'uploads');

By default, the original filename was used. You can specify a new filename by passing it as the second parameter:

  1. $newName = $file->getRandomName();
  2. $file->move(WRITEPATH.'uploads', $newName);

The move() method returns a new File instance that for the relocated file, so you must capture the result if theresulting location is needed:

  1. $file = $file->move(WRITEPATH.'uploads');