tim peterson
tim peterson

Reputation: 24305

What is point of a temporary file name during a file upload?

What is the point of $_FILES["file"]["tmp_name"]?

I know it is the name of the temporary copy of the file stored on the server but why do we need this when we have $_FILES["file"]["name"]?

I would assume it has to do with preventing name collisons, is that true? Are there other reasons? I'm using PHP syntax but I would guess the concept would apply to all the languages?

Upvotes: 1

Views: 2910

Answers (2)

Crozin
Crozin

Reputation: 44376

  1. This is not a PHP syntax.
  2. $_FILES['x']['name'] stores the name of the file on the user's filesystem - this is only extra information that is set up by the browser - just like $_FILES['x']['type'].
  3. $_FILES['x']['tmp_name'] stores the name of the uploaded file on the server.

EDIT:

When you upload a file, it is phisically stored on the server's hard drive. A name to the file (not the entire path) will be available in your PHP under $_FILES['x']['tmp_name'] variable. You should move the file using move_uploaded_file() function. This function knows the path to the directory where the file is stored, so it's capable of moving the file to its new location.

$_FILE['x']['name'] / ['type'] are actually completely useless as the're being set by the browser during file upload, therefore they could store false information.

Upvotes: 4

Ray
Ray

Reputation: 41428

Because when someone uploads a file, until you save it somewhere, it's not really on disk under the 'name' until you explicitly save it somewhere else. Until then it's contents are only found in the temporary file.

Upvotes: 1

Related Questions