asprin
Active member
My file upload code is working fine, first hurdle passed. Now onto the second one......
I'm uploading it in the
Now when trying to use this path in template, what would be the correct approach of getting the fully qualified URL? For now I'm hardcoding it like this:
Finally, inside the template
So this brings me to:
I'm uploading it in the
data://
directory using the following code:
PHP:
$uploads = $this->request->getFile('upload', false, false);
if($uploads)
{
$uploads->requireImage();
if (!$uploads->isValid($errors))
{
return $this->error($errors[0]);
}
$dataDir = \XF::app()->config('externalDataPath');
$datDir .= "://foo/bar/xyz.png";
\XF\Util\File::copyFileToAbstractedPath($uploads->getTempFile(), $dataDir);
// this is moving the file to <root>/data/foo/bar/xyz.png
}
Now when trying to use this path in template, what would be the correct approach of getting the fully qualified URL? For now I'm hardcoding it like this:
PHP:
// public controller
$fileid = 'xyz.png'; // this is pulled from database record
$path = 'data/foo/bar'; // this is the hardcoded stuff AND also I'm using relative path here
$fullpath = $path.'/'.$fileid;
$viewParms = [
'url' => $fullpath
];
return $this->view('', 'asp_foobar', $viewParms);
Finally, inside the template
asp_foobar
:
HTML:
<img src="{$url}" alt="Logo" />
So this brings me to:
- Is this approach fine as far as best practices are concerned?
- If no, is there any helper function in XF that will get me the folder path without having to do some sort of hardcoding?
- Or at least something that will help me get to the
data
folder in the file system?