Loading.......
Loading...

Move File from One Folder to the other using PHP

Posted on 23rd October, 2021
MOVE FILE SCRIPT TRANSFER FILE SCRIPT PHP FILE UPLOAD FILE UPLOAD SCRIPT
This tutorial teaches you how to Move or Transfer Files from one folder on your web server to the other using PHP

The tutorial is beneficial to as many whose current or future projects or applications requires moving or transferring files between folders on their web server.

The php move_uploaded_file() function is used to upload a file from your local machine to your web server when you try to add a new file and the php rename() function is used to move/transfer the files from one folder to the other on your web server.

FILE UPLOAD SCRIPT

<?php
// Check to be sure that the only request we can process is POST REQUEST
if(isset($_SERVER["REQUEST_METHOD"]) && strip_tags($_SERVER["REQUEST_METHOD"]) == strip_tags("POST"))
{
	// Be default, files are uploaded to folder one
	if(isset($_POST['submit']))
	{
		// Variables declaration
		$filename = $_FILES['file']['name'];
		$filesize = $_FILES['file']['size'];
		$filetemp = $_FILES['file']['tmp_name'];
	
		if($filesize < 1000000) // Maximum allowed file size is 1MB
		{
			$file = explode(".", $filename);
			$file_ext = end($file);
			$ext = array("png", "gif", "jpg", "jpeg"); // Allowed file types
	
			// If the file trying to upload is in the list of allowed file types, proceed to upload
			if(in_array($file_ext, $ext))
			{
				$location = "folder1/".$filename;
				if(move_uploaded_file($filetemp, $location))
				{
					echo "<script>alert('Great, the file was uploaded successfully.')</script>";
					echo "<script>window.location = 'index.php'</script>";
				}
			}
			else // Else to not upload if its not in the list of allowed file types
			{
				echo "<script>alert('Sorry, you tried to upload a wrong file type\\n Allowed file types are png, gif, jpg, jpeg')</script>";
				echo "<script>window.location = 'index.php'</script>";
			}
		}
		else
		{
			echo "<script>alert('Sorry, the file size you tried to upload was too large.\\nMaximum allowed file size is 1MB')</script>";
			echo "<script>window.location = 'index.php'</script>";
		}
	}
	else
	{
		echo "<script>alert('No proper request was made')</script>";
		echo "<script>window.location = 'index.php'</script>";
	}
}
else 
{
	// Deny access if the request brought to this page is not a POST REQUEST
	die('Access Denied');
}
?>


MOVE / TRANSFER FILE BETWEEN THE FOLDERS SCRIPT

<?php
// Check to be sure that the only request we can process is POST REQUEST
if(isset($_SERVER["REQUEST_METHOD"]) && strip_tags($_SERVER["REQUEST_METHOD"]) == strip_tags("POST"))
{
	if(isset($_POST['transfer']))
	{
		$old_file_location = trim(strip_tags($_POST['old_location'])); // This is the old file location
		$new_file_location = trim(strip_tags($_POST['new_location'])); // This is the new file location
	
		if( rename( $old_file_location, $new_file_location ) )
		{
			echo "<script>alert('The file has been transferred successfully to ".$new_file_location."')</script>";
			echo "<script>window.location = 'index.php'</script>";
		}
		else
		{
			echo "<script>alert('".$old_file_location." could not be transferred at the moment, please try again')</script>";
			echo "<script>window.location = 'index.php'</script>";
		}
	}
	else
	{
		echo "<script>alert('No proper request was made')</script>";
		echo "<script>window.location = 'index.php'</script>";
	}
}
else 
{
	// Deny access if the request brought to this page is not a POST REQUEST
	die('Access Denied');
}
?>
The script is very easy to understand and customize with programming code comments to ease usability.
To see the system in action, please click on the Live Demo button below and click on the Download button to download the script if you like it.

Thank You!
The Vasplus Team.
Post Comment
Press Enter to send
No comment yet.

Submit your Job or Project Today!

We can help you turn your idea into reality, take over your existing project, or extend your current development team.

Submit your idea job or project below and we will follow up with you shortly.

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.

Please Wait....
Please Wait...