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?
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;
for $object_src variable you have to used baseurl() function in below format
base_url()."folderpath/file.pdf"
where folderpath is the path from your server root directory say 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.
$object_src = base_url()."public/mi-corpus/file.pdf";
I hope this helps you in understanding the problem.
thanks