简体   繁体   English

PHP 文件上传和 zip 上传

[英]PHP file upload and zip on upload

Does anyone know of a good way to upload a file (.mp3 or.wav or.txt or.whatever) and zip this file before being moved to the directory?有谁知道在移动到目录之前上传文件(.mp3 or.wav or.txt or.whatever)和 zip 这个文件的好方法?

So the user uploads a.mp3 file, before this file is moved to the specific directory it will be zipped and then moved.所以用户上传一个.mp3文件,在这个文件被移动到特定目录之前,它会被压缩然后移动。

Please advise.请指教。

Thank you谢谢

Take a look at the Zip extension:看看 Zip 扩展:

http://www.php.net/manual/en/zip.examples.php http://www.php.net/manual/en/zip.examples.php

I looked at the code you linked to (it would have been good if you included it in the question) and made a few changes:我查看了您链接到的代码(如果您将其包含在问题中会很好)并进行了一些更改:

$nameFile = $_FILES['file']['name'];
$tmpName = $_FILES['file']['tmp_name'];
$download_folder = './files/';

$zip = new ZipArchive();
$fileconpress = $download_folder.$nameFile.".zip";

$conpress = $zip->open($fileconpress, ZIPARCHIVE::CREATE);
if ($conpress === true)
{
    $zip->addFile($tmpName);
    $zip->close();
    echo $fileconpress."<br/>";
    echo "yess !! Success!!!! ";
}
else echo " Oh No! Error";

The important part and likely what's causing your error is $download_folder.重要的部分和可能导致您的错误的原因是 $download_folder。 You need to define the path of where you want to save the file.您需要定义要保存文件的路径。

I also removed the fread() , you can just load the file straight into the zip object with addFile()我还删除了fread() ,您可以使用addFile()将文件直接加载到 zip object

I think it's the best way to upload each type of file in zip exe espesialy when you don't know that your clients or users upload what kind of file.我认为这是在 zip exe 中上传每种类型文件的最佳方式,尤其是当您不知道您的客户或用户上传什么样的文件时。 for this way you should first set your direction in server where you want upload the file and then use:对于这种方式,您应该首先在要上传文件的服务器中设置方向,然后使用:

<?php
$target_dir = '/www/yoursite.com/public_html/sth';
$ext = explode(".", $_FILES["filename1"]["name"]);
$ext = end($ext);
$newfilename = 'resume'. '.' .$ext;
$zip = new ZipArchive();
$zip->open('/www/yoursite.com/public_html/sth .'/'.'resume'.'.zip', 
ZipArchive::CREATE);
$zip->addFile($_FILES['filename1']['tmp_name'],$newfilename);
$zip->close();
?>

just pay attention to my guide as your friend请注意我的导游作为你的朋友

when you want to use this code you should know these points:当您想使用此代码时,您应该了解以下几点:

  1. $target_dir-->your server address where you want upload your file $target_dir-->您要上传文件的服务器地址
  2. in this part of code i want to get the file extension until when i zip it keep the type of file在这部分代码中,我想获取文件扩展名,直到我 zip 它保留文件类型

    $ext = explode(".", $_FILES["filename1"]["name"]); $ext = end($ext); $newfilename = 'resume'. '.'.$ext;
  3. here i named my zip file in my server在这里,我在我的服务器中命名了我的 zip 文件

    $zip->open('/www/yoursite.com/public_html/sth.'/'.'resume'.'.zip',
  4. and in the end of code we get the upoloade file that i named here as "filename1(this is the name of your input in html)"在代码的末尾,我们得到了我在这里命名为“filename1(这是您在 html 中输入的名称)”的上传文件

    $zip->addFile($_FILES['filename1']['tmp_name'],$newfilename);

You can use PHP's ability to run shell commands and use them to make 3rd party software/your OS to zip your files for you.您可以使用 PHP 运行 shell 命令的能力,并使用它们为您的文件制作第 3 方软件/您的操作系统到 zip。 see - http://php.net/manual/en/function.shell-exec.php见 - http://php.net/manual/en/function.shell-exec.php

Or there is Zip extensions for php see - http://php.net/manual/en/book.zip.php Or there is Zip extensions for php see - http://php.net/manual/en/book.zip.php

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

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