2012-09-06 11 views
12

मैं अपने सिर को लपेटने के लिए प्रतीत नहीं कर सकता कि मैं डेटाफिक्स पर फ़ाइल अपलोड जोड़ने के बारे में कैसे जाऊंगा। मैं अपने फिक्स्चर लोड की डमी सामग्री के लिए एक छवि अपलोड करने की कोशिश कर रहा हूं। ऐसा कुछ ऐसा लगता है जो जानना उपयोगी होगा।आप एक सिम्फनी 2 डेटाफिक्चर में फ़ाइल अपलोड कैसे जोड़ेंगे?

उत्तर

9

मुझे अपने प्रश्न का उत्तर मिला है। फ़ाइल बनाने के लिए मुझे कक्षा Symfony\Component\HttpFoundation\File\File का उपयोग करने की आवश्यकता है। सिम्फनी भौतिक रूप से फ़ाइल को स्थानांतरित करेगी और एक प्रतिलिपि नहीं बनाएगी, इसलिए आपको प्रत्येक स्थिरता के लिए एक नई फ़ाइल की आवश्यकता है, इसके बजाय फ़ाइल की प्रतिलिपि बनाने के लिए copy() का उपयोग करें।

$image = new Image(); 
$file = new File('path/to/file.jpg'); 
$image->file = $file; 
$om->persist($image); 

ऐसा कुछ।

+0

छवि मेरी इकाई है जो फ़ाइल अपलोड को संभालती है, http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html – ornj

+4

** 'फ़ाइल' के बजाय 'अपलोड की गई फ़ाइल' का उपयोग करें ** **, कुछ बंडल (जैसे VichUploader) फ़ाइल को अपलोड किया गया है सत्यापित करें .. (यह वास्तव में मुझे यह समझने में एक घंटा लगा कि क्यों VichUploader मेरी फिक्स्चर फ़ाइल को gaufrette पर अपलोड नहीं करेगा।) –

+0

यह काम नहीं करता है। कामकाजी समाधान @ फ्रेड डुआर्ट के एन्सर में है। – loostro

0

जिस छवि का आप उपयोग करना चाहते हैं वह आपके "वेब" फ़ोल्डर में स्थित होना चाहिए और आपको केवल अपने डेटा स्थिरता में फ़ाइल सूचक स्ट्रिंग (यानी "/web/images/test.png") का उपयोग करना चाहिए।

आपको आमतौर पर अपने डेटाबेस में छवियों को संग्रहीत करने से बचना चाहिए।

+0

। जो मैं करने की कोशिश कर रहा हूं वह एक इकाई के माध्यम से एक फ़ाइल लोड करता है और सिद्धांत को फ़ाइल का प्रबंधन करने की अनुमति देता है। एक उदाहरण के रूप में [इस आलेख] (http://symfony.com/doc/current/cookbook/doctrine/file_uploads.html) का उपयोग करके, मैं डेटा फ़िक्स्चर द्वारा अस्थायी फ़ाइलों को अपलोड करने की अनुमति देने के लिए इस पर निर्माण करने की कोशिश कर रहा हूं। – ornj

16

हालांकि इस सवाल से 1 साल पहले पूछा गया है, ऐसा लगता है कि वहां डेटा जानकारी के माध्यम से फ़ाइल अपलोड करने के तरीके पर बहुत सारी जानकारी नहीं है। मुझे केवल यह पोस्ट मिल सकता है।

मैं देख रहा हूं और मैंने ऑर्ंज की तुलना में थोड़ा अलग दृष्टिकोण लिया है। (Symfony के अपडेट के साथ क्या करना पड़ सकता है।)

मैं पहली बार

use Symfony\Component\HttpFoundation\File\UploadedFile; 

करना पड़ा और उसके बाद इस्तेमाल किया प्रतिलिपि() चित्र की प्रतिलिपि बनाने क्योंकि जैसा कि ornj ने कहा कि यह ले जाएगा।

copy($art1->getFixturesPath() . '01.jpg', $art1->getFixturesPath() . '01-copy.jpg'); 

तो बना सकते हैं और का उपयोग करके फ़ाइल जोड़ें:

$file = new UploadedFile($art1->getFixturesPath() . '01-copy.jpg', 'Image1', null, null, null, true); 

$art1->setFile($file); 

$manager->persist($art1); 

मैं पिछले पैरामीटर सेट नहीं किया है करने के लिए '' सही '' '' UploadedFile '' निर्माता में के रूप में यह एक अज्ञात फेंकता '' सिद्धांत: फिक्स्चर: लोड 'चलते समय त्रुटि। यह पैरामीटर है "क्या परीक्षण मोड सक्रिय है"। यह एक स्थिरता देखकर परीक्षण मोड में सेट करना समझ में आता है।

विधि '' getFixturesPath() '' सिर्फ पथ जहाँ मेरे नमूना छवियों को पुन: प्राप्त जमा हो जाती है:

// Entity file 
public function getFixturesPath() 
{ 
    return $this->getAbsolutePath() . 'web/uploads/art/fixtures/'; 
} 

'' getAbsolutePath() '' विधि Doctrine File Uploads से लिया गया है।

पूर्ण काम कर कोड: निकाय:

<?php 
//src/User/MyBundle/Entity/Art.php 

namespace User/MyBundle/Entity; 

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

/** 
* 
* Art Entity 
* 
* @ORM\Entity(repositoryClass="User\MyBundle\Entity\Repository\ArtRepository") 
* @ORM\Table(name="art") 
* @ORM\HasLifecycleCallbacks 
*/ 
class Art 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /** 
    * @ORM\Column(type="string", length=100) 
    */ 
    protected $title; 

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

    /** 
    * @Assert\File(maxSize="6000000") 
    */ 
    private $file; 

    private $temp; 

    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() 
    { 
     // get rid of the __DIR__ so it doesn't screw up 
     // when displaying uploaded doc/image in the view. 
     return 'uploads/art'; 
    } 

    public function getFixturesPath() 
    { 
     return $this->getAbsolutePath() . 'web/uploads/art/fixtures/'; 
    } 

    /** 
    * 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; 
    } 

    /** 
    * @ORM\PrePersist() 
    * @ORM\PreUpdate() 
    */ 
    public function preUpload() 
    { 
     if (null !== $this->getFile()) { 
      // do whatever you want to generate a unique filename 
      $filename = sha1(uniqid(mt_rand(), true)); 
      $this->path = $filename . '.' . $this->getFile()->guessExtension(); 
     } 
    } 

    /** 
    * @ORM\PostPersist() 
    * @ORM\PostUpdate() 
    */ 
    public function upload() 
    { 
     // the file property can be empty if the field is not required 
     if (null === $this->getFile()) { 
      return; 
    } 

     // if there is an error moving the file, an exception will 
     // be automatically thrown by move(). This will properly prevent 
     // the entity from being persisted to the database on error 
     $this->getFile()->move($this->getUploadRootDir(), $this->path); 

     // check if we have an old image 
     if (isset($this->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); 
     } 
    } 
} 

