Classe per gestire file in PHP
Oggi vorrei postare una mia classe che mi sono creato per gestire i file e le direcory con PHP e la SPL.
Della SPL (Standard PHP Library) ne ho già parlato alcune volte.
Questa classe non vuole essere una classe definitva o perfetta; anzi, io stesso la aggiorno e modifico abbastanza spesso.
Suggerimenti e migliorie sono ovviamente ben accetti.
Per il resto usate questa classe come volete, sperando possa essere utile:
<?php
/**
* @author Matteo Ferrone
* @since 2015-07-17
*/
class GestioneFile {
/**
* Create file
*
* @param string $file File with path
* @param string $message Text of the file
* @param string $ext Extension of the file
* @param string $mode Mode for creation
* @return void
*/
public function createFile($file, $message, $ext, $mode = 'w') {
$f = fopen($file . $ext, $mode);
fwrite($f, $message);
fclose($f);
}
/**
* Generic function for upload
*
* @param string $dirUpload Directory to upload
* @param string $fileTmpName Temp file name
* @param string $fileName File name
* @param string $fileSize File size
* @param string $fileType File type
* @param string $typeArray Extensions allowed
* @param string $maxSize Max size for uploading file
* @param string $nome Final name
* @return string Result of the operation
*/
public function uploadFile($dirUpload, $fileTmpName, $fileName, $fileSize, $fileType, $typeArray, $maxSize, $nome) {
$fileSizeMB = $fileSize / 1024;
if (is_uploaded_file($fileTmpName)) {
if (!in_array($fileType, $typeArray)) {
return '<h3 class="error">Il file non è tra quelli ammessi</h3>';
} elseif ($fileSizeMB > $maxSize) {
return '<h3 class="error">Il file è troppo grande</h3>';
} else {
$ext = pathinfo($fileName, PATHINFO_EXTENSION);
if (move_uploaded_file($fileTmpName, $dirUpload . $nome . '.' . $ext)) {
return '<h3>File caricato ' . $dirUpload . $nome . '</h3>';
} else {
return '<h3 class="error">Impossibile caricare il file ' . $fileName . '</h3>';
}
}
} else {
return '<h3 class="error">Si è verficato un errore o non è stato inviato nessun file</h3>';
}
}
/**
* Open a file
*
* @param string $file File with path
* @return array File content
*/
public function openFile($file) {
$f = new SplFileInfo($file);
$result = array();
if ($f->isFile()) {
$openFile = $f->openFile('r');
while (!$openFile->eof()) {
array_push($result, strtoupper($openFile->fgets()));
}
} else {
array_push($result, 'Il file ' . $f->getBasename() . ' non esiste!');
}
sort($result);
return $result;
}
/**
* Open a file without format the content
*
* @param string $file File with path
* @return string Content of the file or error
*/
public function openFileNoExplode($file) {
$f = new SplFileInfo($file);
$result = '';
if ($f->isFile()) {
$openFile = $f->openFile('r');
$result = $openFile->fgets();
} else {
$result = '<p class="error">Il file ' . $f->getBasename() . ' non esiste</p>';
}
return $result;
}
/**
* Delete a file
*
* @param string $file File with path
* @return string Reuslt of operation
*/
public function deleteFile($file) {
$f = new SplFileInfo($file);
if ($f->isFile()) {
if (unlink($f)) {
return '<h3>File cancellato</h3>';
}
}
}
/**
* Get all files in a directory
*
* @param string $directory Directory to get files
* @return array Array of files with attributes
*/
public function getFiles($directory) {
if (is_dir($directory)) {
$iterator = new DirectoryIterator($directory);
$fileArray = array();
foreach ($iterator as $dir) {
if ($dir->isFile()) {
$fileArray[] = array(
'nome' => $dir->getFilename(),
'data' => $dir->getMTime(),
'size' => $dir->getSize(),
'ext' => pathinfo($dir->getFilename(), PATHINFO_EXTENSION)
);
}
}
}
sort($fileArray);
return $fileArray;
}
/**
* Get all files in a directory recursively
*
* @param string $directory Directory to get files
* @return array Array of files with attributes
*/
public function getFilesRecursive($directory) {
$objects = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($directory), RecursiveIteratorIterator::SELF_FIRST);
$fileArray = array();
foreach ($objects as $name => $object) {
if (!$object->isDir() && $object->getFilename() !== '.' && $object->getFilename() !== '..') {
$fileArray[] = array(
'nome' => $object->getFilename(),
'data' => $object->getMTime(),
'size' => $object->getSize(),
'ext' => pathinfo($object->getFilename(), PATHINFO_EXTENSION),
'path' => $object->getPathname()
);
}
}
sort($fileArray);
return $fileArray;
}
/**
* Create a directory
*
* @param type $directory Full path
* @return void
*/
public function createDir($directory) {
$dir = new SplFileInfo($directory);
if (!$dir->isDir()) {
mkdir($dir);
}
}
/**
* Get extension of a file
*
* @param string $file File with path
* @return string Exension of ile
*/
public function getExtension($file) {
return pathinfo($file['extension']);
}
}
Ciao, al prossimo aggiornamento!
php spl
Commentami!