如何使用Excel工作表中的路径上传批量图像

时间:2017-04-25 06:25:23

标签: php excel

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++;

}

?>
  • 当用户上传.csv文件时,读取文件路径将图像移动到特定目录

Home Page

enter image description here

Sample Excel File

enter image description here

2 个答案:

答案 0 :(得分:0)

有一个名为PHPExcel的精彩库,您可以使用它来获取Excel工作表(或CSV文件)中的所有路径。您可以很好地使用此库处理CSV文件。这里有一个代码示例:

$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

答案 1 :(得分:0)

由于您有图像路径,因此您不需要使用tmp_name或name变量,而是可以使用:

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

拷贝($文件,&#34; $位置/ $ FILE_NAME&#34);