XF 2.2 Equivalent xf syntax to upload images

FoxSecrets

Active member
Is there any XF element equivalent to xf syntax <xf:textboxrow> to upload images?

If so, how can I read and delete the uploaded file?

If not, what's the element used to upload images?

Cheers
 
Is there any XF element equivalent to xf syntax <xf:textboxrow> to upload images?
HTML:
<xf:uploadrow name="some_input" accept=".gif,.jpeg,.jpg,.jpe,.png"
    label="Some label" />

If so, how can I read and delete the uploaded file?
The temporary file is available via the request object. which returns a \XF\Http\Upload object. You are responsible for persisting (or not persisting) the file in whatever manner you see fit:

PHP:
$upload = $this->request->getFile('some_input');
 
Last edited:
Nice!! In the request, what does 'upload' term mean? would be the file name?
And what's the path the file is uploaded? to retrieve in <img> tag

You said temporary, but how to persist then? I'm still understanding the process

PHP:
$upload = $this->request->getFile('upload');
 
Last edited:
Sorry, that should have corresponded to the input name:

PHP:
$upload = $this->request->getFile('some_input');

And what's the path the file is uploaded? to retrieve in <img> tag?
It's uploaded to the temporary directory and not publicly accessible. You would need to persist it somewhere (like the data:// mount).
 
Sorry, that should have corresponded to the input name:

PHP:
$upload = $this->request->getFile('some_input');


It's uploaded to the temporary directory and not publicly accessible. You would need to persist it somewhere (like the data:// mount).
Can you explain me better how to persist it? Is there any XF documentation about it?
With some practical examples if possible, I'm new to this
 
This is what I'm trying to do. Upload an image (or a batch), then show them below.
Still no success.

Code:
<xf:form action="{{ link('my-route/save', $info_id }}" upload="true" class="block" ajax="1">
  <xf:uploadrow label="Image" accept=".jpg,.jpeg,.png" name="image" id="image" value="{{ $info.image }}" required="true" />
  <xf:submitrow submit="Save" fa="fa-save" icon="" />
</xf:form>

<xf:datalist>
  <xf:foreach loop="$infos" key="$infoId" value="$info">
    <xf:datarow rowtype="current">
      <xf:cell>{$info.info_id}</xf:cell>
      <xf:cell>{$info.image}</xf:cell>
    </xf:datarow>
  </xf:foreach>
</xf:datalist>
 
I don't think there's comprehensive documentation about it. I would recommend picking a basic example from the core, like avatars (start at \XF\Pub\Controller\Account::actionAvatar), and trace through the code to get a good sense of it.

The gist of it is something like:
PHP:
$upload->requireImage();

if (!$upload->isValid($errors))
{
    $error = reset($errors);
    return $this->error($error);
}

$somePath = 'data://your/addon/some/path.jpg'; // your path can use an entity ID or similar
\XF\Util\File::copyFileToAbstractedPath($upload->getTempFile(), $somePath);

$url = \XF::app()->applyExternalDataUrl('your/addon/some/path.jpg'); // for the public URL

Though this is usually weaved together by a controller, service, and a couple of entity methods/getters.
 
Back
Top Bottom