2014-10-08 43 views
0

我有一个窗体与嵌入式文件窗体(新闻和NewsFiles集合)。在我的本地主机上一切正常:新闻和NewsFiles实体持续存在,文件上传。 但是当我尝试添加文件时,生产服务器上的发布请求被停止。文件上传,实体不存在于数据库中,发布请求停止状态:302找到并返回空白页,而不是重定向到下一页。PHP发布请求停止与空白页

public function createAction(Request $request) { 

    $entity = new News(); 
    $form = $this->createCreateForm($entity); 
    $form->handleRequest($request); 

    if ($form->isValid()) { 

     $em = $this->getDoctrine()->getManager(); 

     $em->persist($entity); 

     // PROBLEM APPEARS HERE - WHEN TRY TO FLUSH 
     $em->flush(); 


     $this->get('session')->getFlashBag()->add(
       'success', 'Wykonano pomyślnie!' 
     ); 

     return $this->redirect($this->generateUrl('website_admin_panel_news')); 
    } 

    return $this->render('WebsiteNewsBundle:News:new.html.twig', array(
       'entity' => $entity, 
       'form' => $form->createView(), 
    )); 
} 

当我尝试添加新闻实体(没有文件)时没有问题。我在编辑实体时也是如此。

日志: logs

INFO - Matched route "news_update" (parameters: "_controller": "Website\NewsBundle\Controller\NewsController::updateAction", "id": "6", "_route": "news_update") 
DEBUG - Read SecurityContext from the session 
DEBUG - Reloading user from user provider. 
DEBUG - Username "admin" was reloaded from user provider. 
DEBUG - Write SecurityContext in the session 

我认为这个问题是在服务器端,我会写信给管理员,但他不是专家,所以我需要建议他什么,他必须改变。 .. 有任何想法吗?可能是超时问题?


编辑: 我有更多的信息。这个问题发生在move()函数中。这不是超时问题,因为我试图发送一个小文件(1px - 539字节),但它仍然不能完成这项工作。 这是我的实体上传:

<?php 

namespace Website\NewsBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 
// use Symfony\Component\Validator\Constraints as Assert; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 
use Symfony\Component\Validator\Constraints; 

/** 
* 
* 
* @ORM\Entity 
* @ORM\HasLifecycleCallbacks 
* @ORM\Table(name="NewsFiles") 
* 
*/ 
class NewsFile { 

