How can the filename be shortened before the file is written when uploading a file in PHP?

When uploading a file in PHP, you can shorten the filename before it is written by using the "pathinfo" function to get the file extension and then generating a unique filename using a combination of a timestamp and a random number. This ensures that the filename is unique and shorter than the original filename.

// Get the file extension
$extension = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);

// Generate a unique filename
$newFilename = uniqid() . '_' . time() . '.' . $extension;

// Move the uploaded file to the desired location with the new filename
move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/" . $newFilename);