我有一个带有嵌入式文件格式的表单(新闻和新闻文件集合)。在我的localhost机器上一切正常:新闻和NewsFiles实体仍然存在,文件被上传。 但是在生产服务器上,当我尝试添加文件时,请求停止了。文件被上传,实体不存在于db中,post请求停止,状态为:302 Found,它返回空白页而不是重定向到下一页。
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 :(得分:0)
好的,我发现了正在发生的事情。 我的prod服务器不接受chmod功能,它被锁定。 Symfony2文件: /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/UploadedFile.php 有move()函数 - &gt;我已经删除了那里的chmod用法,并且每个人都可以正常工作。
这是修改后的move()函数:
public function move($directory, $name = null)
{
if ($this->isValid()) {
if ($this->test) {
return parent::move($directory, $name);
}
$target = $this->getTargetFile($directory, $name);
if (!@move_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());
}