top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

PHP: How to rename uploaded file before saving it into a directory?

+2 votes
578 views
PHP: How to rename uploaded file before saving it into a directory?
posted Aug 20, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

Instead of

move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $_FILES["file"]["name"]);

Use

$temp = explode(".",$_FILES["file"]["name"]);
$newfilename = rand(1,99999) . '.' .end($temp);
move_uploaded_file($_FILES["file"]["tmp_name"], "../img/imageDirectory/" . $newfilename;

Changed to reflect your question, will product a random number between 1-99999 and append the extension from the originally uploaded file.

answer Aug 24, 2014 by Deepak Negi
Similar Questions
+1 vote

I'm writing a PHP extension now in c/c++. User uploads a file (could be POST or PUT method, but I can limit it to POST only). I need to capture the file data while being uploaded, without writing it to disk on the server. I need to process the data and (maybe, depending on a situation) send it somewhere else or save it to disk.
Of course I know, that I can process the file after it has been uploaded (saved on disk on the server), but I would like to avoid it. I also need to do something opposite: I need to generate a file "on the fly" and send it
to the user. All metadata of the generated file is known beforehand (e.g. size, name).

I've been searching around for some time now and I could not find anything even close to the solution. Is there any example(s) or existing PHP extension that do(es) something like this (at least something simmilar) ? If you could give me any pointers that would be awesome.

...