Move Zip File From Server To Server Using Ajax

This is a quick post on the topic of move zip file from server to server using Ajax. Recently, I have worked on the similar task for one of my client website. It may be helpful for someone so I decided to write a post on it.

For this article, I am assuming you have a submit button and you need to fetch zip file from another server. Let’s say server path for a zip file is http://example.com/archieves/test.zip.

Place the below code in your file. In this file, we are giving an Ajax call on the clicking of a button. To the Ajax file, we send a source path of a zip file.

<input type="button" class="submit" value="Submit">

<script src="https://code.jquery.com/jquery-1.10.2.js"></script>
<script type="text/javascript">
	$(function() {
		$('body').on('click', '.submit', function() {
			$.ajax({
				type: 'POST',
				url: 'ajax.php',
				data: {'source' : 'http://example.com/archieves/test.zip'},
				success:function(response) {
					alert(response);
				}
			});
		});
	});
</script>

Next, in ajax.php file we write actual code to move zip file from server to server. Below is the code in ajax.php file.

<?php
$arr_file = explode("/", $_POST['source']);
$filename = end($arr_file);

file_put_contents($filename, file_get_contents($_POST['source']));

echo 'success';
?>

That’s It. I hope you understand how to move zip file from server to server using Ajax. If you have any questions or suggestions please leave a comment below. Please read our related articles Ajax File Upload With PHP And jQuery and Drag And Drop File Upload Using JavaScript And PHP.

If you liked this article, then please subscribe to our YouTube Channel for video tutorials.

Leave a Reply

Your email address will not be published. Required fields are marked *