[英]CodeIgniter base_url() for href and getcwd() for file_exists()
How do I generalise or neutralise the difference between base_url()
and getcwd()
? 如何概括或抵消
base_url()
和getcwd()
之间的区别?
In a view, I want to display both an inline PDF-viewer (using the <object src=''></object>
) and the OCR'ed output. 在视图中,我想同时显示嵌入式PDF查看器(使用
<object src=''></object>
)和OCR输出。 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). 两者都需要相同的源文件,该文件位于
project/public/corpus/<some_id>/file.pdf
(其中project
是CodeIgniter根目录)。
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>
. 问题是,当使用
base_url()
,函数file_exists($src)
总是会失败,但是为HTML <object>
生成src
属性时, getcwd()
连接会失败。
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). 我看到
base_url()
产生了一个类似于URL的“真实”字符串(“ http:// localhost / ”等),而getcwd()
产生了机器上的实际路径(“ D:\\ wamp \\ www”等)。就我而言,在Windows 10上使用WAMP,这是不理想的)。
Is there any way to generalise this statement so that I don't need two separate paths concatenations? 有什么方法可以概括该语句,这样就不需要两个单独的路径串联了吗? Am I doing this all wrong?
我做错了吗?
You do need two because one is a path and the other a URL. 您确实需要两个,因为一个是路径,另一个是URL。 But it could be a bit more DRY.
但这可能会更干燥。
This answer makes use of a handy constant defined in index.php
- FCPATH
. 这个答案利用了
index.php
- FCPATH
定义的一个方便的常量。 It is the path to the front controller (index.php) directory. 它是前端控制器(index.php)目录的路径。 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;
for $object_src variable you have to used baseurl() function in below format 对于$ object_src变量,您必须使用以下格式的baseurl()函数
base_url()."folderpath/file.pdf" base_url()。“文件夹路径/文件.pdf”
where folderpath is the path from your server root directory say www. 其中folderpath是服务器根目录中的路径,例如www。
eg. 例如。 if your server path of pdf file is /var/www/public/mi-corpus/file.pdf and your server root directory is /var/www then your object_src path will be below.
如果pdf文件的服务器路径为/var/www/public/mi-corpus/file.pdf,而服务器根目录为/ var / www,则object_src路径将在下面。
$object_src = base_url()."public/mi-corpus/file.pdf"; $ object_src = base_url()。“ public / mi-corpus / file.pdf”;
I hope this helps you in understanding the problem. 希望这有助于您理解问题。
thanks 谢谢
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.