public function uploadFile($file) { $target_dir = "data/"; $filename = md5(time()); $i = 1; $cpt = md5($i); $ext = strtolower(substr(strrchr($file['name'], '.'), 1)); $target_file = $target_dir . $filename . "_" . $cpt . "." . $ext; // Si le fichier existe while (file_exists(Page::path($target_file))) { $i++; $cpt = md5($i); $target_file = $target_dir . $filename . "_" . $cpt . "." . $ext; } if (move_uploaded_file($file["tmp_name"], $target_file)) { $f = new FileObject(); $f->permission = "access content"; $f->module = ""; $f->path = "/" . $target_file; $f->content_type = $file['type']; $f->save(); return $f->id_file; } else { return false; } }
/** * Append target file's content to current file * * @param string $file_name Path of target file * @return void **/ function append($file_name) { $target = new FileObject($file_name, "r"); while (!$target->feof()) { $readstr = $target->read(); $this->write($readstr); } $target->close(); }
/** * Destructor */ public function __destruct() { parent::__destruct(); if (file_exists($this->filename)) { unlink($this->filename); } }
function getMimeType() { if (!empty($this->data['meta_info']['mime'])) { return $this->data['meta_info']['mime']; } return parent::getMimeType(); }
function __construct($meta = array(), array $options = array()) { if (!is_array($meta) && is_numeric($meta)) { $meta = array('id' => $meta); } $meta['table'] = 'documents'; $meta['name'] = 'Document'; $meta['fields'] = array('id' => 'primarykey', 'name' => 'string', 'active' => 'boolean', 'modified' => 'lastmodified', 'added' => 'dateadded'); return parent::__construct($meta, $options); }
public static function page_upload_content() { $theme = new Theme(); if (isset($_FILES['file'])) { $file = new FileObject(); if ($file->uploadFile($_FILES['file'])) { Notification::statusNotify(t("Le fichier a bien été uploadé."), Notification::STATUS_SUCCESS); } else { Notification::statusNotify(t("Une erreur s'est produite lors de l'upload."), Notification::STATUS_ERROR); } } $f = new Form("POST", Page::url("/file/upload")); $f->setAttribute("enctype", "multipart/form-data"); $t = new InputElement("file", t("Fichier : "), "", "file"); $f->addElement($t); $t = new InputElement("submit-file", "", t("Charger"), "submit"); $f->addElement($t); $formulaire = $theme->forming($f); $theme->set_title("Charger un fichier"); $theme->add_to_body($formulaire); $theme->process_theme(Theme::STRUCT_ADMIN); return; }
function testInsert() { $file = new FileObject(); $file->setDescription($description = 'some description'); $file->setMediaFileId($media_file_id1 = 'dsada'); $file->setName($name1 = 'original'); $file->setEtag($etag1 = 'dsajadhk'); $file->setMimeType($mime_type1 = 'jpeg'); $file->setFileName($file_name1 = 'some file'); $this->mapper->expectOnce('_doParentInsert', array($file)); $this->mapper->insert($file); $this->db->sqlSelect('media'); $media_rows = $this->db->getArray(); $this->assertEqual(1, sizeof($media_rows)); $media1 = reset($media_rows); $this->assertEqual($media1['id'], $file->getMediaId()); $this->assertEqual($media1['media_file_id'], $media_file_id1); $this->assertEqual($media1['file_name'], $file_name1); $this->assertEqual($media1['mime_type'], $mime_type1); $this->assertEqual($media1['size'], $size1); $this->assertEqual($media1['etag'], $etag1); }
/** * Make an integer to be stored in a file * @param int $n */ public function __construct($n = 0) { parent::__construct(4); $this->set($n); }
public static function page_set_avatar() { $theme = new Theme(); if (isset($_FILES['file'])) { $file = new FileObject(); if ($id_file = $file->uploadFile($_FILES['file'])) { $u = User::get_user_logged(); $u->set_avatar($id_file); header("location: " . Page::url("/profile")); return; } else { Notification::statusNotify(t("Une erreur s'est produite lors de l'upload."), Notification::STATUS_ERROR); } } $f = new Form("POST", Page::url("/profile/settings/avatar")); $f->setAttribute("enctype", "multipart/form-data"); $t = new InputElement("file", t("Fichier : "), "", "file"); $f->addElement($t); $t = new InputElement("submit-file", "", t("Charger"), "submit"); $f->addElement($t); $formulaire = $theme->forming($f); $theme->set_title("Changer mon avatar"); $theme->add_to_body($formulaire); $theme->process_theme(Theme::STRUCT_ADMIN); return; }
function __construct(&$string) { $this->file = tempnam($_ENV['storageFolder'], 'stringFile.'); file_put_contents($this->file, $string); parent::__construct($this->file); }
/** * 上传一张图片并保存为缩略图(丢弃原图) * @param string $fileField 表单名称 * @param string $savePath 保存的路径,存放从[upload][rootPath]目录开始的文件夹 * @param array $thumbArray 要生成的缩略图的信息,使用二维数组:[{'set':'big_image','width':135,'height':246}, ...] * @return array($stateInfo, $f) 返回一个成功信息和一个详细的内容(素材文件) */ public function upImage($fileField, $savePath = null, $thumbArray) { $this->changeConfig($savePath); // 检查文件是否可以上传 $info = isset($_FILES[$fileField]) ? $_FILES[$fileField] : null; $ifChecked = $this->checkFile($info); $stateInfo = $this->getStateInfo($ifChecked); if ($ifChecked === 0) { // 读取文件,开始生成压缩图 import("GDImageDeal"); $filename = $info["tmp_name"]; if (is_readable($filename)) { $gdd = GDImageDeal::readImageByFilename($filename); foreach ($thumbArray as $value) { $targetImage = $gdd->crop($value['width'], $value['height']); $filePath = PathGeneration::getFolderAppendDateAndValue($this->config['savePath'], $value['set']); $fileName = FileObject::fileNameByTime($info['ext']); $targetImage->saveImage($filePath . $fileName); //$targetImage->saveImage("cache/0.tmp"); //list($1,$2,$3)=FileObject::autoCreateByCopy($this->config['savePath'], $info['ext'], $info['tmp_name']); // 纪录成功信息 $tf['width'] = $value['width']; $tf['height'] = $value['height']; $tf['savePath'] = $filePath; $tf['filename'] = $fileName; $tf['url'] = getConfig("site", "root") . $filePath . $fileName; $info[] = $tf; } } } unset($info['tmp_name']); return array($stateInfo, $info); }