如何解决如何使用php上传,存档和重命名文件?
| 是否可以使用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>
这就是我想要的,如何编写能够创建所选两个文件的zip存档文件并将其重命名为所选类别值,然后将其上传到/ upload文件夹的upload.php?
解决方法
在PHP中这样做也是可能且容易的。但是,您要在这里查询3种功能
上载多个文件
压缩文件
重命名文件
每个解决方案都不相同,我的代码可能需要根据您的变量进行更改,
您可以查看zip实用程序链接以获取详细信息。此外,您还可以在Stackoverflow中获得有关每个任务(例如Zip文件)的帖子数
快速运行PHP ZIP文件
上载多个文件
一次上传两个文件
上载
<?
$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);
?>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。