Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/257.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php symfony 2中的文件上载错误_Php_Forms_File Upload_Symfony_Doctrine Orm - Fatal编程技术网

Php symfony 2中的文件上载错误

Php symfony 2中的文件上载错误,php,forms,file-upload,symfony,doctrine-orm,Php,Forms,File Upload,Symfony,Doctrine Orm,根据本食谱,尝试设置附加到条令实体的文件上载表单: 以下是我的实体/模型类: <?php use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\ArrayCollection; use Symfony\Component\Validator\Constraints as Assert; use Symfony\Component\HttpFoundation\File\UploadedFile; /** *

根据本食谱,尝试设置附加到条令实体的文件上载表单:

以下是我的实体/模型类:

<?php

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\HttpFoundation\File\UploadedFile;

/**
 * Invoice
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="TechPeople\InvoiceBundle\Entity\InvoiceRepository")
 * @ORM\HasLifecycleCallbacks
 */
class Invoice
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    private $id;

    /**
     * @var integer
     *
     * @ORM\ManyToOne(targetEntity="User", inversedBy="user_invoices")
     * @ORM\JoinColumn(name="vendor_id", referencedColumnName="id")
     */
    private $vendor;

    /**
     * @var string
     *
     * @ORM\Column(name="month", type="string", length=255)
     */
    private $month;

    /**
     * @var integer
     *
     * @ORM\Column(name="year", type="smallint")
     */
    private $year;

    /**
     * @var boolean
     *
     * @ORM\Column(name="expenses", type="boolean")
     */
    private $expenses;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="due", type="date")
     */
    private $due;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="paid", type="date")
     */
    private $paid;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime")
     */
    private $created;

    /**
     * @var float
     *
     * @ORM\Column(name="expense_amount", type="decimal")
     */
    private $expense_amount;

    /**
     * @var float
     *
     * @ORM\Column(name="total_amount", type="decimal")
     */
    private $total_amount;

    /**
     * @var UploadedFile
     *
     * @Assert\File(maxSize="6000000")
     */
    public $attachment;

    /**
     * @var string
     *
     * @ORM\Column(name="attachment_path", type="string", length=255)
     */
    private $attachment_path;

    /**
     * @ORM\OneToMany(targetEntity="InvoiceItem", mappedBy="invoice")
     */
    protected $invoice_items;


    /**
     * Constructor for Invoice Entity class
     */
    public function __construct()
    {
        $this->products = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set month
     *
     * @param string $month
     * @return Invoice
     */
    public function setMonth($month)
    {
        $this->month = $month;

        return $this;
    }

    /**
     * Get month
     *
     * @return string 
     */
    public function getMonth()
    {
        return $this->month;
    }

    /**
     * Set year
     *
     * @param integer $year
     * @return Invoice
     */
    public function setYear($year)
    {
        $this->year = $year;

        return $this;
    }

    /**
     * Get year
     *
     * @return integer 
     */
    public function getYear()
    {
        return $this->year;
    }

    /**
     * Set expenses
     *
     * @param boolean $expenses
     * @return Invoice
     */
    public function setExpenses($expenses)
    {
        $this->expenses = $expenses;

        return $this;
    }

    /**
     * Get expenses
     *
     * @return boolean 
     */
    public function getExpenses()
    {
        return $this->expenses;
    }

    /**
     * Set due
     *
     * @param \DateTime $due
     * @return Invoice
     */
    public function setDue($due)
    {
        $this->due = $due;

        return $this;
    }

    /**
     * Get due
     *
     * @return \DateTime 
     */
    public function getDue()
    {
        return $this->due;
    }

    /**
     * Set paid
     *
     * @param \DateTime $paid
     * @return Invoice
     */
    public function setPaid($paid)
    {
        $this->paid = $paid;

        return $this;
    }

    /**
     * Get paid
     *
     * @return \DateTime 
     */
    public function getPaid()
    {
        return $this->paid;
    }

    /**
     * Set created
     *
     * @param \DateTime $created
     * @return Invoice
     */
    public function setCreated($created)
    {
        $this->created = $created;

        return $this;
    }

    /**
     * Get created
     *
     * @return \DateTime 
     */
    public function getCreated()
    {
        return $this->created;
    }

    /**
     * Set expense_amount
     *
     * @param float $expenseAmount
     * @return Invoice
     */
    public function setExpenseAmount($expenseAmount)
    {
        $this->expense_amount = $expenseAmount;

        return $this;
    }

    /**
     * Get expense_amount
     *
     * @return float 
     */
    public function getExpenseAmount()
    {
        return $this->expense_amount;
    }

    /**
     * Set total_amount
     *
     * @param float $totalAmount
     * @return Invoice
     */
    public function setTotalAmount($totalAmount)
    {
        $this->total_amount = $totalAmount;

        return $this;
    }

    /**
     * Get total_amount
     *
     * @return float 
     */
    public function getTotalAmount()
    {
        return $this->total_amount;
    }

    /**
     * Set attachment
     *
     * @param string $attachment
     * @return Invoice
     */
    public function setAttachment($attachment)
    {
        $this->attachment = $attachment;

        return $this;
    }

    /**
     * Get attachment
     *
     * @return string 
     */
    public function getAttachment()
    {
        return $this->attachment;
    }

    /**
     * Set vendor
     *
     * @param \TechPeople\InvoiceBundle\Entity\User $vendor
     * @return Invoice
     */
    public function setVendor(\TechPeople\InvoiceBundle\Entity\User $vendor = null)
    {
        $this->vendor = $vendor;

        return $this;
    }

    /**
     * Get vendor
     *
     * @return \TechPeople\InvoiceBundle\Entity\User 
     */
    public function getVendor()
    {
        return $this->vendor;
    }

    /**
     * Add invoice_items
     *
     * @param \TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems
     * @return Invoice
     */
    public function addInvoiceItem(\TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems)
    {
        $this->invoice_items[] = $invoiceItems;

        return $this;
    }

    /**
     * Remove invoice_items
     *
     * @param \TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems
     */
    public function removeInvoiceItem(\TechPeople\InvoiceBundle\Entity\InvoiceItem $invoiceItems)
    {
        $this->invoice_items->removeElement($invoiceItems);
    }

    /**
     * Get invoice_items
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getInvoiceItems()
    {
        return $this->invoice_items;
    }

    /**
     * Get path for attachment
     *
     * @param string $type Can return web path, or absolute path, web is default
     * @return null|string
     */
    public function getAttachmentPath($type='web')
    {
        if($type == 'absolute') {
            return null === $this->attachment_path
                ? null
                : $this->getUploadRootDir().'/'.$this->attachment_path;
        } else {
            return null === $this->attachment_path
                ? null
                : $this->getUploadDir().'/'.$this->attachment_path;
        }
    }

    protected function getUploadRootDir()
    {
        // the absolute directory path where uploaded
        // documents should be saved
        return __DIR__.'/../../../../web/'.$this->getUploadDir();
    }

    protected function getUploadDir()
    {
        // get rid of the __DIR__ so it doesn't screw up
        // when displaying uploaded doc/image in the view.
        return 'uploads/invoice/attachments';
    }

    /**
     * @ORM\PrePersist()
     * @ORM\PreUpdate()
     */
    public function preUpload()
    {
        if (null !== $this->attachment) {
            // do whatever you want to generate a unique name
            $filename = sha1(uniqid(mt_rand(), true));
            $this->attachment = $filename.'.'.$this->attachment->guessExtension();
        }
    }

    /**
     * @ORM\PostPersist()
     * @ORM\PostUpdate()
     */
    public function upload()
    {
        if (null === $this->attachment) {
            return;
        }

        // if there is an error when moving the file, an exception will
        // be automatically thrown by move(). This will properly prevent
        // the entity from being persisted to the database on error
        $this->attachment->move($this->getUploadRootDir(), $this->path);

        unset($this->attachment);
    }

    /**
     * @ORM\PostRemove()
     */
    public function removeUpload()
    {
        if ($file = $this->getAttachmentPath('absolute')) {
            unlink($file);
        }
    }
}
可能与以下内容相同:

