ladybirdweb's avatar

How to convert an image into file object?

Here is a test file object

Illuminate\Http\Testing\File {#3132
  +name: "avatar.jpg"
  +tempFile: stream resource {@934
    timed_out: false
    blocked: true
    eof: false
    wrapper_type: "plainfile"
    stream_type: "STDIO"
    mode: "r+b"
    unread_bytes: 0
    seekable: true
    uri: "/private/var/folders/t1/yrwk61ln3c9dsy3cvdxw0z440000gn/T/phphMWhlK"
    options: []
  }
  +sizeToReport: null
  -test: true
  -originalName: "avatar.jpg"
  -mimeType: "image/jpeg"
  -size: 70
  -error: 0
  #hashName: null
  path: "/private/var/folders/t1/yrwk61ln3c9dsy3cvdxw0z440000gn/T"
  filename: "phphMWhlK"
  basename: "phphMWhlK"
  pathname: "/private/var/folders/t1/yrwk61ln3c9dsy3cvdxw0z440000gn/T/phphMWhlK"
  extension: ""
  realPath: "/private/var/folders/t1/yrwk61ln3c9dsy3cvdxw0z440000gn/T/phphMWhlK"
  aTime: 2018-10-23 06:51:23
  mTime: 2018-10-23 06:51:23
  cTime: 2018-10-23 06:51:23
  inode: 31382590
  size: 70
  perms: 0100600
  owner: 501
  group: 20
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
}

Now as soon as I move this image to a specific location, it is no longer a file object. If I try to get the image, I get that as binary. How can I convert the image into same old image object after it is uploaded?

0 likes
4 replies
Borisu's avatar

@ladybirdweb yes that's the problem. You're using the php function, which will get you the contents (it's in the name hehe). To get an object you'll have to load it through your File class. Something like this should be enough:

$file = new File($pathToFile);

Please or to participate in this conversation.