【问题标题】:An uncaught Exception was encountered Type: ImagickException Message: Failed to read the file in Codeigniter遇到未捕获的异常类型:ImagickException 消息:无法读取 Codeigniter 中的文件
【发布时间】:2018-08-04 16:00:41
【问题描述】:

以下php pdf to image code with imagick in codeigniter framework有问题,imagick无法读取我的pdf文件。

错误:

[codeigniter] 遇到未捕获的异常类型:ImagickException 消息:无法读取 Codeigniter 中的文件。

控制器:

<?php defined('BASEPATH') OR exit('No direct script access allowed');
class Files_upload extends CI_Controller {
    function  __construct() {
        parent::__construct();
        $this->load->model('files');
    }    

    function index(){
        $data['gallery'] = $this->db->query("select * from gallery order by id desc limit 10")->result();
        $data = array();
        if($this->input->post('submitForm') && !empty($_FILES['upload_Files']['name'])){
            $filesCount = count($_FILES['upload_Files']['name']);
            for($i = 0; $i < $filesCount; $i++){
                $_FILES['upload_File']['name'] = $_FILES['upload_Files']['name'][$i];
                $_FILES['upload_File']['type'] = $_FILES['upload_Files']['type'][$i];
                $_FILES['upload_File']['tmp_name'] = $_FILES['upload_Files']['tmp_name'][$i];
                $_FILES['upload_File']['error'] = $_FILES['upload_Files']['error'][$i];
                $_FILES['upload_File']['size'] = $_FILES['upload_Files']['size'][$i];
                $uploadPath = 'uploads/files/';
                $config['upload_path'] = $uploadPath;
                $config['allowed_types'] = 'gif|jpg|png|pdf|mp4|avi';                
                $this->load->library('upload', $config);
                $this->upload->initialize($config);
                if($this->upload->do_upload('upload_File')){
                    $fileData = $this->upload->data();
                    $uploadData[$i]['file_name'] = $fileData['file_name'];
                    $uploadData[$i]['created'] = date("Y-m-d H:i:s");
                    $uploadData[$i]['modified'] = date("Y-m-d H:i:s");
                }
            }            
            if(!empty($uploadData)){
                //Insert file information into the database
                $insert = $this->files->insert($uploadData);
                $statusMsg = $insert?'Files uploaded successfully.':'Some problem occurred, please try again.';
                $this->session->set_flashdata('statusMsg',$statusMsg);
            }

            $this->load->helper('url');

            $ImageName      = $_FILES['upload_File']['name'];
            $loc =  base_url().$uploadPath.$ImageName;   

            echo $ImageName;
            echo $loc;                                

            $im = new imagick($loc);
            $noOfPagesInPDF = $im->getNumberImages(); 

            if ($noOfPagesInPDF) { 
                for ($i = 0; $i < 1; $i++) { 
                    $url = $loc.'['.$i.']'; 
                    $image = new Imagick($url);
                    $image->setImageFormat("jpg"); 
                    $image->setImageCompressionQuality(80); 
                    $image->writeImage("uploads/files/img/".($i+1).'-'.$ImageName.'.jpg'); 
                }
            }
            for($i = 0; $i<1;$i++) {
                $img = "uploads/files/img/".($i+1).'-'.$ImageName.'.jpg';
                $display .= "<img src='$img' title='Page-$i' /><br>";            
            }
            $message = "PDF converted to JPEG sucessfully!!";
        }
        //Get files data from database
        $data['gallery'] = $this->files->getRows();
        //Pass the files data to view
        $this->load->view('files_upload/index', $data);
    }
}

已解决

是正确的代码

$ImageName= $fileData['file_name'];
$loc =  realpath(APPPATH.'../uploads/files/').'/'.$ImageName;

或者你可以这样做

$loc = $fileData['full_path'];

【问题讨论】:

  • 指的是哪个imagick? imagick($loc); 还是循环中的那个?无论如何,您都无法提供 imagick 的 URL。它必须是一条真实的路径。
  • 循环只是取page-0的值,如果不能使用url怎么获取real_path?
  • ./uploads/files/$image_name ... 因为您的代码似乎处理文件,您应该研究路径 abs vs real vs url。它的关键知识
  • @Alex 错误兄弟 => Type: ImagickException Message: UnableToOpenBlob './uploads/files/tes.pdf': No such file or directory @ error/blob.c/OpenBlob/3315
  • 继续查看uploads 文件夹中的htdocs 文件夹,然后查看files 文件夹。有没有叫做tes的pdf?看起来不像。从字面上看,这就是它的意思

