/** * Test, if we can create a file. */ function testCreateAndDelete() { // create a temporary file and just delete it again: $temp_path = $this->createUserFile(); if (!$temp_path) { return; } unlink($temp_path); $temp_name = basename($temp_path); $File = new File('user', 1, $temp_name); $this->assertFalse($File->exists(), 'File does not exist.'); $File->create(); $this->assertTrue($File->exists(), 'File exists after create().'); $this->assertTrue(file_exists($temp_path), 'File really exists.'); if (file_exists($temp_path)) { $File->unlink(); $this->assertFalse($File->exists(), 'File thinks it is unlinked.'); $this->assertFalse(file_exists($temp_path), 'File is really unlinked.'); } }
/** * 内部调用删除文件 * */ public function action_unlink() { # 目录 $dir = $this->arguments[0]; if (!isset(\File::$dir[$dir])) { # 目录不允许操作 static::show_error('目录不允许操作'); } if (!$this->arguments[1]) { static::show_error('缺少参数'); } # 文件 $file = \File::$dir[$dir] . $this->arguments[1]; if (\File::unlink($file)) { static::show_message('success', null, 1); } else { # 记录错误日志 \Core::log('delete file(' . $file . ') error.', 'error'); static::show_error('执行失败'); } }
/** * 清除配置缓存 * * @return boolean */ public function clear_cache($type = '') { if (!$this->is_use_cache) { // 非普通文件写入,不缓存,无需清理 return true; } $tmp_file = array(); $projects = array_keys((array) Core::config('core.projects')); if ($projects) { foreach ($projects as $project) { # 所有项目的配置文件 $tmp_file[] = $this->get_config_cache_file($project, $type); } } if (!$tmp_file) { return true; } $rs = File::unlink($tmp_file); if (IS_DEBUG) { Core::debug()->log('clear extends config cache ' . ($rs ? 'success' : 'fail') . '.'); } return $rs; }
/** * Files */ function files() { dibi::activate('old'); $res = dibi::query(' SELECT [id_picture] as [id], [basename] as [code], [mime] as [mimetype], [titulek] as [description], [popisek] as [name] FROM [picture] ')->fetchAll(); //dump($res); dibi::activate('new'); dibi::query('DELETE FROM [file] WHERE id < 3000'); //dibi::query('TRUNCATE TABLE [file]'); try { foreach ($res as $n => $r) { $ext = getExtensionForMime($r['mimetype']); $filepath = FILESTORAGE_DIR . "/imported/" . $r['code'] . "-orig." . $ext; if (!file_exists($filepath)) { $filepath = FILESTORAGE_DIR . "/imported/" . $r['code'] . "." . $ext; } echo $filepath . "<br>"; if (file_exists($filepath)) { $values = array('id' => $r['id'], 'name' => $r['code'] . "." . $ext, 'type' => $r['mimetype'], 'size' => filesize($filepath), 'tmp_name' => $filepath, 'error' => UPLOAD_ERR_OK); // dump($values); $file = new \Nette\Http\FileUpload($values); $ff = new \File(); //$ff->simulation = TRUE; $ff->force = TRUE; $ff->created = filemtime($filepath); $ff->changed = filectime($filepath); $ff->id = $r['id']; $ff->name = $r['name']; $ff->description = $r['description']; $ff->filename = $r['code']; try { $ff->insertUploaded($file, FILESTORAGE_DIR); $ff->save(); echo ' - File is uploaded and saved.<br/>'; } catch (\DibiException $e) { echo get_class($e) . ': ' . $e->getMessage() . "<br><br>"; if ($e->getCode() !== 0) { $ff->unlink(FILESTORAGE_DIR); } } catch (\Exception $e) { echo get_class($e) . ': ' . $e->getMessage() . "<br><br>"; } } else { echo " - Not found!<br/><br/>"; } } echo "Files OK.<br/><br/>"; } catch (DibiException $e) { echo get_class($e) . ': ' . $e->getMessage(); } }
public function saveFile(\Nette\Application\UI\Form $form) { $ff = new \File(); try { $ff->file_prefix = ""; $ff->file_sufix = "_u" . $this->user->id . "_" . substr(sha1(time()), 0, 4); $ff->user_id = $this->user->id; $ff->insertUploaded($form->getValues()->file, FILESTORAGE_DIR); $ff->save(); $this->flashMessage('File is uploaded and saved.', 'ok'); $this->redirect('edit', array('id' => $ff->id)); } catch (\DibiException $e) { $this->flashMessage($e->getMessage()); if ($e->getCode() !== 0) { $ff->unlink(FILESTORAGE_DIR); } } catch (\Exception $e) { $this->flashMessage($e->getMessage()); } }
/** * 删除指定key的缓存,若$key===true则表示删除全部 * * @param string $key */ public function delete($key) { if (true === $key) { # 删除全部 return File::remove_dir($this->dir); } if (is_array($key)) { # 支持多取 $data = array(); $i = 0; foreach ($key as $k => $v) { if ($this->delete((string) $v)) { $i++; } } return $i == count($key) ? true : false; } $filename = $this->get_filename_by_key($key); if (!file_exists($filename)) { return true; } return File::unlink($filename); }
/** * 删除指定key的缓存,若$key===true则表示删除全部 * * @param string $key */ public function delete($key) { if ($this->is_file_write_disalbed) { return false; } if (true === $key) { # 删除全部 return File::remove_dir($this->dir . $this->prefix, $this->storage); } if (is_array($key)) { # 支持多取 $data = array(); $i = 0; foreach ($key as $k => $v) { if ($this->delete((string) $v)) { $i++; } } return $i == count($key) ? true : false; } $filename = $this->get_filename_by_key($key); return File::unlink($filename, $this->storage); }
/** * 清除配置缓存 */ public function clear_cache() { $projects = array_keys((array) Core::config('core.projects')); if ($projects) { foreach ($projects as $project) { # 所有项目的配置文件 $tmpfile[] = DIR_DATA . $project . '/extends_config.txt'; } } File::unlink($tmpfile); }
switch ($this->owner) { case 'list': echo '<h1>All uploaded files</h1>'; $list = File::getList(); $dt = new YuiDatatable(); $dt->addColumn('id', '#', 'link', 'a/files/details/', 'name'); $dt->addColumn('time_uploaded', 'Uploaded'); $dt->addColumn('uploader', 'Uploader', 'link', 'u/profile/'); $dt->addColumn('type', 'Type'); $dt->addColumn('size', 'Size'); $dt->addColumn('mimetype', 'Mime'); $dt->setDataSource($list); echo $dt->render(); break; case 'delete': if (confirmed('Are you sure you want to permanently delete this file?')) { File::unlink($this->child); js_redirect('a/files/list'); } break; case 'details': // child = file id $view = new ViewModel('views/user/file_details.php'); $view->registerVar('owner', $this->child); echo $view->render(); echo '<br/>'; echo '» ' . ahref('a/files/delete/' . $this->child, 'Permanently delete file') . '<br/>'; break; default: echo 'No handler for view ' . $this->owner; }
/** * 内部调用删除文件 * */ public function action_unlink() { # 目录 $dirs = (array) $this->arguments[0]; # 文件 $file_names = (array) $this->arguments[1]; if (count($dirs) !== count($file_names)) { $this->show_error('参数错误'); } $files = array(); foreach ($dirs as $k => $dir) { if (!isset(File::$dir[$dir])) { # 目录不允许操作 $this->show_error('目录不允许操作'); } $files[] = File::$dir[$dir] . $file_names[$k]; } if (File::unlink($files)) { $this->show_success(); } else { # 记录错误日志 Core::log('system.error.file.delete', array('file' => $files), LOG_ERR); $this->show_error('执行失败'); } }
/** * 清除配置缓存 * * @return boolean */ public function clear_cache() { if (!$this->is_use_cache) { // 非普通文件写入,不缓存,无需清理 return true; } $projects = array_keys((array) Core::config('core.projects')); if ($projects) { foreach ($projects as $project) { # 所有项目的配置文件 $tmpfile[] = DIR_DATA . 'extends_config' . $project . '.txt'; } } $rs = File::unlink($tmpfile); if (IS_DEBUG) { Core::debug()->log('clear extends config cache ' . ($rs ? 'success' : 'fail') . '.'); } return $rs; }
public function movingToAnotherFolder() { $this->writeData($this->file, null); $target = new File($this->folder, $this->file->getFilename()); $this->file->move($this->folder); $exists = $target->exists(); $target->unlink(); $this->assertTrue($exists); }
/** * Tests File::unlink * * @expectedException \phpbu\App\Exception */ public function testUninkFail() { $spl = $this->getDeletableFileInfo(); $file = new File($spl); // delete file so next unlink fails unlink($spl->getPathname()); $file->unlink(); $this->assertFalse(true, 'exception should be thrown'); }
public function callingMain() { $temp = System::tempDir(); // Create web.ini in system's temp dir $ini = new File($temp, 'web.ini'); $ini->open(FILE_MODE_WRITE); $ini->write("[app]\n" . "mappings=\"/:welcome\"\n" . "[app::welcome]\n" . "class=undefined\n" . "[app::welcome@dev]\n" . "class=\"" . self::$welcomeScriptlet->getName() . "\"\n"); $ini->close(); // Run ob_start(); xp新criptlet愛unner::main(array($temp, $temp, 'dev', '/')); $content = ob_get_contents(); ob_end_clean(); $ini->unlink(); // Assert $this->assertEquals('<h1>Welcome, we are open</h1>', $content); }