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
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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.