Do you know what an “important” function to add is to upload a photo and rename it?
See the official documentation on how to do this. There are good working examples for simple file uploads. Also check out the doctrine documentation for lifecycle callbacks .
How to check the extension to see if download is possible?
Each browser has an HTML form check. See this question for the HTML attribute accept="" in input . Also in Symfony2, you can specify the MIME type of the downloaded file using this annotation:
/** * @Assert\File( * maxSize = "1024k", * mimeTypes = {"application/pdf", "application/x-pdf"}, * mimeTypesMessage = "Please upload a valid PDF" * ) */
Even if you do not want to use any bundles, I should recommend you KnpDoctrineBehavioursBundle , which makes downloading files easier.
Step by step:
Since you already read the documentation, I will give you a step-by-step code example.
First of all, you need an entity. Let me call it Image :
class Image extends BaseEntity {
Note the annotation @ORM\HasLifecycleCallbacks . This is very important, and you need it later. We create all the main fields, such as ID and what not. We also need a field to store the file path in:
protected $path;
And one for the image itself. Here we also define validation for images. In my example, this should be 5M large and one of the defined mimeTypes . It should be clear. Otherwise, official documents help, as always.
/** * Image file * * @var File * * @Assert\File( * maxSize = "5M", * mimeTypes = {"image/jpeg", "image/gif", "image/png", "image/tiff"}, * maxSizeMessage = "The maxmimum allowed file size is 5MB.", * mimeTypesMessage = "Only the filetypes image are allowed." * ) */ protected $file;
Add all Getters & Setters and update the database schema with this command:
php app/console doctrine:schema:update
Next we need lifecycles . These are methods in Entity that are called on certain events. For example, the annotation @ORM\PreUpdate() before a method indicates that this method is called immediately before updating the entity.
public function preUpload() { if (null !== $this->file) {
Before an object is saved or updated, this method is called. You can use it, for example, generate a unique file name.
public function removeUpload() { if ($file = $this->getAbsolutePath()) { unlink($file); } }
Called before an object is deleted. This gives you time to delete an image from your folders or register a message if you want.
public function upload() {
This is an important part where your file is actually moved to the correct directory. Please note that I used some additional methods. You can get them from official white papers .
The next thing you need is a form. The form class itself is very simple. Just make sure you set the default data_class as follows:
public function setDefaultOptions(OptionsResolverInterface $resolver) { $resolver->setDefaults( array( 'data_class' => 'FSchubert\SiyabongaBundle\Entity\Image', ) ); }
A file upload field can be created very easily in the buildForm() method:
$builder->add('file', 'file');
The methods for your Controller bit long for just pasting them here, and IMHO is not part of the answer to your question. There are many examples for writing the right Controller Action for your purpose.
More things you should keep in mind:
- You need to grant your write permissions to the
app for the files to which you upload the files. Although it seems obvious, it can be annoying if you have several servers on which you are running the application. - There is
Image Constraint for your object. You can find it here . But since you were talking about downloading a file , I used File Constraint . - As I mentioned at the beginning of this post, there are many Bundles that handle all of these things for you. Check them out if you need an easy life.
Edit:
- Changed from
DoctrineExtensionsBundle to DoctrineBehaviours , since development on the old one was stopped in favor of the DoctrineBehaviours package.