【问题标题】:how to upload, archive and rename files using php?如何使用 php 上传、归档和重命名文件?
【发布时间】:2011-05-26 00:05:12
【问题描述】:

是否可以使用 PHP 上传两个不同的文件并使用新文件名以 zip 格式存档?以下是我创建的表单。

 <form action="upload.php" method="post" enctype="multipart/form-data" name="form1" id="form1">
        <h1>Submit here</h1>

    <p>

    <label for="cat">category</label>

    <select id="cat" name="cat" value="">Category</option>

    <option value="csr2050">Cns</option>

    <option value="npp2023">npp</option>

    </select>

    </p><p>

        <label for="fsheet">fsheet</label>
    <input name="fsheet" type="file" id="fsheet" />
    </p><p>

        <label for="report">Report</label>
    <input name="report" type="file" id="report" />
    </p><p>

    <input type="submit" name="Submit" id="Submit" value="upload" />
    </form>

我想要的是,如何编写upload.php,它可以创建一个选定两个文件的zip存档文件并将其重命名为选定的类别值,然后将其上传到/upload文件夹?

【问题讨论】:

  • 看看页面的右上角,你看到那个“搜索”的东西了吗?

标签: php file-upload rename archive


【解决方案1】:

在 PHP 中也可以轻松实现。但是,您在此处询问 3 个功能

  1. 上传多个文件
  2. 压缩文件
  3. 重命名文件

每一个都是不同的解决方案,我的代码可能需要根据你的变量来改变, 您可以查看 zip 实用程序链接以了解详细信息。您还可以在 Stackoverflow 中为每个任务(例如 Zip 文件)获取帖子数量

PHP ZIP files on the fly

上传多个文件

Upload two files at once

上传

<?

$file_name1 = $_FILES['fsheet']['name'];
$file_name1 = stripslashes($file_name1);
$file_name1 = str_replace("'","",$file_name1);
$copy = copy($_FILES['fsheet']['tmp_name'],$file_name1);

 // prompt if successfully copied
 if($copy){
 echo "$file_name1 | uploaded sucessfully!<br>";
 }else{
 echo "$file_name1 | could not be uploaded!<br>";
 }


$file_name2 = $_FILES['report']['name'];
$file_name2 = stripslashes($file_name2);
$file_name2 = str_replace("'","",$file_name2);
$copy = copy($_FILES['report']['tmp_name'],$file_name2);

 // prompt if successfully copied
 if($copy){
 echo "$file_name2 | uploaded sucessfully!<br>";
 }else{
 echo "$file_name2 | could not be uploaded!<br>";
 }

?>

** 邮编 ** 首先从下载 zip 实用程序类 http://www.phpclasses.org/browse/file/9524.html

<?php
    $directoryToZip="secret"; // 
            $outputDir = $_POST['rootfolder'];
            //$outputDir="$folder"; //Replace "/" with the name of the desired output directory.
            $zipName="backup.zip";

            include_once("zip/CreateZipFile.inc.php");
            $createZipFile=new CreateZipFile;
            /*
            // Code to Zip a single file
            $createZipFile->addDirectory($outputDir);
            $fileContents=file_get_contents($fileToZip);
            $createZipFile->addFile($fileContents, $outputDir.$fileToZip);
            */

            //Code toZip a directory and all its files/subdirectories
            $createZipFile->zipDirectory($directoryToZip,$outputDir);


            $fd=fopen($zipName, "wb");
            $out=fwrite($fd,$createZipFile->getZippedfile());
            fclose($fd);
            $msg = "Files backup successfully";
            //$createZipFile->forceDownload($zipName);
            $trgtName = date("F-Y-h-i-s"). ".zip";
            copy ($zipName,$outputDir."/".$trgtName);
            @unlink($zipName);


    ?>

【讨论】:

  • 文件上传脚本说无法上传!上传文件夹有777权限,一开始我只想重命名上传。
猜你喜欢
  • 2010-10-16
  • 2013-04-29
  • 2020-08-29
  • 2020-05-19
  • 2021-10-25
  • 1970-01-01
  • 2016-02-28
  • 2012-05-04
  • 1970-01-01
相关资源
最近更新 更多