Jump to content

Add the user_id to the random string


Chrisj

Recommended Posts

In the PHP script I'm using, in the Upload Form the user selects an image to Upload, the Form renames it like so:

$allowedExts = array("gif", "jpeg", "jpg", "pdf", "png");
$temp = explode(".", $_FILES["file"]["name"]);
$extension = strtolower( end($temp) );

if (!in_array($extension,$allowedExts))
{
echo ("Error - Invalid File Name");

}
$length = 20;
$randomString = (time());
$thumbnail = $randomString . "." . $extension;

The random string works successfully, but I'd like to add the user_id to the beginning of it and a dash, like this:

user_id - 

So, the new file name would be something like:   user_id-randomString.extension

 

Can you please help me add that?

 

Link to comment
https://forums.phpfreaks.com/topic/292167-add-the-user_id-to-the-random-string/
Share on other sites

I've seen this code before.  Copy it from somewhere?

 

Anyway - what you want to do is no different than what you are doing to build $thumbnail, altho as I recall from before $thumbnail never gets used.

 

PS - a 'form' doesn't do any renaming of your file.  PHP code does it.  Also - what is your script supposed to do when if finds an invalid extension?  According to your code it proceeds along its merry way trying to rename the file and do whatever else you haven't shown us.

This code (your code?) creates a variable called $thumbnail.  It does it by concatenating 3 things.  So - follow that example and concatenate user id and whatever else you want to include in your filename.

 

Perhaps you should be reading some introductory php manuals/books/references in order to understand basic syntax.  What I am showing you is Very Basic so if you can't follow it you will find learning php vary difficult.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.