Symfony3:“无法访问文件/ tmp / php4k3bf8”

时间:2017-08-13 10:49:25

标签: symfony vichuploaderbundle

上传文件时收到错误消息“无法访问文件/ tmp / php4k3bf8”。请参阅此处的完整错误日志。

[2017-08-13 15:50:29] request.INFO: Matched route "apply_for_job". {"route":"apply_for_job","route_parameters":{"_controller":"Vendor\\FinanceBundle\\Controller\\JobResponseController::createAction","job_id":"2850","_route":"apply_for_job"},"request_uri":"https://my.site.in/job-enrollment/open-position/2850/apply","method":"POST"} []
[2017-08-13 15:50:29] security.INFO: Populated the TokenStorage with an anonymous Token. [] []
[2017-08-13 15:50:29] request.CRITICAL: Uncaught PHP Exception Symfony\Component\HttpFoundation\File\Exception\AccessDeniedException: "The file /tmp/php4k3bf8 could not be accessed" at /mnt/hdd2/ya_guest/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php line 127 {"exception":"[object] (Symfony\\Component\\HttpFoundation\\File\\Exception\\AccessDeniedException(code: 0): The file /tmp/php4k3bf8 could not be accessed at /mnt/hdd2/ya_guest/vendor/symfony/symfony/src/Symfony/Component/HttpFoundation/File/MimeType/MimeTypeGuesser.php:127)"} []

这是用于表单提交的实体:

<?php

namespace Vendor\FinanceBundle\Entity;

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

/**
 * @ORM\Entity
 * @ORM\Entity(repositoryClass="Vendor\FinanceBundle\Repository\RRFPreScreenRepository")
 * @ORM\Table(name="vendor_finance_rrf_pre_screen")
 * @Vich\Uploadable
 */
class RRFPreScreen
{

    /**
     * Unique Id
     *
     * @var integer $id
     *
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;

    ///
    /// ...
    ///

    /**
     * NOTE: This is not a mapped field of entity metadata, just a simple property.
     *
     * @Vich\UploadableField(mapping="profile_image_prescreen", fileNameProperty="imageName")
     *
     * @var File $imageFile
     */
    protected $imageFile;

    /**
     * @ORM\Column(type="string", length=255, name="profile_image", nullable=true)
     *
     * @var string $imageName
     */
    protected $imageName;

    /**
     * @ORM\Column(type="datetime", name="updated_at", nullable=true)
     *
     * @var \DateTime $updatedAt
     */
    protected $updatedAt;

    public function __construct()
    {
        $this->created = new \DateTime('now');
    }

    /**
     * If manually uploading a file (i.e. not using Symfony Form) ensure an instance
     * of 'UploadedFile' is injected into this setter to trigger the  update. If this
     * bundle's configuration parameter 'inject_on_load' is set to 'true' this setter
     * must be able to accept an instance of 'File' as the bundle will inject one here
     * during Doctrine hydration.
     *
     * @param File|\Symfony\Component\HttpFoundation\File\UploadedFile $image
     */
    public function setImageFile(File $image = null)
    {
        $this->imageFile = $image;

        if ($image) {
            // It is required that at least one field changes if you are using doctrine
            // otherwise the event listeners won't be called and the file is lost
            $this->updatedAt = new \DateTime('now');
        }
    }
}

然后是表单类型:

<?php

namespace Vendor\FinanceBundle\Form\Type;

use Vendor\FinanceBundle\Entity\RRFPreScreen;
use Vendor\FormBundle\Form\Type\CountryType;
use Vendor\FormBundle\Form\Type\EducationalQualificationType;
use Vendor\FormBundle\Form\Type\FunctionalAreaType;
use Vendor\FormBundle\Form\Type\MultiFileType;
use Vendor\FormBundle\Form\Type\OtherLanguageType;
use Vendor\FormBundle\Form\Type\ProgramHistoryType;
use Vendor\FormBundle\Form\Type\StateIndiaType;
use Vendor\FormBundle\Form\Type\WorkLocationType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\FileType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextareaType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Vendor\FinanceBundle\Form\Type\RRFDocumentType;

