PHP post request stopped with blank page

∥☆過路亽.° 提交于 2020-01-26 02:28:24

问题


I've got a form with embedded file form (News and NewsFiles collection). On my localhost machine everything works fine: News and NewsFiles entities persists, files are uploaded. But on production server post request is stopped when I try add file. Files are uploaded, entities don't exist in db, post request is stopped with status: 302 Found and it returns blank page instead of redirect to next page.

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(),
    ));
}

There is no problem when I try to add only News entity (without files). The same is when I am editing entity.

LOGS: 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

I think the problem is on the server side, I will write to the administrator but he isn't an expert so I need to suggest him what he has to change... Any ideas? May it be a problem of timeouts?


Edit: I've got more information. The problem occurs in move() function. It's not a problem with timeouts because I've tried to send little file (1px - 539 byte) and it still doesn't do the job. Here is my Entity to upload:

<?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;
    }

}

回答1:


Ok, I've discovered what's going on. My prod server doesn't accept chmod function, it is locked. The Symfony2 file: /vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/UploadedFile.php has move() function -> I've deleted chmod usage there and everythning works fine.

Here is move() function after modification:

   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());
    }   


来源:https://stackoverflow.com/questions/26251854/php-post-request-stopped-with-blank-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!