Skip to content

Instantly share code, notes, and snippets.

@sword-jin
Last active September 7, 2015 11:03
Show Gist options
  • Save sword-jin/998b125216e0852e5539 to your computer and use it in GitHub Desktop.
Save sword-jin/998b125216e0852e5539 to your computer and use it in GitHub Desktop.
SPL File and directory
$dir = new DirectoryIterator('common/images');
foreach($dir as $key => $file) {
if ($file->isFile()) {
// echo $key . ': ' . $file->getFilename() . '<br / >';
$files[] = clone $file; // 必须colne不然无法存入数组
}
}
echo $files[1]->getFilename();;

DirectoryIterator

  • include dot files
  • numbered keys
  • Path not included in value
  • no configuration options
  • array requires cloned objects

FilesystemIterator

  • Dot files skipped
  • pathname used for key
  • Path included key
  • configurable
  • clone not necessary

File and Directory permissions

  • isDir() determines if the file is a directory
  • isFile() determines if the file is a file
  • isReadable()
  • isWritable()
  • isExecutable()

Filename and path

  • getFilename()
  • getPath()
  • getPathname()
  • getRealPath()
  • getPathInfo()

File statistics and type

  • getSize()
  • getATime()
  • getMTime()
  • getExtension()
  • getType() return file,dir,link

Access file Content

  • openFile() create a File Object
<?php
$rates = [
['currency' => 'Australia $', 'rate' => 0.93630],
['currency' => 'Canada $', 'rate' => 0.92060],
['currency' => 'Euro', 'rate' => 1.35630],
['currency' => 'Hong Kong $', 'rate' => 0.12900],
['currency' => 'Japan yen', 'rate' => 0.00980],
['currency' => 'Swiss franc', 'rate' => 1.11300],
['currency' => 'UK sterling', 'rate' => 1.69700]
];
if (isset($_POST['download'])) {
$titles = array_keys($rates[0]);
$file = new SplTempFileObject();
$file->fputcsv($titles);
foreach ($rates as $rate) {
$file->fputcsv($rate);
}
$file->rewind();
header('Content-Type: text/csv');
header('Content-Disposition: attachment;filename=rates.csv');
$file->fpassthru();
exit;
}
<?php
$files = new FilesystemIterator('common/images', FilesystemIterator::UNIX_PATHS);
foreach ($files as $file) {
if ($file->getExtension() == 'jpg') {
echo $file->getFilename() . ' is ' . $file->getSize() . ' bytes. ' . ' Its absolute path is: ' . $file->getRealpath() . '<br />';
}
}
<?php
$csvfile = new SplFileObject('common/data/cars_tab.csv');
$csvfile->setFlags(SplFileObject::READ_CSV);
$csvfile->setCsvControl("\t");
foreach ($csvfile as $line) {
$cars[] = $line;
}
echo '<pre>';
print_r($cars);
echo '</pre>';
<?php
$docs = new FilesystemIterator('common/documents', FilesystemIterator::UNIX_PATHS);
foreach ($docs as $doc) {
if ($doc->getExtension() == 'txt') {
$textfile = $doc->openFile('r+');
$textfile->setFlags(SplFileObject::SKIP_EMPTY |
SplFileObject::READ_AHEAD |
SplFileObject::DROP_NEW_LINE);
echo '<h2>' . $textfile->getFilename() . '</h2>';
foreach ($textfile as $line) {
echo "$line<br />";
}
$textfile->seek(3);
echo '<br />';
echo 'This is the fourth line: ' . $textfile;
while (! $textfile->eof()) {
$textfile->next();
}
$textfile->fwrite("\r\n\r\nThis line was added");
}
}
<?php
//$dir = new FilesystemIterator('common/images', FilesystemIterator::UNIX_PATHS);
$dir = new FilesystemIterator('common/images');
$dir->setFlags(FilesystemIterator::UNIX_PATHS | FilesystemIterator::KEY_AS_FILENAME);
foreach ($dir as $key => $file) {
// echo $key . '>> ' . $file . '<br />';
$files[] = $file;
}
var_dump($files[2]->getFilename());
<?php
$files = new RecursiveDirectoryIterator('common');
$files->setFlags(FilesystemIterator::SKIP_DOTS | FilesystemIterator::UNIX_PATHS);
$files = new RecursiveIteratorIterator($files, RecursiveIteratorIterator::SELF_FIRST);
$files->setMaxDepth(1);
foreach ($files as $file) {
echo $file . '<br />';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment