php renaming(unknown)上传文件扩展名

时间:2010-08-19 14:01:42

标签: php

嗨伙计们,我必须将(未知)上传的文件扩展名从.MP3重命名为.zip,请帮助我已经坚持了几天了。在此先感谢:)

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);
}

2 个答案:

答案 0 :(得分:2)

如果你需要添加.zip,这是否意味着你还需要实际压缩文件?只是.zip扩展名不会使它成为有效的zip文件。你可能想看看: 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';
}

答案 1 :(得分:1)

请改用:

function replace_extension($file, $new_extension) {
    $t = pathinfo($file);
    unset($t["basename"]);
    unset($t["extension"]);
    return implode(DIRECTORY_SEPARATOR, $t) . "." . $new_extension;
}