स्थिरता:

<?php 
// src/User/MyBundle/DataFixtures/ORM/ArtFixtures.php 

namespace User\MyBundle\DataFixtures\ORM; 

use Doctrine\Common\DataFixtures\AbstractFixture; 
use Doctrine\Common\DataFixtures\OrderedFixtureInterface; 
use Doctrine\Common\Persistence\ObjectManager; 
use Fredzz\LotwBundle\Entity\Art; 
use Symfony\Component\HttpFoundation\File\UploadedFile; 

class ArtFixtures extends AbstractFixture implements OrderedFixtureInterface 
{ 
    public function load(ObjectManager $manager) 
    { 
     $art1 = new Art(); 
     $art1->setTitle('MyTitle'); 
     $art1->setDescription('My description'); 

     copy($art1->getFixturesPath() . '01.jpg', $art1->getFixturesPath() . '01-copy.jpg'); 
     $file = new UploadedFile($art1->getFixturesPath() . '01-copy.jpg', 'Image1', null, null, null, true); 
     $art1->setFile($file); 

     $art1->setUser($manager->merge($this->getReference('user-1'))); 

     $manager->persist($art1); 
     $manager->flush(); 
    } 
} 

आशा इस कोई मदद करता है! क्षमा करें अगर कुछ गलत है। मैं अभी भी सीख रहा हूँ :)

+0

आप अपनी इकाई वर्ग पर बहुत अधिक तर्क सहित! ये सभी सहायक (पूर्ण प्राप्त करें, फिक्स्चर प्राप्त करें ..) को कुछ सहायक सेवा में लागू किया जाना चाहिए .. या 'स्थिरता डेटा' वर्ग –

+1

पर यह सिर्फ मुझे बचाया - सबसे महत्वपूर्ण चाल अपलोड किए गएफ़ाइल के $ testMode को सत्य पर सेट करना है। – Maerlyn

+0

यह स्वीकृत anwser होना चाहिए। – loostro

0

मैं PHP 5.3+

का उपयोग कैसे करें के लिए एक FileUpload श्रेणी का निर्माण?:

Documentation

RFC 3023 से (एक्सएमएल मीडिया प्रकार):

शीर्ष स्तर के मीडिया प्रकार "पाठ" वे [RFC2045 में वर्णित हैं माइम संस्थाओं पर कुछ प्रतिबंध है और ] और [आरएफसी 2046]। विशेष रूप से, यूटीएफ -16 परिवार, यूसीएस -4, और यूटीएफ -32 की अनुमति नहीं है ( HTTP [RFC2616] को छोड़कर, जो एक एमआईएम-जैसी तंत्र का उपयोग करता है)।

केवल YAML फ़ाइल अपलोड करने के लिए अनुमति दें: सभी माइम प्रकार के साथ

<?php 
$file = new FileUpload\FileUpload(); 
$file->setInput("file"); 
$FileUpload->setAllowedMimeTypes(array(
    "text/x-yaml", //RFC 3023 
    "application/x-yaml", // Ruby on Rails 
    "text/plain",//Possible option(only text plain) 
    "text/yaml",//Possible option 
    "text/x-yaml",//Possible option 
    "application/yaml",//Possible option 
)); 
$file->setDestinationDirectory("/var/www/html/myapp/"); 
$file->save(); 
if ($file->getStatus()) { 
    echo "Okay"; 
} 
?> 

नमूना:

<?php 
$file = new FileUpload\FileUpload(); 
$file->setInput("file"); 
$file->save(); 
if ($file->getStatus()) { 
    echo "is Upload!"; 
} 
?> 
<html> 
    <head> 
     <title>FileUpload Example</title> 
     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
    </head> 
    <body> 
     <form method="post" action="" enctype="multipart/form-data"> 
      <input type="file" name="file" /> 
      <input type="submit" value="Upload now!" /> 
     </form> 
    </body> 
</html> 

GitHub: https://github.com/olaferlandsen/FileUpload-for-PHP

बेशक