public static function createFromFile($filename) { // get MIME type switch ($mimeType = File::getMIMEType($filename)) { case 'text/plain': case 'text/csv': return new static(fopen($filename, 'r')); case 'application/vnd.ms-office': throw new Exception('Excel import not yet supported'); default: throw new Exception('Unsupported spreadsheet mime-type: ' . $mimeType); } return $mimeType; }
public static function saveRecordData(&$record, $data, $sha1 = null) { // save file $filePath = static::getRealPathByID($record['ID']); file_put_contents($filePath, $data); // update in-memory record $record['SHA1'] = $sha1 ? $sha1 : sha1_file($filePath); $record['Size'] = filesize($filePath); $record['Type'] = File::getMIMEType($filePath); $record['Status'] = 'Normal'; $record['Timestamp'] = date('Y-m-d H:i:s'); // override MIME type by extension $extension = strtolower(substr(strrchr($record['Handle'], '.'), 1)); if ($extension && array_key_exists($extension, static::$extensionMIMETypes)) { $record['Type'] = static::$extensionMIMETypes[$extension]; } // write record to database DB::nonQuery('UPDATE `%s` SET SHA1 = "%s", Size = %u, Type = "%s", Timestamp = "%s", Status = "Normal" WHERE ID = %u', array(static::$tableName, $record['SHA1'], $record['Size'], $record['Type'], $record['Timestamp'], $record['ID'])); // invalidate cache Cache::delete(static::getCacheKey($record['CollectionID'], $record['Handle'])); }
public static function saveRecordData($record, $data) { if (defined('DEBUG')) { print "saveRecordData({$record['ID']})\n"; } // save file $filePath = static::getRealPathByID($record['ID']); file_put_contents($filePath, $data); // get mime type $mimeType = File::getMIMEType($filePath); // override MIME type by extension $extension = strtolower(substr(strrchr($record['Handle'], '.'), 1)); if ($extension && array_key_exists($extension, static::$extensionMIMETypes)) { $mimeType = static::$extensionMIMETypes[$extension]; } // calculate hash and update size DB::nonQuery('UPDATE `%s` SET SHA1 = "%s", Size = %u, Type = "%s", Status = "Normal" WHERE ID = %u', array(static::$tableName, sha1_file($filePath), filesize($filePath), $mimeType, $record['ID'])); }
public static function createFromFile($filename, $options = array()) { return static::createFromStream(fopen($filename, 'r'), File::getMIMEType($filename), $options); }