Loading...
 
Gmail / Facebook Style Chat Application


Posted: Sunday 30th of September 2012

Upload Files to Remote Server, View Files on Remote Server and Delete Files from Remote Server



This tutorial helps you or teaches you how to upload different type of files to your remote server, view already uploaded files on the remote server and also gives you the option to delete uploaded files from the remote server.
Users are given the privilege to choose where they wish to upload their files or to choose their preferred upload location on the server.
There are five default folders or directories at the document root of the server for demonstration purpose.
The names of these directories are Directory A, Directory B, Directory C, Directory D and Directory E.
A user can upload his or her file to any of these directories, view the directories contents and also delete a file of his or her choice from any of the directories.
If a user fails to set the location where he or she intends to upload a file, the script will automatically upload the file to the document root of the server with an upload notification displayed on the screen to the user upon successful upload.
The script is written completely in PHP and its very easy to understand and customize.
Files are deleted simultaneously and directory contents recursively.




Download Live Demo


Example PHP script to encrypt and its encrypted result are shown below:

Upload Code:
<?php
$allowedExtensions = array("zip", "doc", "docx", "pdf", "txt", "jpeg", "jpg", "gif", "png");
function isAllowedExtension($fileName)
{
global $allowedExtensions;
return in_array(end(explode(".", $fileName)), $allowedExtensions);
}

if ($_POST["submitted"]=="yes")
{
$location_to_send_files = strip_tags($_POST['location_to_send_files']);

if (!empty($location_to_send_files) && file_exists($location_to_send_files) && is_dir($location_to_send_files)) //Check if the users sets a location where he or she intends to send file to and also check to be sure that the location exists and its a directory
{
if(isset($_FILES["file"]) && $_FILES["file"]["name"] != "")
{
$file = $_FILES['file'];

if(isAllowedExtension($file['name']))
{
if(strrpos($location_to_send_files, '/')) //Check if user adds slash at the end of the given directory on the server where he or she intends to send file to
{
$upload_location = $location_to_send_files; //Do not add slash since the user has already added slash at the end of the given directory
}
else
{
$upload_location = $location_to_send_files.'/'; //Add slash at the end of the given directory because the user did not add slash and that's cool :)
}

if(move_uploaded_file($_FILES["file"]["tmp_name"], "$upload_location".$_FILES["file"]["name"]))
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:blue;">' . $_FILES["file"]["name"] . ' uploaded successfully to '.$location_to_send_files.'&nbsp;<img src="ok.gif" align="absmiddle"></p>';
}
else
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;">' . $_FILES["file"]["name"] . ' was unsuccessful&nbsp;<img src="cancel.gif" align="absmiddle"></p>';
}
}
else
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;">You attached an invalid file format. Allowed file types are: "zip", "doc", "docx", "pdf", "txt", "jpeg", "jpg", "gif", "png" <img src="cancel.gif" align="absmiddle"></p>';
}
}
else
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;">Please browse for a file that you wish to upload to proceed <img src="cancel.gif" align="absmiddle"></p>';
}
}
else //Proceed and upload file to the root directory since the user did not set any location to upload file
{
if(isset($_FILES["file"]) && $_FILES["file"]["name"] != "")
{
$file = $_FILES['file'];

if(isAllowedExtension($file['name']))
{
if(move_uploaded_file($_FILES["file"]["tmp_name"], "".$_FILES["file"]["name"]))
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:blue;">' . $_FILES["file"]["name"] . ' uploaded successfully to the root directory since your <b>Upload Location</b> is empty or does not exist&nbsp;<img src="ok.gif" align="absmiddle"></p>';
}
else
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;">' . $_FILES["file"]["name"] . ' was unsuccessful&nbsp;<img src="cancel.gif" align="absmiddle"></p>';
}
}
else
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;">You attached an invalid file format. Allowed file types are: "zip", "doc", "docx", "pdf", "txt", "jpeg", "jpg", "gif", "png" <img src="cancel.gif" align="absmiddle"></p>';
}
}
else
{
$upload_status = '<p style="font-family: helvetica, arial, 'lucida grande', sans-serif; font-size:12px; color:red;">Please browse for a file that you wish to upload to proceed <img src="cancel.gif" align="absmiddle"></p>';
}
}
}
?>


That's it guys...


Views Today: 1
Total Views: 1028

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