How to add a word at the end of filename when uploading in php

Viewed 26

I want to add a word or string at the end of every file I upload in in php script. For example, music.mp3 would be music(jarahub.xyz).mp3 after been uploaded. I tried achieving that with this line of code

$filename = $_FILES['myfile']['name']. '(Jarahub.xyz)';

But it ended up changing the file extension please can anyone help me with a more efficient code.

1 Answers

Try it please:

<?php
$myFile = $_FILES['myfile']['name'];
$randomString = '(asd.xyz)';
$currentExtension = pathinfo($myFile, PATHINFO_EXTENSION);

$newExtension = $randomString . '.' . $currentExtension;
$info = pathinfo($myFile);
$result = $info['filename'] . $newExtension;

echo $result;
?> 
Related