I want to rename the files inside the extracted folder and it is being the problem now I faced.
Can someone give a solution for this issue?
Following is my code.
Code: Select all
<?php
function openZip($file_to_open) {
global $target;
$zip = new ZipArchive();
$x = $zip->open($file_to_open);
if($x === true) {
$zip->extractTo($target);
$zip->close();
unlink($file_to_open);
} else {
die("There was a problem. Please try again!");
}
}
if(isset($_FILES['fupload'])) {
function findexts ($filename) {
$filename = strtolower($filename);
$exts = explode("[/\\.]", $filename);
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$ext = findexts ($_FILES['fupload']['name']) ;
$ran = time () ;
$ran2 = $ran."-";
$source = $_FILES['fupload']['tmp_name'];
$type = $_FILES['fupload']['type'];
$name = explode('.', $ext);
$target = "images/";
$accepted_types = array('application/zip',
'application/x-zip-compressed',
'multipart/x-zip',
'application/s-compressed');
foreach($accepted_types as $mime_type) {
if($mime_type == $type) {
$okay = true;
break;
}
}
$okay = strtolower($name[1]) == 'zip' ? true: false;
if(!$okay) {
die("Please choose a zip file!");
}
$saved_file_location = $target . $ext;
if(move_uploaded_file($source, $saved_file_location)) {
openZip($saved_file_location);
} else {
die("There was a problem. Sorry!");
}
$scan = scandir($target . $name[0]);
print '<ul>';
for ($i = 0; $i<count($scan); $i++) {
if(strlen($scan[$i]) >= 3) {
$check_for_html_doc = strpos($scan[$i], 'html');
$check_for_php = strpos($scan[$i], 'php');
if($check_for_html_doc === false && $check_for_php === false) {
echo '<li>' . '1 - ' . $scan[$i] . '</li>';
} else {
echo '<li><a href="' . $target . $name[0] . '/' . $scan[$i] . '">' . $scan[$i] . '</a></li>';
}
}
}
print '</ul>';
$rname = scandir($target . $name[0]);
for ($j = 0; $j<count($rname); $j++) {
rename($target . $name[0], $ran2 . $name[0]);
}
$connect = mysql_connect("localhost", "root", "123") or die ("Error , check your server connection.");
mysql_select_db("upload_and_unpack_zipped_file") or die("Error , check your database.");
$query = "INSERT INTO table_1 (image1, image2, image3, image4) values('$scan[2]', '$scan[3]', '$scan[4]', '$scan[5]')";
mysql_query($query) or die(mysql_error());
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<title>Upload</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<div id="container">
<h1>Upload A Zip File</h1>
<form enctype="multipart/form-data" action="" method="post">
<input type="file" name="fupload" /><br />
<input type="submit" value="Upload Zip File" />
</form>
</div>
</body>
</html>