Is it possible upload two different files and archive in zip with new file name using PHP? following is the form I created.
<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>
here what I want is, how to write upload.php that can create a zip archive file of selected two files and rename it to the selected category value then upload it to /upload folder?
It is possible and easy too doing in PHP. However there are 3 functionalities you are asking here
Each one is different solutions, My code may need to change according to your variables, The zip utility link you can see for the details. Also you can get number of posts in Stackoverflow for each of your tasks like, Zip files
Upload multiple files
Upload
<?
$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 ** First get download the zip utility class from 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);
?>