Created
August 19, 2013 07:20
-
-
Save rajvanshipradeep15/6266453 to your computer and use it in GitHub Desktop.
upload a zip file and unzip inserver
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if($_FILES["zip_file"]["name"]) { | |
$filename = $_FILES["zip_file"]["name"]; | |
$source = $_FILES["zip_file"]["tmp_name"]; | |
$type = $_FILES["zip_file"]["type"]; | |
$name = explode(".", $filename); | |
$accepted_types = array('application/zip', 'application/x-zip-compressed', 'multipart/x-zip', 'application/x-compressed'); | |
foreach($accepted_types as $mime_type) { | |
if($mime_type == $type) { | |
$okay = true; | |
break; | |
} | |
} | |
$continue = strtolower($name[1]) == 'zip' ? true : false; | |
if(!$continue) { | |
JError::raiseWarning(100, 'The file you are trying to upload is not a .zip file. Please try again.'); | |
$app->redirect("index.php?option=com_managequest"); | |
} | |
$target_path = JPATH_ROOT."/questionnaire/".time().$filename; | |
if(move_uploaded_file($source, $target_path)) { | |
$zip = new ZipArchive(); | |
$x = $zip->open($target_path); | |
if ($x === true) { | |
$zip->extractTo(JPATH_ROOT."/questionnaire/"); | |
$zip->close(); | |
unlink($target_path); | |
} | |
JFactory::getApplication()->enqueueMessage('Your '. $filename .' file was uploaded and unpacked.'); | |
$app->redirect("index.php?option=com_managequest"); | |
} else { | |
JError::raiseWarning(100, 'There was a problem with the upload. Please try again.'); | |
$app->redirect("index.php?option=com_managequest"); | |
} | |
} | |
else { | |
JError::raiseWarning(100, 'Please upload a valid file and try again.'); | |
$app->redirect("index.php?option=com_managequest"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment