Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import excel files with image in php/mysql

I want to make an import script which allows users to upload their excel file (extension not important) to my php application.

The application should reconize a list of items (so far so good).

The difficulty in this case is that the excel files contain images...

I've read information about phpexcel library but it does not say anything about images.

Anybody ideas?

like image 824
Marcel Avatar asked Jun 15 '10 14:06

Marcel


People also ask

How do I import Excel data into MySQL using PHP?

php use Phppot\DataSource; use PhpOffice\PhpSpreadsheet\Reader\Xlsx; require_once 'DataSource. php'; $db = new DataSource(); $conn = $db->getConnection(); require_once ('./vendor/autoload. php'); if (isset($_POST["import"])) { $allowedFileType = [ 'application/vnd. ms-excel', 'text/xls', 'text/xlsx', 'application/vnd.

How do I import a XLSX file into MySQL?

Step 1: Click on the Browse button and select the Excel file you want to import to MySQL. Step 2: Select MySQL as your desired database. According to your excel file, check or uncheck My File has a Header Row. Step 3: Based on your Excel file, check Use CHECK IF TABLE EXISTS.

Can we upload image in MySQL database?

Get the file extension using pathinfo() function in PHP and validate the file format to check whether the user selects an image file. Upload image to server using move_uploaded_file() function in PHP. Insert image file name in the MySQL database using PHP. Upload status will be shown to the user.


2 Answers

You can access images by PHPExcel library.

For importing images:

$objPHPExcel = PHPExcel_IOFactory::load("MyExcelFile.xls");

foreach ($objPHPExcel->getSheetByName("My Sheet")->getDrawingCollection() as $drawing) {
    if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
        ob_start();
        call_user_func(
            $drawing->getRenderingFunction(),
            $drawing->getImageResource()
        );
        $imageContents = ob_get_contents();
        ob_end_clean();
    }
}

Then $imageContents will contain the image data that you can output or save as a file etc. You can see this question: PHPExcel: How to insert an image in the first page header and enlarge it to fit it's content?

For accessing images:

ArrayObject of all the image objects in the active worksheet will return by:

$objPHPExcel->getActiveSheet()->getDrawingCollection() ;

These objects will be either PHPExcel_Worksheet_Drawing or PHPExcel_Worksheet_MemoryDrawing objects: you can identify which using is_a(). You can then use the methods appropriate to that class (as described in the API) either to read the image data from file (for PHPExcel_Worksheet_Drawing objects) or directly from the PHPExcel_Worksheet_MemoryDrawing object itself. The getName() and getDescription() methods can be used to retrieve the relevant values fro the image object.

Now it's also possible to have image objects associated with print headers:

$objPHPExcel->getActiveSheet()->getHeaderFooter()->getImages() 

can be used to retrieve images from the header/footer. This is an array of PHPExcel_Worksheet_HeaderFooterDrawing objects. All the PHPExcel_Worksheet_Drawing methods can be used to extract the image file from these objects.

like image 190
Somnath Muluk Avatar answered Oct 04 '22 05:10

Somnath Muluk


To import data from Excel (or any other program that can produce a text file) is very simple using the LOAD DATA command from the MySQL Command prompt.

1) Save your Excel data as a csv file (In Excel 2007 using Save As) Check the saved file using a text editor such as Notepad to see what it actually looks like, i.e. what delimiter was used etc.

2) LOAD DATA LOCAL INFILE ‘C:\temp\yourfile.csv’ INTO TABLE database.table FIELDS TERMINATED BY ‘;’ ENCLOSED BY ‘”‘ LINES TERMINATED BY ‘\r\n’ (field1, field2);

3) To store image to have to use TO_BASE64

Done!

Reference

like image 24
Angelin Nadar Avatar answered Oct 04 '22 05:10

Angelin Nadar