0

我在控制器中使用了以下代码以获取上传文件的文件名我的控制器是

class uploadController extends Controller

{

public function uploadAction(Request $request)
{

$id= $_GET['id'];


$user = new attachments();

$form = $this->createFormBuilder($user)->add('files','file',array("data_class" => null,"attr"=>array("multiple" =>"multiple",)))->getForm();

$formView = $form->createView();

    $formView->getChild('files')->set('full_name','files[]');
if ($request->getMethod() == 'POST') 

  {

            $em = $this->getDoctrine()->getManager();
            $data = $form["files"]->getData();
   }
}

当我打印 $data 它没有给出上传文件的文件名它返回空值

我的实体是:

use Symfony\Component\HttpFoundation\File\UploadedFile;

class attachments
{

private $id;

/**
 * @var integer
 * @ORM\Column(name="user", type="integer", nullable=false)
 * @ORM\ManyToOne(targetEntity="users", inversedBy="annotations")
 */
protected $userId;

/**
 * @var string
 * 
 * @Assert\File(maxSize="6000000")
 * @ORM\Column(name="files", type="array", length=255, nullable=true)
 */
public $files=array();
public function __construct() 
{

}
/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}
 /**
 * Set userId
 *
 * @param integer $userId
 * @return attachments
 */
public function setUserId($userId) 
{
    $this->userId = $userId;
    return $this;
}
/**
 * Set files
 * @param object $files
 * 
 * @return attachments
 */
public function setFiles($files)
{
    $this->files = $files;
}
 /**
 * Get files
 *
 * @return object 
 */
public function getFiles()
{
    return $this->files;
}

 public function uploadFiles() 
{
    // the files property can be empty if the field is not required
    if (null === $this->files) 
{

        return;
    }
else
{
        $this->files->move($this->getUploadRootDir(), $this->files->getClientOriginalName());
    }
    $this->setFiles($this->files->getClientOriginalName());
}
/**
 * Get userId
 *
 * @return integer 
 */
public function getUserId()
{
    return $this->userId;
}
public function getAbsolutePath()
{
    return null === $this->path
        ? null
        : $this->getUploadRootDir() . DIRECTORY_SEPARATOR . $this->path;
}

public function getWebPath()
{
    return null === $this->path
        ? null
        : $this->getUploadDir() . DIRECTORY_SEPARATOR . $this->path;
}

protected function getUploadRootDir()
{
    return __DIR__ . '/../../../../web/'. $this->getUploadDir();
}

protected function getUploadDir()
{
    return 'uploads/';
}

}

4

1 回答 1

1

Symfony2 中上传的文件是Symfony/Component/HttpFoundation/File/UploadedFile类型。

您可以通过以下方式获取原始客户端名称(将文件放入 php_upload_tmp_dir 时 php 将重命名文件):

$file->getClientOriginalName();

...文件移动到新位置:

$file->move('path/to/your_file', 'new_name.jpg');

您不能对数组使用断言文件约束。

* @Assert\File(maxSize="6000000")
*/
 protected $files = array();

因此,您需要All约束。

此外,您不能只在数组或集合上调用 move 方法……您必须遍历集合/数组。

$this->files->move('..')    // this is never going to work...

如果这是您想要的,请使用数组集合并为您上传的文件创建一个属性。

protected $files;

protected $uploadedFiles;

public function __construct()
{
    $this->files = new ArrayCollection;
    $this->uploadedFiles = new Array();
}

如果要将 UploadedFile 实体的 Doctrine 集合转换为数组,请执行以下操作:

$collection = $entity->getFiles();
$array = $collection->toArray();

但无论您尝试做什么......最好使用 OOP 而不是像您在这里尝试的数组。

于 2013-05-27T10:10:46.667 回答