class JobResponseType extends AbstractType
{

    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $self = $this;
        $factory = $builder->getFormFactory();


        $builder->addEventListener(
            FormEvents::PRE_SET_DATA,
            function(FormEvent $event) use($self, $factory, $builder) {
                $form = $event->getForm();
                $preScreen = $event->getData();

                $form
                    ->add('firstName', TextType::class, array(
                        'attr' => array('data-section' => 'Personal Details'),
                        'label_attr' => array('data-note' => 'Please provide your name as displayed in your identity proof and expand all initials'),
                    ))
                    ->add('lastName', TextType::class, array())
                    ->add('phone', TextType::class)
                    ->add('email', TextType::class, array(
                        'label_attr' => array('data-note' => 'As we will be contacting you by mail, please double check the spelling of your mail id.')
                    ))

                    ///
                    /// ...
                    /// 

                    ->add('imageFile',FileType::class, array(
                        'required' => false,
                        'label' => 'Upload your profile picture',
                    ))
                    ->add('multiFiles', CollectionType::class, array(
                        'entry_type' => MultiFileType::class,
                        'allow_add' => true,
                        'allow_delete' => true,
                        'prototype' => true,
                        'prototype_name' => 'multiFiles__name__',
                        'by_reference' => false,
                        'required' => false,
                        'label' => 'Upload your CV, Cover Letter, or Certificates',
                        'label_attr' => array('class' => 'force-validation'),
                        'attr' => array('data-entry' => '1', 'data-max-entry' => '3', 'class' => 'suggested-rows col-sm-8')
                    ));
            }
        );

    }

    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Vendor\FinanceBundle\Entity\RRFPreScreen',
        ));
    }

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

我很确定imageFile会导致问题,因为只有在我将属性添加到类中后才会出现错误。

最后是config.yml中Vich的配置:

  vich_uploader:
  db_driver: orm

  mappings:

      profile_image_prescreen:
          uri_prefix:         /uploads/images-prescreen
          upload_destination: %kernel.root_dir%/data/uploads/images-prescreen

          inject_on_load:     false #should the file be injected into the uploadable entity when it is loaded from the data store
          delete_on_update:   true #should the file be deleted when a new file is uploaded
          delete_on_remove:   true #should the file be deleted when the entity is removed

有人知道如何解决这个问题吗? / tmp文件夹的权限是777.

更新

我正在运行Ubuntu 14.04并具有对服务器的root访问权限。

更新2

运行ls -la / tmp /时,这是我得到的输出:

