我已经从Symfony 3.4更新到4.0,并验证了操作。图片保存功能显示"完成",但是没有保存图片。因为保存目的地被设置为web/uploads,在image .php中,我将图像目录路径从web
更改为public
,但它没有改变。还有什么需要更改的吗?
ImageController.php
public function saveAction(Request $request)
{
$response = new Response();
$imageService = $this->get("admin.imageService");
if ($token === $imageService->getImageToken($staffId, $uniq)) {
try {
$imageService->saveImage($image);
// Normal termination parameters #This is required for onComplete to fire on Mac OSX
$msg = $this->container->getParameter('uploadify_success');
} catch (Exception $e) {
$msg = $e->getMessage();
$response->setStatusCode(400);
}
} else {
$msg = 'Access is illegal.';
$response->setStatusCode(400);
}
$response->send();
ImageService.php
public function saveImage(Image $image)
{
// Set the extension (already set when saving from an email attachment)
$ext = $image->getImageExtension();
if (!$ext && $image->getFileUpload()) {
$ext = $image->getFileUpload()->guessExtension();
$image->setImageExtension($ext);
}
// save
$this->entityManager->persist($image);
$this->entityManager->flush();
}
Image.php
private function getUploadRootDir()
{
// Absolute path to the location to save the uploaded file
return __DIR__.'/../../../../../../public/'.$this->getUploadDir();
}
/**
* Returns the image directory name
*
* @return string
*/
private function getUploadDir()
{
return 'uploads';
}
Postscript
index.html.twig
{% block javascripts %}
{{ parent() }}
<script src="{{ asset('uploadifive/jquery.uploadifive.js') }}"></script>
<script src="{{ asset('js/image.five.js') }}"></script>
{% endblock %}
{% block stylesheets %}
{{ parent() }}
<link rel="stylesheet" href="{{ asset('uploadifive/uploadifive.css') }}">
{% endblock %}
{# contentBody #}
{% block contentBody %}
{{ render(controller('AppBundle:Hq/Image:manager')) }}
{% endblock %}
manager.html.twig
<div class="tabcontent">
<div class="operations">
{{ form_start(form, {"attr": {"id": "uploadForm"}}) }}
{{ form_widget(form.fileUpload) }}
{{ form_rest(form) }}
{{ form_end(form) }}
</div>
<div id="fileQueue"></div>
</div>
你应该用projectDir
代替你的getUploadRootDir()
和危险的../../
看一下这篇文章Symfony 4,从一个自定义类(不是控制器类)中获得项目的根路径的详细说明
Symfony 3.3新增功能:https://symfony.com/blog/new-in-symfony-3-3-a-simpler-way-to-get-the-project-root-directory