tags:

views:

47

answers:

2

hi guys i have to rename the (unknown) uploaded file extension from .MP3 to .zip please help I have been stuck with this for a few days now. Thanks in advance:)

if (!copy($file, $newfile)) {
    echo "failed to copy $file...\n";
}
$filename = "members/$id/music" . "/" . $_FILES["file"]["name"];
    function replace_extension($filename, $zip) {
    return preg_replace('/\..+$/', '.' . $zip, $filename);
}
+1  A: 

Use this instead:

function replace_extension($file, $new_extension) {
    $t = pathinfo($file);
    unset($t["basename"]);
    unset($t["extension"]);
    return implode(DIRECTORY_SEPARATOR, $t) . "." . $new_extension;
}
Artefacto
Ahh, clever! More complete than mine, as it preserves a possible full path. +1 and removing
Pekka
@Pekka Unfortunately the ` PATHINFO_DIRNAME | PATHINFO_BASENAME | PATHINFO_FILENAME` combination seems buggy, because it doesn't unclude the filename. I had to change the implementation.
Artefacto
thank you all so much for the help OK apolagies guys for the ignorance but I changed stuff as I don't know what needed changing in the above quote but anyways here goes.//////////////////////////////$new_extension = "zip";function replace_extension($file, $new_extension) { $t = pathinfo($file); unset($t[$_FILES["file"]["name"]]); unset($t["MP3"]); return implode("members/$id/music" . "/" . $_FILES["file"]["name"], $t) . "." . $new_extension;}
drakos
Hi Guys thanks for the reply to all, I still can't rename the file. The file extension i know is a mp3 but the file name itself is unknown as it's being uploaded by the user, i tried this zip archive that i got from the php learning web page but like the help you gave me i do not know what to change in the script to make it work with the uploaded file
drakos
$file = "members/$id" . "/" . $_FILES["file"]["name"];$newfile = "members/$id/music" . "/" . $_FILES["file"]["name"];so due to my stupidity can someone please give me an exsample explaining what i need to change to what in the script as at this point my file look like this,$_FILES["file"]["name"]thanks again
drakos
this is when i moved the file to the position i want,$file = "members/$id" . "/" . $_FILES["file"]["name"]; $newfile = "members/$id/music" . "/" . $_FILES["file"]["name"];
drakos
and this is what i tried acoording to your help:
drakos
$new_extension = "zip";function replace_extension($file, $new_extension) { $t = pathinfo($file); unset($t[$_FILES["file"]["name"]]); unset($t["MP3"]); return implode("members/$id/music" . "/" . $_FILES["file"]["name"], $t) . "." . $new_extension;}
drakos
a simple rename function works well if i know what the name of the (file).mp3 is , but i cant rename the $_FILES["file"]["name"]
drakos
what is the "basename" and what should it look like?
drakos
do i need to identify the $file and $new_extension before hand and so i include the (.) in the "extension" var
drakos
+1  A: 

If you need to add .zip, does this mean you need to actually compress the file too? Just ading the .zip extension will not make it a valid zip file. You may want to have a look at: http://www.php.net/manual/en/function.ziparchive-addfile.php

$zip = new ZipArchive;
if ($zip->open('test.zip') === TRUE) {
    $zip->addFile('/path/to/index.txt', 'newname.txt');
    $zip->close();
    echo 'ok';
} else {
    echo 'failed';
}
cdburgess
I tried your idea which is great thanx but the server gives me and error: Warning: ////////////////////////////////ZipArchive::addFile() [ziparchive.addfile]: Invalid or unitialized Zip object//////////////when i try this:////////////$path = "./";$file = "members/$id" . "/" . $_FILES["file"]["name"];$zip=new ZipArchive;$zip->addFile('./', $_FILES["file"]["name"]);
drakos
You may need to load the PECL extension or compile php with `--with-zip`. See: http://www.php.net/manual/en/zip.installation.php. Another option could be to use gz compression (http://www.php.net/manual/en/function.gzwrite.php)
cdburgess