my_username@servername:~/suvya$ ls -la /tmp/
total 4649812
drwxrwxrwt  6 root     root          36864 Aug 17 07:44 .
drwxr-xr-x 23 root     root           4096 Jul 19 07:14 ..
-rw-r--r--  1 root     root     4739987837 Aug 16 23:59 data_160817.tgz
-rw-r--r--  1 www-data www-data          6 Aug 13 20:28 dos-100.12.183.170
-rw-r--r--  1 www-data www-data          5 Jul 21 16:04 dos-103.16.71.202
-rw-r--r--  1 www-data www-data          6 Jul 22 09:55 dos-103.245.119.146
-rw-r--r--  1 www-data www-data          6 Aug 14 09:14 dos-103.42.174.116
-rw-r--r--  1 www-data www-data          6 Jul 29 17:04 dos-103.46.192.6
-rw-r--r--  1 www-data www-data          5 Jul 28 06:24 dos-106.51.37.22
-rw-r--r--  1 www-data www-data          6 Jul 22 09:27 dos-106.51.48.108
-rw-r--r--  1 www-data www-data          5 Aug  4 22:42 dos-110.172.170.68
-rw-r--r--  1 www-data www-data          6 Jul 28 02:46 dos-111.93.198.38
-rw-r--r--  1 www-data www-data          6 Jul 21 18:47 dos-115.110.134.131
-rw-r--r--  1 www-data www-data          5 Jul 19 15:55 dos-115.249.1.117
-rw-r--r--  1 www-data www-data          5 Aug  7 14:54 dos-117.239.110.178
-rw-r--r--  1 www-data www-data          6 Jul 26 09:28 dos-117.239.66.131
-rw-r--r--  1 www-data www-data          6 Aug  9 13:19 dos-122.15.252.20
-rw-r--r--  1 www-data www-data          6 Aug  5 11:26 dos-122.164.65.31
-rw-r--r--  1 www-data www-data          6 Aug  1 14:16 dos-155.94.150.75
-rw-r--r--  1 www-data www-data          6 Jul 24 12:05 dos-157.50.8.106
-rw-r--r--  1 www-data www-data          6 Jul 24 11:22 dos-157.50.9.96
-rw-r--r--  1 www-data www-data          6 Aug  9 22:09 dos-157.55.39.132
-rw-r--r--  1 www-data www-data          6 Jul 30 08:52 dos-157.55.39.137
-rw-r--r--  1 www-data www-data          6 Aug  9 22:11 dos-157.55.39.166
-rw-r--r--  1 www-data www-data          6 Aug 10 21:28 dos-157.55.39.171
-rw-r--r--  1 www-data www-data          6 Aug 15 22:36 dos-157.55.39.173
-rw-r--r--  1 www-data www-data          5 Aug 12 20:15 dos-157.55.39.174
-rw-r--r--  1 www-data www-data          6 Aug  2 22:54 dos-157.55.39.179
-rw-r--r--  1 www-data www-data          6 Jul 23 14:50 dos-157.55.39.191
-rw-r--r--  1 www-data www-data          6 Aug 15 22:39 dos-157.55.39.206
-rw-r--r--  1 www-data www-data          6 Jul 28 02:17 dos-157.55.39.233
-rw-r--r--  1 www-data www-data          6 Aug 15 22:37 dos-157.55.39.43
-rw-r--r--  1 www-data www-data          5 Jul 30 05:18 dos-157.55.39.71
-rw-r--r--  1 www-data www-data          5 Aug 12 20:16 dos-157.55.39.79
-rw-r--r--  1 www-data www-data          5 Aug  5 19:22 dos-157.55.39.81
-rw-r--r--  1 www-data www-data          6 Aug  9 22:10 dos-157.55.39.86
-rw-r--r--  1 www-data www-data          6 Jul 28 02:16 dos-157.55.39.88
-rw-r--r--  1 www-data www-data          5 Aug  4 22:14 dos-172.73.76.38
-rw-r--r--  1 www-data www-data          5 Jul 28 16:56 dos-175.138.239.41
-rw-r--r--  1 www-data www-data          6 Aug  7 08:02 dos-187.158.39.247
-rw-r--r--  1 www-data www-data          5 Jul 27 11:06 dos-1.9.102.127
-rw-r--r--  1 www-data www-data          6 Aug  2 23:44 dos-192.206.181.81
-rw-r--r--  1 www-data www-data          6 Jul 27 14:39 dos-202.129.198.162
-rw-r--r--  1 www-data www-data          5 Jul 19 11:05 dos-203.106.159.34
-rw-r--r--  1 www-data www-data          5 Aug  1 09:24 dos-207.46.13.104
-rw-r--r--  1 www-data www-data          6 Jul 30 08:52 dos-207.46.13.108
-rw-r--r--  1 www-data www-data          5 Aug 12 20:18 dos-207.46.13.115
-rw-r--r--  1 www-data www-data          6 Aug 10 21:27 dos-207.46.13.126
-rw-r--r--  1 www-data www-data          6 Jul 23 14:48 dos-207.46.13.128
-rw-r--r--  1 www-data www-data          5 Aug  5 19:22 dos-207.46.13.142
-rw-r--r--  1 www-data www-data          6 Jul 30 08:53 dos-207.46.13.18
-rw-r--r--  1 www-data www-data          5 Jul 20 18:22 dos-207.46.13.26
-rw-r--r--  1 www-data www-data          5 Jul 20 18:22 dos-207.46.13.39
-rw-r--r--  1 www-data www-data          6 Jul 24 11:21 dos-207.46.13.51
-rw-r--r--  1 www-data www-data          5 Jul 20 18:23 dos-207.46.13.72
-rw-r--r--  1 www-data www-data          6 Aug  9 22:09 dos-207.46.13.82
-rw-r--r--  1 www-data www-data          5 Aug  1 09:24 dos-207.46.13.94
-rw-r--r--  1 www-data www-data          5 Aug  3 05:09 dos-210.242.49.157
-rw-r--r--  1 www-data www-data          6 Jul 19 21:59 dos-220.225.141.237
-rw-r--r--  1 www-data www-data          6 Aug  6 15:12 dos-2.224.61.44
-rw-r--r--  1 www-data www-data          6 Aug 15 14:19 dos-223.225.135.102
-rw-r--r--  1 www-data www-data          5 Jul 19 23:43 dos-24.184.101.84
-rw-r--r--  1 www-data www-data          6 Jul 20 16:14 dos-27.100.26.179
-rw-r--r--  1 www-data www-data          6 Aug  2 18:20 dos-27.59.88.173
-rw-r--r--  1 www-data www-data          6 Jul 28 00:34 dos-27.62.23.187
-rw-r--r--  1 www-data www-data          6 Jul 19 18:31 dos-37.228.242.81
-rw-r--r--  1 www-data www-data          5 Jul 27 10:00 dos-40.77.167.136
-rw-r--r--  1 www-data www-data          6 Jul 23 14:50 dos-40.77.167.14
-rw-r--r--  1 www-data www-data          5 Jul 21 04:44 dos-42.60.136.149
-rw-r--r--  1 www-data www-data          6 Jul 23 08:08 dos-49.134.47.203
-rw-r--r--  1 www-data www-data          6 Aug  4 15:26 dos-49.178.4.104
-rw-r--r--  1 www-data www-data          6 Aug 13 20:12 dos-49.35.12.88
-rw-r--r--  1 www-data www-data          6 Aug  2 13:14 dos-59.124.164.111
-rw-r--r--  1 www-data www-data          6 Aug 13 07:43 dos-69.127.115.103
-rw-r--r--  1 www-data www-data          6 Jul 29 23:56 dos-78.13.186.72
-rw-r--r--  1 www-data www-data          6 Aug  7 10:26 dos-78.180.127.129
-rw-r--r--  1 www-data www-data          6 Aug  6 10:29 dos-89.212.48.222
-rw-r--r--  1 www-data www-data          6 Jul 25 17:59 dos-90.54.206.172
-rw-r--r--  1 www-data www-data          5 Aug  6 05:54 dos-97.123.243.123
drwxr-xr-x  2 root     root           4096 Aug 16 23:34 hsperfdata_root
drwxrwxrwt  2 root     root           4096 Jul 19 07:14 .ICE-unix
drwxr-xr-x  2 root     root           4096 Aug 16 23:56 webapp
-rw-r--r--  1 root     root       21040943 Aug 16 23:56 webapp_160817.tgz
drwxrwxrwt  2 root     root           4096 Jul 19 07:14 .X11-unix

1 个答案:

答案 0 :(得分:1)

我不确定但是 ls -la / tmp / 输出可能是下面的问题。

  

my_username @ servername:〜/ suvya $ ls -la / tmp /   总计4649812   drwxrwxrwt 6 root root 36864 Aug 17 07:44。

似乎/ tmp所有者是root我想它应该是HTTP,即 www-data

如果是这种情况,那么您可以从php.ini文件中更改tmp目录位置并设置为项目中的某个位置

  

upload_tmp_dir = “your_project_base_path / TMP”