gpt4 book ai didi

php - Laravel 通过 excel 上传图片

转载 作者:行者123 更新时间:2023-12-04 20:56:32 25 4
gpt4 key购买 nike

你好 friend 我想通过 laravel 中的 excel 使用 laravel excel 插件上传学生数据,我想用 excel 文件上传图像(通过本地路径或其他)
请建议我解决这个问题

我创建了一个在我的本地服务器上上传文件但在服务器上不起作用的函数

$excel = Excel::load(Input::file('file'))->get();
foreach($excel as $exc) {
$data = $exc->all();
$data['class_id'] = $id;
$data['school_id'] = $class->school_id;
if ($student = Student::firstOrCreate($data)) {
if (File::exists($exc->image)) {
$destinationPath = 'uploads/schools/' . $class->school_id . '/' . $class->id . '/' . $student->id . '/'; // upload path
if (!file_exists($destinationPath)) {
mkdir($destinationPath, 0755, true);
}
$file = file_get_contents($exc->image);
$fileName = str_slug(Carbon::now()->toDayDateTimeString()) . rand(11111, 99999) . '.jpg'; // renameing image
if(file_put_contents($destinationPath.$fileName,$file)) {
$student->image = $destinationPath . $fileName;
$student->update();
}
}
}
}

最佳答案

$objphpexcel = PHPExcel_IOFactory::load("MyExcelFile.xls");

foreach ($objphpexcel ->getSheetByName("My Sheet1")->getDrawingCollection() as $drawing) {
if ($drawing instanceof PHPExcel_Worksheet_MemoryDrawing) {
ob_start();
call_user_func(
$drawing->getRenderingFunction(),
$drawing->getImageResource()
);
$imageContents = ob_get_contents();
ob_end_clean();
$cellID = $drawing->getCoordinates();
// .... do your save here
}
}

希望对你有帮助

关于php - Laravel 通过 excel 上传图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46488754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com