Are you a Magento developer who have accidentally deleted some part of the /media folder Magento installation on the server? So the point is find out the way to recover image without affecting the database.
Is there any probability of deleting media folder ?
Well ….As you get your moves in the new platform, you may be pleasantly surprised to discover how similar the line-by-line code you’ll write really is. Magento2 has completely different architecture. The way Magento2 execute is totally different from Magento1.9x. So the real objective of this article is to give you an overview of the most significant differences from Magento 1 that you’ll encounter, basically digging the media.
THE PUB DIRECTORY AND CODE GENERATION
Automated code generation and deployment play a huge role in Magento 2, and these can be partially understood with a look at the pub directory. Static view file deployment materializes all necessary static assets from their source in application code into the web-accessible pub directory. This deployment process happens automatically in development mode but requires an explicit action in production. This automatic deployment of assets is what allows all JS/CSS code to live in its appropriate module but still be web-accessible in a centralized location. Code generation doesn’t involve the pub directory, but it’s closely related to the concept of deployment. Certain types of PHP classes are now automatically generated by Magento and facilitate more granular and unobtrusive customization than has been possible in the past.
While developing an online store in Magento2, the developer often needs to clear pre-compiled static data from pub directory. So there is a huge chance to accidently delete media directory from pub folder.
So, the scenario is what should be the next step after deleting the media directory? If the is the backup then great restore from there if not then what? Yeah…you upload one by one for all the product…if products are in big numbers, then how embracing will be upload image one by one?
How to recover image?
So, the problem is media directory is deleted but the path for existing product image are available in the database, now we have to import the image with same name and location. Our approach is to read the image file from your backup (provide image name with product-SKU or any specific pattern) and find the path for you each product and then rename image and insert that specific path.
For example, there is a product with SKU PR10001 have image path like /p/r/test-product.jpg. We search the image in image backup directory and save this file as the specified path for the product.
Basically, we have gone through this situation and we have prepared a Magento script for the same. Here is our Recover Image GitHub repository link.
Conclusion
If you accidently delete the media directory in Magento, please don’t be panic use our script to recover image for your Magento products. If you have any custom requirement feel free to contact us. Glad to hear from you.