簡體   English   中英

Laravel上傳圖片由excel

[英]Laravel Upload image by excel

您好朋友,我想使用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
    }
}

希望這個能對您有所幫助

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM