SchoolPhysicalExamination/application/admin/controller/Execlaa.php

132 lines
4.6 KiB
PHP

<?php
namespace app\admin\controller;
use PhpOffice\PhpSpreadsheet\IOFactory;
use think\Controller;
class Execlaa extends Controller
{
public function readexcel()
{
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$publicPath = rtrim($rootPath, '/');
$filePath = '/tsf/2302.xls';
$directory = '/tsf/2302班'; // 替换为你的图片文件夹路径
// Excel文件路径
$excelPath = $publicPath . $filePath;
// dump($excelPath);
// die;
// 读取Excel文件
$spreadsheet = IOFactory::load($excelPath);
$worksheet = $spreadsheet->getActiveSheet();
// dump($spreadsheet);
// die;
// 获取数据
$data = [];
$highestRow = $worksheet->getHighestRow(); // 获取最高行数
$highestColumn = $worksheet->getHighestColumn(); // 获取最高列
for ($row = 2; $row <= $highestRow; $row++) { // 假设第一行是标题行
$rowData = [];
for ($col = 'A'; $col <= $highestColumn; $col++) {
$rowData[] = $worksheet->getCell($col . $row)->getValue();
}
$data[] = $rowData;
}
// 处理数据或返回数据
// 例如:输出数据到控制台
// foreach ($data as $row) {
// var_dump($row);
// }
// 或者你可以将数据保存到数据库或进行其他操作
// ...
// dump($data);
// die;
$picPath = $publicPath . $directory;
$tupian_arr = [];
// 打开目录
if ($handle = opendir($picPath)) {
// 遍历目录中的文件和子目录
while (false !== ($file = readdir($handle))) {
// 跳过当前目录和上级目录
if ($file != "." && $file != "..") {
// 检查文件扩展名是否为图片格式
$extension = pathinfo($file, PATHINFO_EXTENSION);
$imageExtensions = array('jpg', 'jpeg', 'png', 'gif', 'bmp'); // 添加你需要的图片格式
if (in_array(strtolower($extension), $imageExtensions)) {
// 输出图片文件名或进行其他处理
// echo "找到图片文件: " . $file . "<br>";
// 如果你想获取图片完整路径,可以这样做:
$fullPath = $picPath . '/' . $file;
// echo "图片完整路径: " . $fullPath . "<br>";
// dump($fullPath);
// dump($file);
// dump(pathinfo($file, PATHINFO_FILENAME));
// dump('~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~');
$tupian_arr[pathinfo($file, PATHINFO_FILENAME)] = $fullPath;
}
}
}
// 关闭目录
closedir($handle);
} else {
echo "无法打开目录 $directory";
}
foreach ($tupian_arr as $key => $value) {
for ($i=0; $i < count($data); $i++) {
if($data[$i][0] == $key){
$this->renameImage($value,$data[$i][4]);
break;
}
}
}
// dump($tupian_arr);
// dump($tupian_arr[1]);
// return json(['status' => 'success', 'data' => $data]); // 假设你想返回JSON响应
}
public function renameImage($filename,$newfn)
{
// 假设这是你的图片文件的当前路径
// $filename = './public/uploads/old_name.jpg';
// 构建新的文件名(确保它与目录中的其他文件不冲突)
$newfn = $newfn.'.jpg';
$rootPath = $_SERVER['DOCUMENT_ROOT'];
$publicPath = rtrim($rootPath, '/');
$newImagePath = $publicPath . '/tsf/demo/'.$newfn;
// $newImagePath = './public/uploads/' . $newFileName;
// dump($filename);
// dump($newImagePath);
// 使用rename()函数重命名文件
if (rename($filename, $newImagePath)) {
// 重命名成功,可以进行其他操作,如记录日志、返回成功消息等
echo '图片重命名成功!';
} else {
// 重命名失败,处理错误
echo '图片重命名失败,请检查路径和权限!';
}
}
}