    private $temp; 

    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="IDENTITY") 
    */ 
    private $id; 

    /** 
    * @ORM\Column(type="string", length=255, nullable=true) 
    * 
    */ 
    private $owner; 

    /** 
    * 
    */ 
    private $file; 

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

    /** 
    * @ORM\Column(type="datetime") 
    */ 
    private $created_at; 

    /** 
    * @ORM\ManyToOne(targetEntity="News", inversedBy="newsFiles") 
    * @ORM\JoinColumn(name="news_id", referencedColumnName="id") 
    */ 
    protected $news; 

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

    /** 
    * @var string 
    * 
    * @ORM\Column(type="boolean", nullable=false) 
    */ 
    private $isMain; 

    /** 
    * Now we tell doctrine that before we persist or update we call the updatedTimestamps() function. 
    * 
    * @ORM\PrePersist 
    * @ORM\PreUpdate 
    */ 
    public function updatedTimestamps() { 
     if ($this->getCreated_At() == null) { 
      $this->setCreated_At(new \DateTime(date('Y-m-d H:i:s'))); 
     } 
    } 

    public function setId($id) { 
     $this->id = $id; 
    } 

    public function getId() { 
     return $this->id; 
    } 

    public function setOwner($owner) { 
     $this->owner = $owner; 
    } 

    public function getOwner() { 
     return $this->owner; 
    } 

    public function setName($name) { 
     $this->name = $name; 
    } 

    public function getName() { 
     return $this->name; 
    } 

    public function setPath($path) { 
     $this->path = $path; 
    } 

    public function getPath() { 
     return $this->path; 
    } 

    /** 
    * Set created_at 
    * 
    * @param string $created_at 
    * @return File 
    */ 
    public function setCreated_at($created_at) { 
     $this->created_at = $created_at; 

     return $this; 
    } 

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

    /** 
    * Sets file. 
    * 
    * @param UploadedFile $file 
    */ 
    public function setFile(UploadedFile $file = null) { 
     $this->file = $file; 
     // check if we have an old image path 
     if (isset($this->path)) { 
      // store the old name to delete after the update 
      $this->temp = $this->path; 
      $this->path = null; 
     } else { 
      $this->path = 'initial'; 
     } 
    } 

    /** 
    * Get file. 
    * 
    * @return UploadedFile 
    */ 
    public function getFile() { 
     return $this->file; 
    } 

    public function getAbsolutePath() { 
     return null === $this->path ? null : $this->getUploadRootDir() . '/' . $this->path; 
    } 

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

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

    protected function getUploadDir() { 

     return 'uploads/News/' . $this->getNews()->getId(); 
    } 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() { 
     if (null !== $this->file) { 
      // zrób cokolwiek chcesz aby wygenerować unikalną nazwę 
      $this->setName(sha1(uniqid(mt_rand(), true))); 
      $this->setPath($this->getName() . '.' . $this->file->guessExtension()); 
     } 

    } 

    /** 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() { 
     // zmienna file może być pusta jeśli pole nie jest wymagane 
     if (null === $this->file) { 
      return; 
     } 

     // HERE HE CANCEL HIS WORK 
     $this->getFile()->move($this->getUploadRootDir(), $this->path); 


     // check if we have an old image 
     if (isset($this->temp)) { 
      echo "isset temp"; 
      // delete the old image 
      unlink($this->getUploadRootDir() . '/' . $this->temp); 
      // clear the temp image path 
      $this->temp = null; 
     } 
     $this->file = null; 

    } 

    /** 
    * @ORM\PostRemove() 
    */ 
    public function removeUpload() { 
     if ($file = $this->getAbsolutePath()) { 
      unlink($file); 
     } 
    } 

    /** 
    * Set news 
    * 
    * @param string $news 
    * @return News 
    */ 
    public function setNews($news) { 
     $this->news = $news; 

     return $this; 
    } 

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

    /** 
    * Set isMain 
    * 
    * @param string $isMain 
    * @return IsMain 
    */ 
    public function setIsMain($isMain) { 
     $this->isMain = $isMain; 

     return $this; 
    } 

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

} 
+0

你有没有试过清除缓存督促环境? – stevenll 2014-10-08 11:25:30

+0

是的,很多次。我无法通过ftp访问命令行,因此我从缓存中删除了文件并将权限设置为777 -R ... – rKow 2014-10-08 12:26:32

+0

进入app.php文件并打开debug:$ kernel = new AppKernel('prod',true);这可能会给你一个更好的错误信息。 – Cerad 2014-10-08 15:13:32

回答

0

好吧,我已经发现了这是怎么回事。 我的prod服务器不接受chmod功能,它被锁定。 Symfony2文件: /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/UploadedFile.php has move()函数 - >我已经删除了chmod使用,并且everythning工作正常。

这里是移动()函数修改后:

public function move($directory, $name = null) 
    { 
     if ($this->isValid()) { 
      if ($this->test) { 
       return parent::move($directory, $name); 
      } 

      $target = $this->getTargetFile($directory, $name); 

      if ([email protected]_uploaded_file($this->getPathname(), $target)) { 
       $error = error_get_last(); 
       throw new FileException(sprintf('Could not move the file "%s" to "%s" (%s)', $this->getPathname(), $target, strip_tags($error['message']))); 
      } 

      return $target; 
     } 

     throw new FileException($this->getErrorMessage()); 
    }