Forum

Thread tagged as: Question

Rename file on upload

I want to allow an editor to upload a PDF and for this PDF to be renamed before saving to the server. Does Perch allow me to do this, and if so, how would I implement this?

Helen Forrest

Helen Forrest 0 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

Perch will preserve the file name, unless a file with that name already exists. You can't specify a third option, no.

Is there any way the uploaded file name can be captured and fed into a database?

Drew McLellan

Drew McLellan 2638 points
Perch Support

Yes, that happens. What are you trying to accomplish?

I have 3 areas on the website which allows for a regular upload of a PDF and there is a link to the PDF on the website page. My thinking was that if the PDF could be renamed on upload, the link reference could remain the same. As this isn't possible with Perch, my other idea was to save the PDF name to a database table which could then be queried and fed dynamically into the link.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

Hello Helen,

I have 3 areas on the website which allows for a regular upload of a PDF and there is a link to the PDF on the website page

Do you have a region on a page where an editor uploads a PDF and want to output a link to that PDF on another page?

Hi Helen

You can just output the path to the uploaded document and then you don't need to change the filename:

<a href="<perch:content id="document_file" type="file" label="Upload document" bucket="documents">"><perch:content id="document_title" type="text" label="Document title"></a>

Hussein Al Hammad said:

Do you have a region on a page where an editor uploads a PDF and want to output a link to that PDF on another page?

Yes, that is exactly what I want to do.

Hussein Al Hammad

Hussein Al Hammad 105 points
Registered Developer

If you have a region with the key My Region on the page /my-page.php:

perch_content('My Region');

You can output the same region on other pages with perch_content_custom() like so:

perch_content_custom('My Region', [
    'page' => '/my-page.php',
]);

You can also specify a different template if you need to display it differently on the other pages:

perch_content_custom('My Region', [
    'page' => '/my-page.php',
    'template' => 'template.html',
]);

That's brilliant. I'll have a go. Thank you so much!

Hussein Al Hammad said:

If you have a region with the key My Region on the page /my-page.php:

perch_content('My Region');

You can output the same region on other pages with perch_content_custom() like so:

perch_content_custom('My Region', [
  'page' => '/my-page.php',
]);

You can also specify a different template if you need to display it differently on the other pages:

perch_content_custom('My Region', [
  'page' => '/my-page.php',
  'template' => 'template.html',
]);

This worked a treat. Thank you Hussein