本文整理汇总了PHP中Silex\Application::sendFile方法的典型用法代码示例。如果您正苦于以下问题:PHP Application::sendFile方法的具体用法?PHP Application::sendFile怎么用?PHP Application::sendFile使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Silex\Application
的用法示例。
在下文中一共展示了Application::sendFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendFileAction
public function sendFileAction(Request $request, Application $app)
{
$file = $request->query->get('file', '');
$locale = $request->getLocale();
$base_path = $request->getBasePath();
$base_path = $base_path . 'download/' . $locale;
$file_path = $base_path . '/' . $file;
$finder = new Finder();
$finder->files()->in($base_path)->name('*' . $file . '*');
$file = null;
foreach ($finder as $archivo) {
/** @var SplFileInfo $file */
$file = $archivo;
break;
}
if (null === $file) {
return $app->redirect('/');
}
$log = $app['monolog'];
$nombre = $file->getBasename('.' . $file->getExtension());
$nombre = $app['translator']->trans(sprintf("%s.%s", 'archivo', $nombre));
$nombre = $nombre . '.' . $file->getExtension();
$log->addInfo($nombre);
$log->addInfo(sprintf('Se ha solicitado el archivo: %s', $file_path));
return $app->sendFile($file)->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, $nombre);
}
示例2: boot
public function boot(Application $app)
{
$app['dispatcher']->addListener(KernelEvents::RESPONSE, [$this, 'onKernelResponse'], -1000);
$app->get($app['debug_bar.path'] . '/{path}', function ($path) use($app) {
return $app->sendFile($app['debug_bar']->getJavascriptRenderer()->getBasePath() . '/' . $path, 200, ['Content-Type' => 'text/css']);
})->assert('path', '.+');
}
示例3: register
public function register(Application $app)
{
$app['public-vendor.css'] = 'text/css';
$app['public-vendor.js'] = 'application/javascript';
$app['public-vendor.html'] = 'text/html';
$app['public-vendor.eot'] = 'application/vnd.ms-fontobject';
$app['public-vendor.svg'] = 'image/svg+xml';
$app['public-vendor.ttf'] = 'application/x-font-ttf';
$app['public-vendor.woff'] = 'application/font-woff';
$app['public-vendor.jpg'] = 'image/jpeg';
$app['public-vendor.png'] = 'image/png';
$app['public-vendor.jpeg'] = 'image/jpeg';
$app['public-vendor.gif'] = 'image/gif';
$app['public-vendor.ico'] = 'image/x-icon';
$app['public-vendor'] = $app->share(function () use($app) {
return new Container();
});
$app['public-vendor.response'] = $app->protect(function ($file) use($app) {
$name = 'public-vendor.' . strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (!file_exists($file) || !isset($app[$name])) {
$app->abort(404, "Not found");
}
return $app->sendFile($file, 200, array('Content-type' => $app[$name]));
});
}
示例4: getDevFile
public function getDevFile(Request $request, Application $app, $lang, $fileName)
{
global $rfExampleConfig;
if (!isset($rfExampleConfig['devStaticPaths'][$lang])) {
$app->abort(404, "Cannot find language files");
}
$filePath = $rfExampleConfig['devStaticPaths'][$lang] . $fileName;
if (!file_exists($filePath)) {
$app->abort(404, "Cannot find file");
}
$arr = explode(".", $fileName);
$extension = array_pop($arr);
$mime = "text/plain";
if ($extension === "css") {
$mime = "text/css";
} else {
if ($extension === "js") {
$mime = "application/javascript";
} else {
if ($extension === "html") {
$mime = "text/html";
}
}
}
return $app->sendFile($filePath, 200, array('Content-Type' => $mime));
}
示例5: connect
public function connect(Application $app)
{
// creates a new controller based on the default route
$controllers = $app['controllers_factory'];
$names = [];
foreach ($this->faculty->professors as $professor) {
$names[] = $professor->name;
}
$fs = new FileSystem();
$fs->remove($this->faculty->baseCacheDestination);
$names = '(' . implode('|', $names) . ')';
$faculty = $this->faculty;
$controllers->get('/{processor}/{path}', function (Application $app, $processor, $path) use($faculty) {
$exten = [];
preg_match($faculty->extenstions, $path, $exten);
$exten = ltrim($exten[0], '.');
if (empty($exten)) {
return $app->abort(404);
}
$faculty->process($processor, $path);
$imagePath = $faculty->getLastProcessed()[0];
return $app->sendFile($imagePath, 200, array('Content-Type' => 'image/' . $exten));
})->assert('path', $this->faculty->extenstions);
return $controllers;
}
示例6: connect
public function connect(Application $app)
{
global $beforeTokenCheker;
$controllers = $app['controllers_factory'];
$self = $this;
// ToDo: Add token check
$controllers->get('/filedownloader', function (Request $request) use($app, $self) {
$fileID = $request->get('file');
$filePath = __DIR__ . '/../../../' . FileController::$fileDirName . "/" . basename($fileID);
$app['logger']->addDebug($filePath);
if (file_exists($filePath)) {
$response = new Response();
$lastModified = new \DateTime();
$file = new \SplFileInfo($filePath);
$lastModified = new \DateTime();
$lastModified->setTimestamp($file->getMTime());
$response->setLastModified($lastModified);
if ($response->isNotModified($request)) {
$response->prepare($request)->send();
return $response;
}
$response = $app->sendFile($filePath);
$currentDate = new \DateTime(null, new \DateTimeZone('UTC'));
$response->setDate($currentDate)->prepare($request)->send();
return $response;
} else {
return $self->returnErrorResponse("file doesn't exists.");
}
});
//})->before($app['beforeTokenChecker']);
// ToDo: Add token check
$controllers->post('/fileuploader', function (Request $request) use($app, $self) {
$file = $request->files->get(FileController::$paramName);
$fineName = \Spika\Utils::randString(20, 20) . time();
if (!is_writable(__DIR__ . '/../../../' . FileController::$fileDirName)) {
return $self->returnErrorResponse(FileController::$fileDirName . " dir is not writable.");
}
$file->move(__DIR__ . '/../../../' . FileController::$fileDirName, $fineName);
return $fineName;
})->before($app['beforeApiGeneral']);
//})->before($app['beforeTokenChecker']);
return $controllers;
}
示例7: download
public function download(Application $app)
{
$r = new Response();
$util = new Utility();
$_path = $_POST['path'];
$c = $app['FileManager'];
$c['ext'] = array_merge($c['ext_img'], $c['ext_file'], $c['ext_misc'], $c['ext_video'], $c['ext_music']);
// include 'include/mime_type_lib.php';
if (strpos($_path, '/') === 0 || strpos($_path, '../') !== false || strpos($_path, './') === 0) {
return $r->create('wrong path', 400);
}
if (strpos($_POST['name'], '/') !== false) {
return $r->create('wrong path', 400);
}
$path = $c['current_path'] . $_path;
$name = $_POST['name'];
$info = pathinfo($name);
if (!in_array($util->fix_strtolower($info['extension']), $c['ext'])) {
return $r->create('wrong extension', 400);
}
if (!file_exists($path . $name)) {
return $r->create('File not found', 404);
}
return $app->sendFile($path . $name)->setContentDisposition(\Symfony\Component\HttpFoundation\ResponseHeaderBag::DISPOSITION_ATTACHMENT, $name);
//$img_size = (string) (filesize($path . $name)); // Get the image size as string
//
//$mime_type = get_file_mime_type($path . $name); // Get the correct MIME type depending on the file.
//
//response(file_get_contents($path . $name), 200, array(
// 'Pragma' => 'private',
// 'Cache-control' => 'private, must-revalidate',
// 'Content-Type' => $mime_type,
// 'Content-Length' => $img_size,
// 'Content-Disposition' => 'attachment; filename="' . ($name) . '"'
//))->send();
//
//exit;
}
示例8: Application
<?php
include __DIR__ . '/../vendor/autoload.php';
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
$app = new Application();
$app['debug'] = true;
$app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../views'));
$app->get('/', function () use($app) {
return $app['twig']->render('home.twig');
});
$app->post('/img', function (Request $request) use($app) {
$img = $request->get('img');
$fp = fopen(__DIR__ . '/img.jpg', 'w+');
fwrite($fp, base64_decode($img));
fclose($fp);
return $app->json(true);
});
$app->get('/apk', function () use($app) {
return $app->sendFile(__DIR__ . '/../../cordova/Camera/platforms/android/bin/HelloCordova-debug.apk');
});
$app->run();
示例9: downloadClipAction
public function downloadClipAction(Request $request, Application $app, $fileId)
{
/** @var \Doctrine\ORM\EntityManager $entityManager */
$entityManager = $app['entityManager'];
$clip = $entityManager->getRepository('Entities\\Clip')->find($fileId);
if (!$clip) {
$app->abort(404, 'Deze clip bestaat niet');
}
return $app->sendFile('uploads/clips/' . $clip->getPath(), 200, array('Content-type' => $clip->getMimeType()), 'attachment');
}
示例10: getJavascript
/**
* @param Application $app
* @param string $file
* @return BinaryFileResponse
*/
public function getJavascript(Application $app, $file)
{
$mainPath = $app['paths']['sys_root'] . 'main/inc/lib/javascript/';
$fileToLoad = $mainPath . $file;
if (is_file($fileToLoad) && \Security::check_abs_path($fileToLoad, $mainPath)) {
return $app->sendFile($fileToLoad);
}
}
示例11: connect
public function connect(Application $app)
{
$app->view(function (array $result, Request $request) use($app) {
return $app->json($result['body'], $result['code'], $result['headers']);
});
$app->before(function (Request $request) {
if (0 === strpos($request->headers->get('Content-Type'), 'application/json')) {
$data = json_decode($request->getContent(), true);
$request->request->replace(is_array($data) ? $data : []);
}
});
if (isset($app['restapi']['cors'])) {
$app->before(function (Request $request, Application $app) {
return $app['restapi.middleware.cors']->processRequest($request);
}, Application::EARLY_EVENT);
$app->after(function (Request $request, Response $response, Application $app) {
return $app['restapi.middleware.cors']->processResponse($request, $response);
});
}
$controllers = $app['controllers_factory'];
if (isset($app['restapi']['auth'])) {
$controllers->post('/auth/login', function (Request $request) use($app) {
if (!$request->request->has('username') or !$request->request->has('password')) {
return new Response(null, 400, ['Content-Type' => 'application/json']);
}
$username = $request->request->get('username');
$password = $request->request->get('password');
if (true !== $app['restapi.auth']->verifyCredentials($username, $password)) {
return new Response(null, 401, ['Content-Type' => 'application/json']);
}
$response = new Response(['username' => $username, 'token' => $app['restapi.auth']->createJwtTokenForUser($username)]);
$response->headers->setCookie($app['restapi.auth']->createCookieForToken($token));
if (true === $request->request->has('redirect')) {
$response->headers->set('Location', $request->request->get('redirect'));
$response->setStatusCode(302);
}
$response->headers->set('Content-Type', 'application/json');
return $response;
});
$controllers->post('/auth/logout', function () use($app) {
$response = new Response(null, 204, ['Content-Type' => 'application/json']);
$cookie = $app['restapi.auth']->deleteCookie();
$response->headers->setCookie($cookie);
return $response;
});
}
$resources = $app['controllers_factory'];
if (isset($app['restapi']['auth'])) {
$resources->before($app['restapi.listener.auth_checker']);
}
$resources->get('/', function () use($app) {
return $app['restapi.service']->listResources();
});
$resources->get('/files/{hash}', function ($hash) use($app) {
return $app->sendFile($app['restapi.service']->fetchFile($hash));
});
$resources->get('/thumbs/{hash}', function ($hash) use($app) {
try {
return $app->sendFile($app['restapi']['thumbs_path'] . '/' . $app['restapi.storage']->hashToFilePath($hash) . '.png');
} catch (\Exception $e) {
return new Response(null, 404);
}
});
$resources->get('/{table}', function (Request $request, $table) use($app) {
return $app['restapi.service']->readCollection($table, $request->query->all());
});
$resources->post('/{table}', function (Request $request, $table) use($app) {
return $app['restapi.service']->createResource($table, array_merge($request->request->all(), $request->files->all()));
});
$resources->get('/{table}/{pk}', function (Request $request, $table, $pk) use($app) {
return $app['restapi.service']->readResource($table, $pk, $request->query->all());
});
$resources->match('/{table}/{pk}', function (Request $request, $table, $pk) use($app) {
return $app['restapi.service']->updateResource($table, $pk, array_merge($request->request->all(), $request->files->all()));
})->method('POST|PATCH');
$resources->delete('/{table}/{pk}', function ($table, $pk) use($app) {
return $app['restapi.service']->deleteResource($table, $pk);
});
$controllers->mount('/', $resources);
return $controllers;
}
示例12: getUserFile
/**
* @param Application $app
* @param $file
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse|void
*/
public function getUserFile(Application $app, $file)
{
try {
$file = $app['chamilo.filesystem']->get('upload/users/' . $file);
return $app->sendFile($file->getPathname());
} catch (\InvalidArgumentException $e) {
return $app->abort(404, 'File not found');
}
}
示例13: function
$response['Reports'] = $reports;
$response['SearchText'] = $searchText;
$response['Years'] = $years;
// Conversion de la réponse en JSON et retour
return $app->json($response);
});
/*****************************************************************************************
* *
* Transfert des documents pdf pour tracer leur telechargement. *
* *
*****************************************************************************************/
$app->get('/files/{path}', function ($path) use($app) {
if (!file_exists(__DIR__ . '/reports/' . $path)) {
$app->abort(404, "Le fichier " . $app->escape($path) . " n'existe pas.");
}
return $app->sendFile(__DIR__ . '/reports/' . $path);
});
/*****************************************************************************************
* *
* Page des mentions légales *
* *
*****************************************************************************************/
$app->get('/mentions-legales', function () use($app) {
return $app['twig']->render('mentions.twig', array('layout_template' => 'layout.twig'));
})->bind('mentions');
/*****************************************************************************************
* *
* Page des crédits *
* *
*****************************************************************************************/
$app->get('/credit', function () use($app) {
示例14: downloadFile
public function downloadFile(Application $app, Request $request)
{
$relativeFilePath = $request->get('filePath');
$absFilePath = $app['config']->get('admin.FileManager.folderPath') . $relativeFilePath;
return $app->sendFile($absFilePath)->setContentDisposition(ResponseHeaderBag::DISPOSITION_ATTACHMENT, basename($absFilePath));
}
示例15: showImage
public function showImage(Request $request, Application $app, $filename)
{
$showpath = $app['upload_folder'] . "/thumbnail/" . "{$filename}";
return $app->sendFile($showpath);
// returns the path of the thumbnail image
}