Move file
By: Daniel

This tutorial / piece of code will show you how to move a file on your website to another location. For instance you have a web application to approve or deny file uploads. Using this function will make make it easier to process this task.

<?php
function moveFile($file, $newFolder) {
        // Check if the move to directory actually exists if not return an error
	if (!is_dir($newFolder)) {
		return 'Move to folder '.$newFolder.' does not exist';
	}
	
        // Make sure that are $newFolder variable ends with a /
	$newFolder = (in_array(substr($newFolder, -1), array('/', '\')) ? $newFolder : $newFolder.'/');
	
        // Check to make sure the file you want to move actually exists
	if (is_file($file)) {
                // Get the filename of the file to be moved
		$filename = basename($file);
		rename($file, $newFolder.$filename);
		return 'File '.$filename.' moved to '.$newFolder;
	}
	return 'File '.$file.' does not exist';
}
?>

Then to use the code you would use something like:

<?php
moveFile('file1.txt', 'movehere/');
?>

Enjoy!

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 3.00 out of 5)
Loading ... Loading ...

Leave a Reply

geovisitors