[英]File upload with Symfony3 not working
我正在嘗試使用Symfony3上傳文件,但是沒有運氣。 我有一個配置文件實體,它以1-1關系鏈接到用戶實體。 該配置文件包含一個圖片列。 我已經創建了一個ProfileType和Profile Model。 提交表單后,模型僅包含文件名,而沒有其他內容。 $ _FILES數組也為空。 這是代碼。
$builder
->add("name", TextType::class, array(
"required" => true,
))
->add("email", EmailType::class, array(
"required" => true,
))
->add("city", TextType::class, array(
"required" => false,
))
->add("country", ChoiceType::class, array(
"required" => false,
))
->add("picture", FileType::class, array(
"required" => false,
));
class ProfileModel
{
private $name;
private $email;
private $city;
private $country;
private $picture;
在Controller中,我正在創建這樣的表單。
$profileForm = $this->createForm(ProfileType::class, $profileModel);
當我得到圖片時,它僅包含名稱。
$file = $profileForm->get("picture")->getData();
呵呵rashidkhan〜
Symfony doc在上傳過程中相當完整,您讀過嗎?
http://symfony.com/doc/current/controller/upload_file.html
進行一些修改后,我選擇將其用作服務。 過程如下:
1)在app/config/config.yml
添加一些參數:
在parameters
下:
parameters:
locale: en
profile_directory: '%kernel.root_dir%/../web/upload/profile'
another_directory: '%kernel.root_dir%/../web/upload/another'
在twig
twig:
debug: "%kernel.debug%"
strict_variables: "%kernel.debug%"
globals:
profile_directory: '/upload/profile/'
another_directory: '/upload/another/'
剛才添加的兩個profile_directory
將在您的上傳服務和樹枝中用作變量,以指向targer目錄。 之后,我添加了another_directory
來解釋更多內容。
2)創建服務:
在src/YourBundle/Services/FileUploader.php
下創建一個新文件
從這里開始,我的代碼與您在Symfony網站上找到的代碼有所不同。
FileUploader.php
內容:
<?php
namespace YourBundle\Services;
use YourBundle\Entity\ProfileModel;
use YourBundle\Entity\Another;
class FileUploader {
private $profileDir;
private $anotherDir;
public function __construct($profileDir) {
$this->profileDir=$profileDir;
$this->anotherDir=$anotherDir;
}
public function upload($class) {
if($class instanceof ProfileModel) {
$file=$class->getPicture();
$fileName='picture-'.uniqid().'.'.$file->guessExtension();
$file->move($this->profileDir, $fileName);
$class->setPicture($fileName);
}
if($class instanceof Another) {
$file=$class->getPicture();
$fileName='picture-'.uniqid().'.'.$file->guessExtension();
$file->move($this->anotherDir, $fileName);
$class->setPicture($fileName);
}
return $class;
}
}
3)將服務注冊到app/config/services.yml
:
在services
:
services:
app.file_uploader:
class: YourBundle\Services\FileUploader
arguments:
- '%profile_directory%'
- '%another_directory%'
每個參數的順序必須與FileUploader.php
文件中的private
順序相同。 這些參數是我們在app/config/config.yml
下設置的parameters
。
4)編輯您的控制器:
控制器部分非常簡單。
添加use Symfony\\Component\\HttpFoundation\\File\\File;
在導入部分
在newAction
public function newAction(Request $request)
{
$profileModel = new ProfileModel();
$form = $this->createForm('YourBundle\Form\ProfileModelType', $profileModel);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
// We upload the file with this line
$profileModel=$this->get('app.file_uploader')->upload($profileModel);
$em = $this->getDoctrine()->getManager();
$em->persist($profileModel);
$em->flush();
return $this->redirectToRoute('profile_model_show', array('id' => $profileModel->getId()));
}
return $this->render('YourBundle:Default:new.html.twig', array(
'profileModel' => $profileModel,
'form' => $form->createView(),
));
}
在editAction
public function editAction(Request $request, ProfileModel $profileModel)
{
// Add this live above everything else in the code.
$profileModel->setPicture(new File($this->getParameter('profile_directory').'/'.$profileModel->getPicture()));
[...]
}
我還沒有走得更遠,所以我只能在之后說明要修改的內容...在您的editAction
,您還必須檢查$ _FILES是否為空。 如果不是,則執行上傳過程。 如果是這樣,請確保不要在SQL查詢中編輯picture
列(您將必須執行自定義查詢)
5)您的樹枝視圖:
在show.html.twig
更改
<tr>
<th>Picture</th>
<td>{{ profileModel.picture) }}</td>
</tr>
至
<tr>
<th>Picture</th>
<td><img src="{{ asset(profile_directory~profileModel.picture) }}"></td>
</tr>
index.html.twig
。 您可以將其添加(而不是替換)到edit.html.twig
,以預覽實際圖片。
6)說明:
在app/config/config.yml
我們添加了一些目錄作為文件中的參數。
以后將可以根據需要更輕松地更改這些目錄。 (不必編輯大量文件...是的!)
Twig目錄始終從/web
文件夾開始。
當我們將服務注冊為arguments
時,將使用這些目錄。 他們將在服務文件FileUploader.php
設置我們的變量。
與Symfony網站示例不同,我們將整個對象傳遞給上傳服務。 然后,我們檢查該對象是從哪個類創建的,並基於該對象執行上載過程。
然后,您在控制器中的上傳過程將縮短為一行。
在樹枝中,我們也將使用在設置目錄變量app/config/config.yml
undet的twig
財產。 就像上面說的,如果我們的上傳目錄更改了,那么我們只需要編輯app/config/config.yml
文件。
希望這可以幫助您解決上傳問題。
親切地
早熟。
你應該試試
$form = $this->createForm(ProfileType::class, $profileModel); $form->handleRequest($request); $file = $profileModel->getBrochure();
更多: http : //symfony.com/doc/current/controller/upload_file.html
伙計們,如果您想在Symfony中上載任何類型的文件,那么我有一個非常簡單的解決方案,下面將對此進行介紹。 為什么我給出簡單的解決方案,因為每當有新版本出現時,您都必須在services.yaml中進行一些設置,或者除了主控制器之外還必須創建其他文件。
所以解決方案是:只需在主控制器中使用move($ storing_place,$ actual_filename)函數。
將以下代碼放入您的控制器文件中。
$folder_path = 'public/uploads/brochures/';
$file = $request->files->get('myfile');
$fileName = $request->files->get('myfile')->getClientOriginalName();
$file->move($folder_path, $fileName);
return new Response($file);
希望給出的解決方案對您的項目有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.