标签: php codeigniter


【解决方案1】:

这将工作(测试):

    //$data['gallery'] = $this->db->query("select * from gallery order by id desc limit 10")->result(); // same at bottom??
    $message = '';
    $display = '';
    if ($this->input->post('submitForm') && !empty($_FILES['userfile']['name'])) {
        $uploadPath = FCPATH . 'uploads/files/';
        if (!is_dir($uploadPath) && mkdir($uploadPath, DIR_WRITE_MODE, true) == false) {
            show_error('Folder cannot be made!');
        }
        $config['upload_path'] = $uploadPath;
        $config['allowed_types'] = 'gif|jpg|png|pdf|mp4|avi';
        $this->load->library('upload', $config);
        // change input field to <input type="file" name="userfile">
        if (!$this->upload->do_upload()) {
            $this->session->set_flashdata('statusMsg', $this->upload->display_errors());
        } else {
            $fileData = $this->upload->data();
            $uploadData['file_name'] = $fileData['file_name'];
            $uploadData['created'] = date("Y-m-d H:i:s");
            $uploadData['modified'] = date("Y-m-d H:i:s");
            $insert = $this->files->insert($uploadData);
            $insert = true;
            if (!$insert) {
                @unlink($fileData['full_path']); // remove orphan
                $this->session->set_flashdata('statusMsg', 'Database error. Please try again');
            } else {
                $this->session->set_flashdata('statusMsg', 'Files uploaded successfully.');
                if ($fileData['file_ext'] == '.pdf') {
                    try {
                        $newPath = $uploadPath . 'img/';
                        if (!is_dir($newPath) && mkdir($newPath, DIR_WRITE_MODE, true) == false) {
                            throw new Exception('Folder cannot be made!');
                        }
                        $ImageName = $fileData['raw_name'];
                        $loc = $fileData['full_path'];
                        $im = new Imagick($loc);
                        $pdfPageCount = $im->getNumberImages();
                        if ($pdfPageCount > 0) {
                            for ($i = 0; $i < $pdfPageCount; $i++) {
                                $url = $loc . '[' . $i . ']';
                                $image = new Imagick($url);
                                $image->setImageFormat("jpg");
                                $image->setImageCompressionQuality(80);
                                $image->writeImage($newPath . ($i + 1) . '-' . $ImageName . '.jpg');
                                $img = base_url("uploads/files/img/" . ($i + 1) . '-' . $ImageName . '.jpg');
                                $display .= "<img src='$img' title='Page-$i' /><br>";
                            }
                            echo $display; // debugging
                            $this->session->set_flashdata('statusMsg', "PDF converted to JPEG(s) sucessfully!");
                        }
                    } catch (Exception $e) {
                        @unlink($fileData['full_path']); // remove orphan
                        $this->session->set_flashdata('statusMsg', $e->getMessage());
                    }
                } else {
                    echo 'not a pdf'; // debugging only
                }
            }
        }
    }
    //Get files data from database
    $data['gallery'] = $this->files->getRows();
    //Pass the files data to view
    $this->load->view('files_upload/index', $data);

请注意,输入文件字段现在应如下所示:

&lt;input type="file" name="userfile" /&gt;

您还必须修改:

$this-&gt;files-&gt;insert($uploadData)函数

【讨论】:

  • 谢谢你,亚历克斯,我已经得到了我的问题的答案,很高兴感谢你的回答。
  • 我建议你看看以if ($noOfPagesInPDF) { 开头的代码并与我的比较。您缺少我在回答中提到的逻辑,这意味着只有 1 页的 pdf 会被成像。您的代码还允许除 pdf 之外的其他类型,但无论如何我已经解决了它试图对其进行成像。感谢您看一看并为我的时间点赞
猜你喜欢
  • 2014-09-09
  • 2016-05-19
  • 1970-01-01
  • 2014-08-09
  • 1970-01-01
  • 2015-02-27
  • 2018-06-12
  • 1970-01-01
  • 2015-04-18
相关资源
最近更新 更多