In this post you will learn how to upload zip file into a folder using HTML form in PHP. Firstly you have to create simple HTML form.
HTML Code
<input type=“file” name=“zipFile” accept=“zip/*”>
<input type=“submit” name=“Submit” value=“Submit”>
</form>
PHP Code
Using this code you can upload .zip files. In this code you change the extension of the files which you want to upload and can change the directory path as your requirement.
$streplaceFileName = cleanSpecialCharacters($_FILES[‘zipFile’][‘name’]);
$zipFile = $rand.”-“.$streplaceFileName;$ds = DIRECTORY_SEPARATOR;
$storeFolder = ‘uploads/zip’;
if((!empty($_FILES)) && !empty($_FILES[‘zipFile’][‘name’])) {
if(preg_match(‘/[.](zip)$/’, $_FILES[‘zipFile’][‘name’])) {
$filename = $rand . “-” . $streplaceFileName;
$tempFile = $_FILES[‘zipFile’][‘tmp_name’];
$targetPath = $storeFolder . $ds;
$targetFile = $targetPath.$filename;
$check = move_uploaded_file($tempFile,$targetFile);
if($check) echo “ZipFile Uploaded Successfully!”;
}
}
function cleanSpecialCharacters($string) {
$string = str_replace(‘ ‘, ‘-‘, $string); // Replaces all spaces with hyphens.
return preg_replace(‘/[^A-Za-z0-9.\-]/’, ”, $string); // Removes special chars.
}
I want to add _ (underscore to file name ) example car_list.zip but its giving error how do i resolve this ?