0

I dont have a database. But I would like the server to send me a unique number for each of my uploads. I have written the Server side is written in PHP. I have defined a global values as fileId and assigned 1 to it at the begining. Then I do an auto increment of it. But for all my uploads I get one (the same number as the begining). Can someone tell me what I'm doing wrong here?

<?php

$fileId = 1;

class UploadController {

  private static function uploadRequest() {
    echo self::$PARAM_FILE_ID . "=" . $GLOBALS['fileId'];
    $GLOBALS['fileId'] = $GLOBALS['fileId'] + 1;
  }
}
?>
lighter
  • 2,808
  • 3
  • 40
  • 59
user77005
  • 1,769
  • 4
  • 18
  • 26
  • Every time you run the script, you set the variable to `1`. Variables don't persist across different runs of the same script. If you don't have a database, put it in a file. – Barmar Nov 27 '14 at 02:34
  • Use Database, data source... – Ryan Nov 27 '14 at 02:42
  • You could count the number of files in your upload directory and return count + 1 as fileid, if you don't want to use sessions or a database – Paul Carlton Nov 27 '14 at 02:45
  • You don't need a database, you could store them in a file and use `fopen`, `fread`, `fclose`. – EternalHour Nov 27 '14 at 03:48
  • You could look at using [UUID](http://stackoverflow.com/questions/2040240/php-function-to-generate-v4-uuid/15875555#15875555) as well. – Ja͢ck Nov 27 '14 at 04:15

1 Answers1

1

Global variables are only global to the current request.

There's no way to do that in PHP itself.

You have to use either a database or some sort of cache mechanism (even if only filesystem based).

Joseph Silber
  • 214,931
  • 59
  • 362
  • 292