0
index.html
    <html>
        <head>
                <title>Excel Upload</title>
        </head> 
            < body >
        <center><h2>Excel Upload</h2></center> 
       <form action = "upload.php" method = "POST"  enctype = "multipart/form-data" >
        <input type="file" name="file"/> < input type = "submit" value = "Upload" >
  < /form>
</body > 
< /html>

upload.php

<?php
$name = $_FILES['file']['name'];
$file = $_FILES['file']['tmp_name'];
$handle = fopen($file, "r");
$c = 0;
$location ="images"
while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
{

 $file = $filesop[0];
 $file_name = $file[0]['name'];
 $file_tmp_name = $file[0]['tmp_name'];
 move_uploaded_file($file_tmp_name,"$location/$file_name");
 $c++;

}

?>
  • When User Upload the .csv file read the file path move the image to particular Directory

Home Page

enter image description here

Sample Excel File

enter image description here

Tobias F.
  • 1,050
  • 1
  • 11
  • 23

2 Answers2

0

There is a wonderful library called PHPExcel which you can use to get all of the paths in your excel sheet (or CSV file). You can handle CSV files with this library quite well. Here you have a code example:

$filepath  = '/path/to/your/csv.csv';
$phpExcelFileType = PHPExcel_IOFactory::identify($filepath);
$reader = PHPExcel_IOFactory::createReader($phpExcelFileType);
$objPHPExcel = $reader->load($filepath); // This object represents your whole excel file

$highestRow = $objPHPExcel->getActiveSheet()->getHighestRow();
// Array to hold all of your image paths
$imagePaths = [];

// Loop through all rows in column A

for ($row = 1; $row < $highestRow; $row++){
  $currentCell = $objPHPExcel->getActiveSheet()->getCellByColumnAndRow(0, $row);
  $imagePaths[] = $currentCell()->getValue(); // this is the path to the image you want
}

// Do with the paths in $imagePaths whatever you want
Tobias F.
  • 1,050
  • 1
  • 11
  • 23
  • But I need to move original source (C:\Users\Infizoom 3\Pictures\1.PNG) in image folder @tobias-f –  Apr 25 '17 at 06:38
  • It is much easier to use PHPExcel because you don't have manually split the content of the CSV file into rows, cells etc. Also it supports using not only csv, but also xls, xslx and some more file types. – Tobias F. Apr 25 '17 at 06:54
  • I read the excel data but their possible to move the image to a particular directory @tobias-f –  Apr 25 '17 at 07:03
  • For moving files you can use [rename](http://php.net/manual/en/function.rename.php), as explained in [this](http://stackoverflow.com/a/19139524/7008354) SO question. If you just want to create a copy of the file elsewhere, but keep the original file where it is you can use [copy](http://php.net/copy). – Tobias F. Apr 25 '17 at 07:07
  • For Example, I Enter the particular image path in text field the image will be pasted to my own location or some folder (image) –  Apr 25 '17 at 07:23
  • After that, all the path enter to excel sheet same process to save the images to some folders –  Apr 25 '17 at 07:25
  • I see what your problem is; You use `move_uploaded_file`, but this would only move the csv file you uploaded. It won't work for the paths which are just simple strings **within** the file you uploaded. So use `copy` or `rename` instead. – Tobias F. Apr 25 '17 at 07:28
0

As you have image path so you don't need to use tmp_name or name variable instead you can use like:

$file = $filesop[0];
$file_name = pathinfo($file,PATHINFO_BASENAME);
move_uploaded_file($file,"$location/$file_name");

or

copy($file,"$location/$file_name");

Ahmed Ginani
  • 6,522
  • 2
  • 15
  • 33
  • I run this code only show the image name can't move the directory Sir. echo $file_name = pathinfo($file,PATHINFO_BASENAME); output:1.PNG –  Apr 25 '17 at 07:05
  • As you can see we are using that code for file name. and moving $file to your given location. just check location path and given path in csv is correct.. and check permission of moving location – Ahmed Ginani Apr 25 '17 at 07:20
  • $location ="images"; $file = "d:/images/1.png"; move_uploaded_file($file,"$location"); –  Apr 25 '17 at 08:16
  • so what problem here? – Ahmed Ginani Apr 25 '17 at 08:34