-10

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.

Waleed
  • 1,097
  • 18
  • 45
  • 6
    You are expected to try to **write the code yourself**. After [**doing more research**](https://meta.stackoverflow.com/q/261592/1011527) if you have a problem **post what you've tried** with a **clear explanation of what isn't working** and provide [a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). Read [How to Ask](http://stackoverflow.com/help/how-to-ask) a good question. Be sure to [take the tour](http://stackoverflow.com/tour) and read [this](https://meta.stackoverflow.com/q/347937/1011527). – Jay Blanchard Jun 20 '17 at 11:51
  • 1
    *"Just want to know your way."* - To me, that is opinion-based. You may not like "my" way. This as per the original post https://stackoverflow.com/revisions/44652273/1 – Funk Forty Niner Jun 20 '17 at 11:51
  • just updated my question for quick heads. – Waleed Jun 20 '17 at 11:52
  • 1
    I had to roll back your question. You can keep the "FFS" to yourself; we know what that means. – Funk Forty Niner Jun 20 '17 at 11:52
  • Well, you couldn't understand what I was asking so I put it there. Removed and updated. – Waleed Jun 20 '17 at 11:53
  • 1
    *"Well, you couldn't understand what I was asking"* - I did and the question is both too broad and opinion-based. – Funk Forty Niner Jun 20 '17 at 11:54
  • care to share 1 ? – Waleed Jun 20 '17 at 11:54
  • 1
    There are ample examples/tutorials and official manuals on the web. Run a benchmark also and see what works the fastest. – Funk Forty Niner Jun 20 '17 at 11:54

5 Answers5

4

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);
}
Create Explorer
  • 357
  • 2
  • 20
3

There are two ways to store images:

  1. As files on a server and the path to the file is stored in the database
  2. Whole image is stored in the database

In most projects images are stored on the server and only paths are stored in the database. This is an old discussion you can read about here: Storing Images in DB - Yea or Nay?

If you have a .csv with the whole images stored in a text column (this is pretty unlikely) you may set up a TEXT or a BLOB column in your database.

The recommended way is to store images as files on the server. Therefore what you need in your project is a .csv column containing one or more image names or image paths.

In this case you have to seperate the process of importing data and importing files somehow.

However you can all do it within one import action for the user this is one example:

<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="mycsv" />
<input type="file" name="images[]" multiple="multiple" />
</form>

You also could upload your files via ftp or use a file upload library this entirely depdends on the use case and on how big amounts of data you have to handle.

If you have a large amount of data (e.g. more than 100 products per .csv file) the process I recommend is like this:

  1. Import your .csv into the database including image names or paths (within your upload script you can alter or extend the image path)

  2. Copy your images to the server (e.g. via FTP) or via a seperate upload script - just make sure the image names / paths are matching

Blackbam
  • 17,496
  • 26
  • 97
  • 150
1

You have two option: 1: put image path to your csv and import csv data to database and upload images to respective path using FTP. 2: put image path to your csv and process each row programmatically and upload each image using code.

Each method has its own pros and cons. For method 1: You can import product in very short time but you need to upload images manually to server and don't have any status report for missing images. For method 2: You can import product product with images with status report for each row but you need to write much more code.

Now, its your choice which method you want to use. Your choice depends on how frequently you need to do this. You should select first option if you need to do this once a while in a moon, otherwise you should go with second option.

You can also store images in database using blog data type, but its not advisable. If you have good number of product, that might cause many issues with database performance.

Virendra Jadeja
  • 821
  • 1
  • 10
  • 20
1

You need to upload all the images on the server or take a path from where you want to fetch the image it can be on any server except your local machine

and state the same name in the CSV that you going to upload in the application.

What you need to do in the code is to provide the path of the uploaded location of the image take the name from the uploaded CSV.

Then you can uploaded the images to the desired location and save the name in the database as desired.

0

It depends also in context of what you need for your application. If you worry more on security than convenience save them into a database as blog files. If you are more concerned of performance go for directory into the server.

Gi1ber7
  • 632
  • 1
  • 11
  • 22