PHP post请求已停止,页面为空白
我有一个嵌入文件表单的表单新闻和新闻文件集合。在我的本地主机上,一切正常:新闻和新闻文件实体持续存在,文件上传。 但在生产服务器上,当我尝试添加文件时,post请求停止。文件已上载,数据库中不存在实体,post请求已停止,状态为:302已找到,它返回空白页,而不是重定向到下一页PHP post请求已停止,页面为空白,php,symfony,post,request,Php,Symfony,Post,Request,我有一个嵌入文件表单的表单新闻和新闻文件集合。在我的本地主机上,一切正常:新闻和新闻文件实体持续存在,文件上传。 但在生产服务器上,当我尝试添加文件时,post请求停止。文件已上载,数据库中不存在实体,post请求已停止,状态为:302已找到,它返回空白页,而不是重定向到下一页 public function createAction(Request $request) { $entity = new News(); $form = $this->createCreate
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(),
));
}
当我尝试只添加没有文件的新闻实体时没有问题。编辑实体时也是如此
日志:
我认为问题出在服务器端,我会写信给管理员,但他不是专家,所以我需要向他建议他需要更改的内容。。。有什么想法吗?可能是超时的问题吗
编辑:
我有更多的信息。问题发生在移动函数中。这不是一个超时问题,因为我已经尝试发送小文件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;
}
}
好吧,我发现了发生的事情。 我的prod服务器不接受chmod功能,它已锁定。Symfony2文件: /供应商/symfony/symfony/src/symfony/Component/HttpFoundation/File/UploadedFile.php 有移动功能->我已经删除了chmod的用法,一切正常 以下是修改后的移动功能:
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());
}
您是否尝试过为prod环境清除缓存?是的,多次。我没有访问命令行的权限,只能通过ftp访问,所以我已经从缓存中删除了文件,并将权限设置为777-R…进入app.php文件并打开debug:$kernel=new-AppKernel'prod',true;这可能会给你一个更好的错误消息;prod真的会更好吗?
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());
}