uploadcare/uploadcare-php

Allow to set file filename during upload

markvaneijk opened this issue · 18 comments

I can't seem to find how to set the filename for the uploaded file using this library? Using the API it's possible, but not using this package it seems..

Uploadcare\File()->data contains the properties fetched from API
You should be able to do the following:

$file = $api->getFile($uuid);
$fn = $file->data['original_filename'];

I actually mean when uploading via the uploader class using the 'fromContents' or 'fromPath' methods, I can't provide the original filename.

So not fetching a file already uploaded to Uploadcare but uploading a file with an filename to Uploadcare.

gotcha.
but what's up with issue title?

also, can you please elaborate on a use case when you need to override a filename?

Weird thing about the title, but now it's better :)

The thing is this..

When I upload using the widget on my Uploadcare account I get this as a result, which is what I would like to see:

Screenshot 2019-08-22 at 17 16 19

But when I upload the same file via the PHP package (where I can't set the filename as far as I can see), I get this:

Screenshot 2019-08-22 at 17 17 15

So the file does not have an extension and I can't seem to provide an alternative filename. This way I also can't download/view the file as an PDF using the CDN link from Uploadcare.

FYI, you can use anything as a filename, when using CDN. Just add what you want after the last slash:
https://uploadcare.com/docs/delivery/file_api/#cdn-filename

Yeah, I did try that..

For example this file: https://ucarecdn.com/b73e4b60-a961-4275-b118-e40e76565480/
Custom filename in URL: https://ucarecdn.com/b73e4b60-a961-4275-b118-e40e76565480/example.pdf

The above does not work in Google Chrome:

Screenshot 2019-08-22 at 17 24 59

It works better in Safari, where I can at least download the file and it recognises that it's a PDF. But the download filename is still the 'ucrHEGHoQ.pdf' instead of 'example.pdf'

it obviously works :) Not sure what content you're having in that doc that makes chrome show the warning. Or some settings in Chrome itself.

curl -I https://ucarecdn.com/b73e4b60-a961-4275-b118-e40e76565480/example.pdf
HTTP/1.1 200 OK
Accept-Ranges: bytes
Access-Control-Allow-Methods: HEAD, GET, OPTIONS
Access-Control-Allow-Origin: *
Content-Disposition: attachment; filename=ucrHEGHoQ
Content-Type: application/pdf
ETag: "c5c8936d98e5083c1ab27ea9bf2e77b4"
Last-Modified: Thu, 22 Aug 2019 15:17:15 GMT
Server: nginx
Content-Length: 4345
Cache-Control: public, max-age=31556513
Date: Thu, 22 Aug 2019 16:08:27 GMT
Connection: keep-alive

The fact that header is not taking "filename" part into account should be fixed on backend side.
Meanwhile, feel free to send a PR that adds "filename" argument to uploading method.

I will create a PR for the addition of the filename when uploading.

When do you think the backend is updated? Currently I see the same situation here:

Screenshot 2019-08-23 at 13 52 50

I can't say when it will be addressed at all, unfortunately.

Oh ok, I thought you meant that you've changed something on the backend when you said:

"The fact that header is not taking "filename" part into account should be fixed on backend side."

Or do you mean you don't know when it will be live?

This is something that has to be done in CDN's origin code.
I can't say when it will be done.

I think this solves this issue #116

This is something that has to be done in CDN's origin code.
I can't say when it will be done.

Ok, with my pull request the biggest problem on this is solved by passing in a correct filename on upload using the API. So I can make sure on before hand that the filename and extension is correct.

It would be nice though to be able to dynamically change the filename via the URL, which was suggested as a feature, but this is a little bit broken then for now.

Addressed in #116

Closed in #116