简体   繁体   English

使用 $_FILES 和 ZipArchive(PHP 和 Angular)- 正在下载不完整的文件

[英]Working with $_FILES and ZipArchive (PHP and Angular) - incomplete file being downloaded

I have an angularJS based project with a PHP server.我有一个基于 angularJS 的项目和一个 PHP 服务器。 The goal at this point is to take a.jar file, and add json file to the META-INF folder in the.jar file and then download it back on the front end.此时的目标是取一个.jar文件,在.jar文件中将json文件添加到META-INF文件夹中,然后在前端下载回来。 Current solution seems to return an incomplete JAR file.当前解决方案似乎返回一个不完整的 JAR 文件。 It is openeable with 7Zip, but there is no content within the archive.它可以用 7Zip 打开,但存档中没有内容。 I am unsure if the issue is the PHP and returning the file or the Angular. I am open to suggestions to do this differently.我不确定问题是 PHP 并返回文件还是 Angular。我愿意听取以不同方式执行此操作的建议。

My JS in the controller:我的 JS 在 controller:

//Upload the Two Files:
exportFile = document.getElementById('jar-file-export');
exportFile.addEventListener('change',(event)=>{
    
    //Get Jar File, and Save name for later use.
    let i = document.getElementById('jar-file-export');
    let formData = new FormData();
    formData.append('jar', i.files[0]);
    $scope.export.filename = i.files[0].name;
    
    //GET JSON File:
    let tFile = new Blob([JSON.stringify($scope.export.file)], {type: 'application/json'});
    formData.append('descriptor',tFile);
    
    $http({
        method:"POST",
        url:"assets/api/fileExport.php",
        data: formData,
        headers:{"Content-Type":undefined},
        dataType: 'text',                                   
        mimeType: 'text/plain; charset=x-user-defined',     
        }).then(function success(response){
            //saveAs = fileSaver.js
            console.log('File Export,Success',response.data);
            let data = response.data;               
            //Code copied from StackOverFlow:
            
            let newContent = "";                                
            for (var i = 0; i < data.length; i++) {         
                newContent += String.fromCharCode(data.charCodeAt(i) & 0xFF); 
            }
            var bytes = new Uint8Array(newContent.length);                     
            for (var i=0; i<newContent.length; i++) {                         
                bytes[i] = newContent.charCodeAt(i);                          
            }
            blob = new Blob([bytes], {type: "application/zip"})
            saveAs(blob, $scope.export.filename);

        },function error(response){
            console.log('File Export,error',response);
        });
    

})

PHP: PHP:

<?php
switch($_SERVER["REQUEST_METHOD"]) {
case("POST"):{
    
    $zip = new ZipArchive;
    if ($zip->open($_FILES['jar']['tmp_name']) === true){
        $zip->open($_FILES['jar']['tmp_name']);
        $zip->addFile($_FILES['descriptor']['tmp_name'],file_get_contents($_FILES['descriptor']['tmp_name']));
        $zip->close();

        echo (file_get_contents($_FILES['jar']['tmp_name']));
    }



    break;
}

} }

Additionally, I have tried just opening the JAR file and then returning it back to the client, and I still get an empty (but openable) jar file.此外,我尝试只打开 JAR 文件,然后将其返回给客户端,但我仍然得到一个空(但可打开)的 jar 文件。 The below results in a JAR that has a META-INF folder:下面的结果是一个 JAR 有一个 META-INF 文件夹:

PHP: PHP:

<?php
switch($_SERVER["REQUEST_METHOD"]) {
case("POST"):{

    $zip = new ZipArchive;
    $newZip = new ZipArchive;

    if ($zip->open($_FILES['jar']['tmp_name']) === true){
        echo (file_get_contents($_FILES['jar']['tmp_name']));
    }


    break;
}

} }

Any suggestions here would be greatly appreciated.这里的任何建议将不胜感激。 Thanks谢谢

On going through several iterations with lots of SO answers - I discovered that the PHP wasn't properly referencing the file location and modified it so that the PHP moved the files to temp folder.在经历了很多 SO 答案的多次迭代后 - 我发现 PHP 没有正确引用文件位置并对其进行了修改,以便 PHP 将文件移动到临时文件夹。

 $jar = $_FILES["jar"]["tmp_name"];
    $desc = $_FILES["desc"]["tmp_name"];
    $folder = "temp/";
    $jar_name = $_FILES["jar"]["name"];
    $desc_name = $_FILES["desc"]["name"];

    move_uploaded_file($jar,$folder.$jar_name);
    move_uploaded_file($desc,$folder.$desc_name);

    //Open the Jar, add the descriptor and return the file name:

    $zip = new ZipArchive();
    if ($zip->open($folder.$jar_name) === TRUE){

        $zip->addFile($folder.$desc_name,"META-INF/data.json");
        $zip->close();

        echo json_encode($jar_name);

    } else {
        echo "Failed to Open the JAR file";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM