1

The goal:

I have a URL of an image. I would like to get the image file and to upload it to $_FILES as if the image was uploaded by a client.

The reason:

I need this because I have a form in which users can either upload an image file in a input type="file" field OR enter an image URL in a input type="text" field and later, when I validate the form, I want the validation function to access the file using $_FILES regardless the method the user chose to supply the image (either by uploading a file or by entering a file URL.)

The problem:

I found this answer on using file_put_contents() and file_get_contents() but I don't understand if I can use it to change $_FILES and if yes then how.

EDIT:

After reading David's comment, I understood my mistake. What I actually need is a way to download a file from a URL using file_get_contents() and file_put_contents() to the same temporary location on the server that uploaded files are downloaded to. By that I would be able to handle files the same way regardless of how they got there (URL or file upload).

Community
  • 1
  • 1
randvir
  • 63
  • 7
  • The answer you linked is clearly telling you to use `curl`. If you'll be using outsource image, you have to fetch it with curl and won't be needing to do anything with `$_FILES`, that just handles uploaded files. – al'ein Sep 30 '15 at 11:45
  • 2
    It sounds like the validation function shouldn't be relying on `$_FILES`. Can both the uploaded file and the downloaded file be homogenized to a common format for validation? – David Sep 30 '15 at 11:49
  • @David Yes, in both cases I can download the image to the server and then validate it but I try to avoid downloading a file before it has been validated by the function. I am using WordPress and I want the file to stay in $_FILES until validated and only afterwards to move it to a directory on the server. Does it make sense or am I missing some simple solution here? – randvir Sep 30 '15 at 11:55

0 Answers0