/** Imports a CSV file as an array. Tries to recover from errors and return * as much as possible. * @param string $fileName - The file to try and import * @return array of arrays (rows) - or else an error */ function importCsv($fileName) { if (!$fileName || !is_string($fileName)) { return false; } $csvMimeTypes = ['text/plain', 'text/csv', 'text/x-csv', 'application/csv', 'text/comma-separated-values', 'application/x-csv']; $fmt = getFileMimeType($fileName); if (!$fmt || !in_array($fmt, $csvMimeTypes, 1)) { throw new \Exception("Importinc CSV file: [{$fileName}], reported MimeType: [{$fmt}]"); } $handle = fopen($fileName, "r"); if ($handle === false) { return false; } $retarr = []; while (($data = fgetcsv($handle)) !== FALSE) { if (!is_array($data) || sizeOf($data) === 1 && $data[0] === null) { continue; } $retarr[] = $data; } return $retarr; }
/** * Save sources to php file. * * @param string $file * @param string $target * @access public * @return bool */ public function save2php($file, $target) { $contentType = getFileMimeType($file); if (!$contentType) { return false; } $content = var_export(file_get_contents($file), true); $phpCodes = <<<EOT <?php header('Content-type: {$contentType}'); echo {$content}; exit; EOT; return file_put_contents($target, $phpCodes); }
// Files to be ignored $ignoredFiles = array(".DS_Store", ".localized"); $total_files = 0; $total_filesize = 0; // scandir function automatically sort the array if ($files = scandir($path)) { foreach ($files as $file) { if (is_file($path . $file) && !in_array($file, $ignoredFiles)) { ?> <tr style='width: 100%'> <td style='vertical-align: middle; width: 65%;' class='text-left'> <a class="trigger" href="<?php echo "functions/sendFile.php?file=" . $path . urlencode($file); ?> "><?php echo getFileMimeType($path . $file) . " " . $file; ?> </a></td> <td style='vertical-align: middle; width: 20%;' class='text-right'><?php echo date("M d Y H:i", filemtime($path . $file)); ?> </td> <td style='vertical-align: middle; width: 10%;' class='text-right'><?php echo getFileSize($path . $file); ?> </td> </tr> <?php $total_files++; $total_filesize += filesize($path . $file); }
if (!array_key_exists('path', $_REQUEST)) { header('401 bad request'); die('bad request'); } $path = CMS_FOLDER . '/' . $_REQUEST['path']; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (count($_FILES) <= 0) { header('401 bad request'); die('bad request'); } $upload = array_shift($_FILES); if (array_key_exists('error', $upload) && $upload['error'] != 0) { header('500 Internal server error'); die('500 Internal server error'); } $source_file = $upload['tmp_name']; if (!is_readable($source_file)) { header('500 Internal server error'); die('500 Internal server error'); } move_uploaded_file($source_file, $path); } else { if (!is_readable($path)) { header('404 Not Found'); die('404 Not Found'); } $m = getFileMimeType($path); header('Content-type: ' . $m); $content = file_get_contents($path); die($content); }
function getFiles() { $files = array(); foreach (glob(CMS_FOLDER . '/*') as $filename) { if (is_dir($filename)) { continue; } $f = array(); $f['name'] = basename($filename); $f['mimetype'] = getFileMimeType($filename); $files[$f['name']] = $f; } return $files; }
/** Returns the mime type if a valid image file, else false * Primitive ; improve some day * @param string $filePath - a string/path for the file * @return false || string - mime-type */ function isValidImagePath($filePath) { if (!$filePath || !is_string($filePath) || !file_exists($filePath)) return false; $mimeType = getFileMimeType($filePath); if (!$mimeType || !is_string($mimeType)) return false; $mimeArr = explode('/', $mimeType); //pkdebug("mimeType: [$mimeType], mimeArr:", $mimeArr); if (!$mimeArr || !is_array($mimeArr) || !(count($mimeArr) === 2) || !($mimeArr[0] === 'image')) return false; //pkdebug("Returninb: $mimeType"); return $mimeType; }