但这个问题没有得到回答。我希望发布第二个问题不会违反礼仪

我对Symfony2还很陌生,只是想了解一下我的方法,这让我很困惑。我可以发布你需要的任何其他信息,只要告诉我

下面是表单类:

<?php

namespace TechPeople\InvoiceBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class InvoiceType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('month')
            ->add('year')
            ->add('expenses')
            ->add('due')
            ->add('paid')
            ->add('created')
            ->add('expense_amount')
            ->add('total_amount')
            ->add('attachment')
            ->add('vendor')
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'TechPeople\InvoiceBundle\Entity\Invoice'
        ));
    }

    public function getName()
    {
        return 'techpeople_invoicebundle_invoicetype';
    }
}

$this->附件必须是UploadedFile对象

但在方法preUpload()中,您犯了一个错误,并将其重写为字符串:

$this->attachment = $filename.'.'.$this->attachment->guessExtension();
if (null === $this->attachment) {
     return;
}
然后,调用upload()方法并检查$this->attachment是否为null,这是真的,因为它是一个字符串:

$this->attachment = $filename.'.'.$this->attachment->guessExtension();
if (null === $this->attachment) {
     return;
}
并在字符串上执行->move()显示错误;)

要回答这个问题,请在preUpload()方法中替换此行:

$this->attachment= $filename.'.'.$this->attachment->guessExtension();
用于:


就在钱上。谢谢西比奥!
$this->path = $filename.'.'.$this->attachment->guessExtension();