Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/273.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 重命名文件,然后将其压缩_Php_Zip - Fatal编程技术网

Php 重命名文件,然后将其压缩

Php 重命名文件,然后将其压缩,php,zip,Php,Zip,我使用下面的代码来压缩一些文件,但是我需要在压缩之前对它们进行重命名 这是我的密码: function create_zip($files = array(),$destination = '',$overwrite = false) { if(file_exists($destination) && !$overwrite) { return false; } $valid_files = array(); if(is_array($files)) {

我使用下面的代码来压缩一些文件,但是我需要在压缩之前对它们进行重命名

这是我的密码:

function create_zip($files = array(),$destination = '',$overwrite = false) {
    if(file_exists($destination) && !$overwrite) { return false; }
    $valid_files = array();
    if(is_array($files)) {
        foreach($files as $file) {
            if(file_exists($file)) {
                $valid_files[] = $file;
            }
        }
    }
    if(count($valid_files)) {
        $zip = new ZipArchive();
        if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
            return false;
        }
        foreach($valid_files as $file) {
            $zip->addFile($file, basename($file));
        }

        $zip->close();

        return file_exists($destination);
    }
    else {
        return false;
    }
}

$query = "SELECT tracks.*, orders_temp.* FROM tracks LEFT JOIN orders_temp ON tracks.trackID = orders_temp.track WHERE session_id = '".session_id()."'";
$result = $conn->query($query);
$files_to_zip = array();

while($row = $result->fetch_object()){
    $files_to_zip[] = 'uploads/music/' . $row->trackMP3;


    $query_got = "SELECT * FROM downloads WHERE downloadTrack = $row->trackID AND downloadUser = $uid";
    $result_got = $conn->query($query_got);

    if ($result_got->num_rows == 0) {
        $query1 = "INSERT INTO downloads(downloadTrack, downloadUser) values($row->trackID,$uid)";
        $result1 = $conn->query($query1);
    }
}

$zipped = 'uploads/zipped/mashup-' . session_id() . '.zip';


$result = create_zip($files_to_zip,$zipped);
$file = 'mashup-' . session_id() . '.zip';
$fp = fopen($zipped, 'rb');

header("Content-Transfer-Encoding: binary"); 
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=$file");
header("Content-Length: " . filesize($zipped));
fpassthru($fp);

我基本上希望
$row->trackName
成为zip文件中的文件名,而不是
$row->trackMP3

ZipArchive::addFile的第二个参数就是这样做的。而不是basename($file)添加您想要的文件名。但是我如何通过它传递-$file目前基本上是$files\u to_zip[]=“uploads/music/”$row->trackMP3,但是如何通过$row->trackName这样的东西呢?