Site icon SmartTutorials.net

How to Upload Files using PHP…..

Here is simple coding in PHP to upload your files to server. Just follow this simple steps to upload your files. You can upload your all format of files, photos and pictures from local computer to server using this following php scripts.

Step 1: Create an HTML form to select file to upload.

Html Form To Upload File

this is above html form code

<form action="upload.php" method="post" enctype="multipart/form-data">

<table border="0" align="center" bgcolor="#353535" cellpadding="3">
  <tr>

   <td bgcolor="#FFFFFF" colspan="2" align="center">
    <p align="center" style="font-style:italic; font-weight:bold">HTML Form to Upload File</p>
    </td>
  </tr>o

  <tr>
    <td bgcolor="#FFFFFF" width="50%">Select File to Upload</td>
    <td bgcolor="#FFFFFF" width="50%">
    <input type="file" name="uploaded"/>

    </td>
  </tr>
  <tr>

   <td bgcolor="#FFFFFF" colspan="2" align="center">
    <input type="submit" name="submit" value="submit"/>
    </td>
  </tr>

</table>

</form>

In the form you need specify some important attributes to process your file

action=”upload.php’ – this php file process the uploaded file.

method=’POST’ – you passing your files using post method.

enctype=”multipart/formdata” – this content type used when you sending files.

Step 2: Create upload.php to process your files.

<?php

if(isset($_POST['submit'])){

if(!is_dir('test')){

	 mkdir('test');
	$target='test/'.basename($_FILES['uploaded']['name']);
	if(move_uploaded_file($_FILES['uploaded']['tmp_name'],$target)){

		 echo "The file ".basename( $_FILES['uploaded']['name']). " has been uploaded";
 }
 else {
 echo "Sorry, there was a problem uploading your file.";
 }
}
 else {

	 $target='test/'.basename($_FILES['uploaded']['name']);
	if(move_uploaded_file($_FILES['uploaded']['tmp_name'],$target)){

		 echo "The file ".basename( $_FILES['uploaded']['name']). " has been uploaded";
 }
 else {
 echo "Sorry, there was a problem uploading your file.";
 }

	 }

	}

?>

where

mkdir(‘test’)- is function which creates directory named ‘test’ during run time.

$_FILES[‘uploaded’][‘tmp_name’] – which outputs temporary address with filename

move_uploaded_file() – function which moves temporary file into targeted folder on the server.

 

 .

Exit mobile version