Generating zip file receiving file input

Asked

Viewed 67 times

0

I have to generate the zip via an upload via file input, get compressed if not compressed and get it ready for download. It is working, the problem is that if the file sent is zip it compresses again and generates another zip if it is . pdf it compresses and uploads . pdf as well. Why are you duplicating zip file? I would like to resolve this issue.

public function beforeSave($insert)
    {
        if ($insert) {
            date_default_timezone_set('America/Recife');

            $this->data = date('d/m/Y');
            $this->hora = date('H:i');
            $this->dir = str_replace('', '', $this->nome . $_FILES['doc1']['name']);
            $zip = new \ZipArchive();
            if ($zip->open('../documentos/' . $this->dir . '.zip', \ZipArchive::CREATE) === TRUE) {

                foreach ($_FILES as $key => $file) {
                    if ($file['name']) {
                        $zip->addFile($file['tmp_name'], $file['name']);
                    }
                }

                $zip->close();
                $nome = $this->dir;

                move_uploaded_file($_FILES['doc1']['tmp_name'], '../documentos/' . $nome);
                Yii::$app->session->setFlash('success', "Documentos registrados com sucesso.");
                return true;
            }
        }
    }
}
  • Well, apparently in the code you create the Zip file and add all the upload files without checking the mimetype, so even though it is already a Zip it will add. Do a mime check to see if it’s already Zip or not.

  • But Anderson, why is he moving two to the directory. /documents?

  • As for the content all right, the problem is that it is sending two zips files to the directory. If Atachar um arquivo exemplo1.zip ele enviar exemplo1.zip e outro exemplo1.zip.

1 answer

0

Once you created the object Ziparchive and gave him a name, this file was created on the disk.

At the end you invoke the function move_uploaded_file() that will take the original file and copy also to the same folder.

I mean, you don’t need this line:

move_uploaded_file($_FILES['doc1']['tmp_name'], '../documentos/' . $nome);

Try to comment and test the routine again.

  • Hey, Anderson worked. Thanks. He was really moving the content that would be, what the code generated and the file input. Vlw. guys. vlw Anderson.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.