#Get a File object from an Object

14 messages · Page 1 of 1 (latest)

haughty harbor
#

hello devs, I have this object I get from an api

  "title": "tere",
  "price": "43",
  "guestNumber": "3",
  "details": "dsdfsdf",
  "hotelId": 2,
  "image": "C:\\fakepath\\happy-bg.jpeg",
  "fileSource": {}
}```

​and fileSource is this 
```fileSource: File { name: "happy-bg.jpeg", lastModified: 1659786869118, size: 62310, … }
​​
lastModified: 1659786869118
​​
name: "happy-bg.jpeg"
​​
size: 62310
​​
type: "image/jpeg"```

I have tried all possible ways to access the File without prevailing.
main shard
#

Well, where is the file located ?

haughty harbor
#

@main shard I am getting it from an api and it is within the main object let me call it resource

main shard
#

sure, but either it is being streamed to you in the resource object, or all you have is a file path, and the file path may not be relevant to the machine that is processing the next function to load the file....

haughty harbor
#

awww. I don't know what to do in this case. But it shows from the response that it's a File that is being gotten.

main shard
#

well, not specifically. what you've shown is you have some json that describes a file. so there is either another element within that contains the file (like a base64 encoded binary object?), or there is another API endpoint to get that file stream so you can then do something with it, or it is expected that you will take the details and load the file directly, at which point where the file is actually stored will be relevant.

haughty harbor
#

@main shard ahh I understand. What I get is just the file information and not the actual file. 😢

main shard
#

Is it your API you are hitting ?

haughty harbor
#

@main shard yes. Angular front-end

main shard
#

so this is posting to an API ?

haughty harbor
#

yes

main shard
#

ok, so you should be able to reference it with $source_file=$_FILES['file']['name']; to get the file.

haughty harbor
#

@main shard now I get undefined array key "file"

main shard
#

you might need to use another key based on your <input> element name. But usually you would just scan through the $_FILES array to extract any input of type file.