Generate single file name after upload

0

Next, I have an upload form so that the user can post some images, and it is necessary that he is logged in to post, in each post the maximum number of allowed images is 5, he was following this idea to create the name of the file:

1-5_infixo_1.jpg

1-5_infixo_2.jpg

Where 1 is the id of the logged-in user, 5 is the id of the post plus any any infix and the number would be generated by a loop repeat, a foreach for example. The problem with this is that the post id will only be generated after the success of the upload so I could not use it to rename the file, how can I create a single-named file following a similar logic?

    
asked by anonymous 08.09.2016 / 01:43

1 answer

1

If you only want to generate a unique name you can use md5 and uniqid plus timestamp ex:

$novaimagem = md5(uniqid()) . '-' . time() . '.jpg';

// exemplo de saída: e9ec23688ceedd4039c527b898575126-1473293386.jpg

If you have to use your user id:

$novaimagem = $_SESSION['USER-ID'] . '-' . md5(uniqid()) . '-' . time() . '.jpg';

// exemplo de saída: userID-e9ec23688ceedd4039c527b898575126-1473293386.jpg

Note: If your user registration system already provides a logical sequence that would not repeat the id, you do not have to worry about md5 in addition to the timestamp is always sequential.

    
08.09.2016 / 02:11