Wednesday, 7 October 2015

How to upload a file in PHP using Xampp


1) Go to C:\xampp\htdocs and make a folder name it how_to_upload_file_in_php
2) Now make a file naming upload_form.php and paste the following code in it

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>
<body>
<form enctype="multipart/form-data" action="upload_file.php" method="POST">
Choose a file to upload: <input type="file" name="file" size="50" />
<input type="submit" value="Upload File" />
</form>
</body>
</html>

3) Now make a php file to upload the file upload_file.php and paste the following code

<?php
$targetfolder = "upload_folder/";
$targetfolder = $targetfolder . basename( $_FILES['file']['name']) ;
if(move_uploaded_file($_FILES['file']['tmp_name'], $targetfolder))
{
echo "The file ". basename( $_FILES['file']['name']). " is uploaded";
}
else {
echo "Error occured while uploading file";
}
?>

4) Now make a folder in your root directory where all the files will upload naming it upload_folder



Now run your code by this url: http://localhost/toturial_website/how_to_upload_file_in_php/upload_form.php

Your file will upload in the upload_folder. Please check it. Feel free to ask any query