Loading...
 
Gmail / Facebook Style Chat Application


Posted: Sunday 30th of September 2012

Upload and Extract Files To Remote Server with PHP



This tutorial will help as many who have been having problems uploading and extracting zipped files to their remote servers.
This program is completely written in PHP to upload and extract zipped files of any kind of document to a specified location on the remote server.
The script will display the content of a specified remote directory on the screen with an option to see all the uploaded and extracted files or directories to the server.




Download Live Demo


Code:

<?php
//Increase script execution time if you deem it necessary
ini_set('max_execution_time', 10000);

if($_POST["submit_file"] == "1")
{
if($_FILES['zip_file_to_upload']['name'] == "")
{
$upload_status = "<font style='font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;'>Please browse for a zip file to upload.</font><br>";
}
else
{
$folder_to_upload_file = "uploads";
$file = $_FILES['zip_file_to_upload'];
$allowedExtensions = array("zip");
function isAllowedExtension($fileName)
{
global $allowedExtensions;
return in_array(end(explode(".", $fileName)), $allowedExtensions);
}
if($file['error'] == UPLOAD_ERR_OK)
{
if(isAllowedExtension($file['name']))
{
$zip = new ZipArchive;
move_uploaded_file($_FILES['zip_file_to_upload']['tmp_name'], "".$folder_to_upload_file."/" .$_FILES['zip_file_to_upload']['name']);
$res = $zip->open("".$folder_to_upload_file."/".$_FILES['zip_file_to_upload']['name']."");

if ($res === TRUE)
{
$zip->extractTo("".$folder_to_upload_file."/");
$zip->close();
$dirnamea = "".$folder_to_upload_file."/".$_FILES['zip_file_to_upload']['name']."";
@chmod($dirnamea,0777);
@unlink($dirnamea);

$upload_status = "<font style='font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:blue;'>".$_FILES['zip_file_to_upload']['name']." uploaded and extracted successfully.</font><br>";
}
else
{
$upload_status = "<font style='font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;'>".$_FILES['zip_file_to_upload']['name']." was not successful.</font><br>";
}
}
else
{
$upload_status = "<font style='font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;'>You can only upload a zip file through this tool.</font><br>";
}
}
else
{
$upload_status = "<font style='font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;'>You can not upload a any file at the moment. please try again or contact us if this problem persist.</font><br>";
}
}
}
?>
<style> .aadileds{ font-weight:bold; font-size:12px;font-family:Arial, Helvetica, sans-serif;text-align:center;background:#f2f2f2 url('addfile.png') no-repeat 12px 9px;color:green;border:1px solid #ccc;height:30px;cursor: default;width:106px;-moz-border-radius:5px; -webkit-border-radius:5px;float:left; }</style>

<center>
<div id="vasp" style="">Upload and Extract Files To Remote Server with PHP</div><br clear="all" /><br clear="all" />
<div style="width:600px; border:6px solid #F6F6F6; font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px;" align="center">
<font color="#000000">Zipped files will be uploaded, unzipped and send to the remote server.</font><br />
<br clear="all" /><?php echo $upload_status; ?><br clear="all" /><br clear="all" />
<form enctype="multipart/form-data" action="zip_upload.php" method="POST" style="width:340px;">
<div align="center" style="400px;">
<div style="width:120px; float:left; padding-top:10px;" align="left">Archive(.zip):</div>
<div style="width:200px; float:left;" align="left"><span class="aadileds"><input name="zip_file_to_upload" type="file" style="opacity:0;-moz-opacity:0;filter:alpha(opacity:0);z-index:9999;width:90px;padding:5px;cursor:default;" /></span></div><br clear="all" /><br clear="all" />
<div style=" padding-left:120px;width:200px; float:left;" align="left"><input type="submit" value="Upload" style="padding:10px; width:80px; cursor:pointer;" /></div><br clear="all" /><br clear="all" /><br clear="all" />

<?php
$directory_to_load_files_from = "uploads/";
@chmod($directory,0777);
if($open_directory_to_read_its_content = opendir($directory_to_load_files_from))
{
echo '<select name="fileToZip" id="fileToZip" style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px;line-height:25px;padding:1px;">';
echo '<option value="">These are all the uploaded files</option>';
while(($directory_contents = readdir($open_directory_to_read_its_content)) !== false)
{
if($directory_contents != "." && $directory_contents != "..")
{
echo '<option value="'.$directory_contents.'">'.$directory_contents.'</option>';
}

}
echo '</select>';
closedir($open_directory_to_read_its_content);
}
?>
</div>
<input type="hidden" name="submit_file" value="1" />
</form>
</div>
</center>




That's it guys...


Views Today: 0
Total Views: 1131

Comments
0
OUR OBJECTIVE

Our objective is to reach a place where our services will be highly regarded by businesses from various industrial domains for building their innovative busines solutions with our cutting-edge technological expertise, interactive designs and uncompromised quality.

OUR MISSION

We aspire to help businesses ranging from startups to enterprises, who reach out to us with their requirements, in achieving great lengths, expanding their reach, upscaling their products, and generate a large user-base with our outstanding and cost-effective services.

Copyright © 2011 - 2024 | All Rights Reserved