2

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.

nacho
  • 5,280
  • 2
  • 25
  • 34
Codejah
  • 21
  • 2
  • 1
    Have you seen the [pathinfo](https://www.php.net/manual/en/function.pathinfo.php#refsect1-function.pathinfo-examples) function? Also, see this https://stackoverflow.com/a/173876/296555. – waterloomatt Sep 23 '22 at 22:10

1 Answers1

2

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;
?> 
mr90
  • 41
  • 1
  • 6
  • 2
    Won't that give _filename.mp3(Jarahub.xyz).mp3_? – waterloomatt Sep 23 '22 at 22:12
  • Hmm guess i know what you want now. Let me edit it... – mr90 Sep 23 '22 at 22:23
  • He is right if there was a way we could explode the file name and then separating it from it's extension and then Concatenating it again with the name the string and file extension for the final file name. Thanks your code gave me an idea – Codejah Sep 26 '22 at 23:57