Search code examples
phpimportbulk

How to import images in a bulk import function?


I am working on a Bulk Import function in PHP for products table. I want to know how do you import images for the products? Do it through csv or upload it externally some other way?

I have worked on importing all the other columns from a csv file. Just image column is left.

EDIT: I dont want code. I am just asking how would you prefer to do it? Would you upload images with CSV or some other media upload implementation?

Thanks.


Solution

  • You can use as bellow script for CSV

    $row = 1;
    $counter = 0;
    $urls = array();
    
    $desdir = "Destination_DIR_URL";
    //Ex:  250x250/ for current directory, create the directory with named 250x250
    if (($handle = fopen("targetfile.csv", "r")) !== FALSE) {
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    $num = count($data);
    $counter++;
    
    $row++;
    
    if($counter > 1 && is_array($data) && sizeof($data) > 3){
    
        $tmppos = strpos($data[3]."","Source_Folder_Url");
    
        if($tmppos === 0){
            $file = $data[3];
            $farry = explode("/",$file);
            $filename = end($farry);
            //echo $filename."<br>";
            $urls[$counter] = $file;
    
            //$current = file_get_contents($file);
            if(!file_exists($desdir.$filename)){
                copy($file,$desdir.$filename);
            }
        }
    
      }
    
    }
     fclose($handle);
    }