views:

123

answers:

1

I need to get the aboslute path of a file uploaded by the user in PHP. How can I do that? Im doing this cause I need to post the information to an API via cURL

+3  A: 

The file path is stored in the $_FILES array - just apply realpath to that

realfile($_FILES['userfile']['tmp_name']);

A few points:

  • Check that the file is the uploaded file with is_uploaded_file
  • Move it to a new location using move_uploaded_file
  • The API you're after will want a url for the file - rather than a file path - assuming the api is not on the same server
adam
+1 Very nice answer
soulmerge
Im actually building an API and want users to be able to upload files from sites that use the API. Ideally I didn't want to have to temporarily store it on the site's space, but it sounds like I will have to.
David
Yeah - the http post upload functionality uploads it to the temp folder on the server, although you might be able to get round this using the more advanced uploaders available via YUI Library, for example
adam
There is actually a way to do with without the need of uploading to the site using the API. If using cURL, just ad "@".realpath($_FILES['userfile']['tmp_name'])
David