doufei6456 2016-10-18 14:52
浏览 36
已采纳

用于href的CodeIgniter base_url()和用于file_exists()的getcwd()

How do I generalise or neutralise the difference between base_url() and getcwd()?

In a view, I want to display both an inline PDF-viewer (using the <object src=''></object>) and the OCR'ed output. Both of these require the same source file, which is located in project/public/corpus/<some_id>/file.pdf (where project is the CodeIgniter root).

The code in the controller can be summarised like this:

    /**
     * Fetch the path to the pdf of the document (OCR is preferred)
     * $handle is a unique identifier
     */
    $object_src = base_url("public/mi-corpus/" . $handle . "/file.pdf");

    $ocr_src = getcwd()."/public/mi-corpus/" . $handle . "/file.pdf";

    // Returns an <object> node from the source
    $this->data['object'] = $this->get_pdf($ocr_src);

    // uses a cloud OCR to parse the source and return OCR'ed text, this is irrelevant. But it does use file_exists($src) to check if the path is valid.
    $this->data['ocr'] = $this->mipreprocessing->open_ocr($ocr_src);

The thing is, when using base_url(), the function file_exists($src) always fails, but the getcwd() concatenation fails when producing a src attribute for the HTML <object>.

I see that base_url() yields a 'real' URL-like string ("http://localhost/" etc.), whereas getcwd() yields the actual path on the machine ("D:\wamp\www" etc. in my case, using WAMP on Windows 10, I know, it's not ideal).

Is there any way to generalise this statement so that I don't need two separate paths concatenations? Am I doing this all wrong?

  • 写回答

2条回答 默认 最新

  • dongwei2610 2016-10-18 15:35
    关注

    You do need two because one is a path and the other a URL. But it could be a bit more DRY.

    This answer makes use of a handy constant defined in index.php - FCPATH. It is the path to the front controller (index.php) directory. Using the constant will make the code more portable, less error prone.

    $doc_location = "public/mi-corpus/" . $handle . "/file.pdf";
    $object_src = base_url($doc_location);
    $ocr_src = FCPATH.$doc_location;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器