Site icon SmartTutorials.net

php file upload script

Using following script we can upload files, photos, pictures, pdf files, images and etc… from your local computer to remote server. I explained all the $_FILES super global variables properties with example.

here is script with html form design

<form action="mkdir.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>
<?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 " IF 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 " Else The file ".basename( $_FILES['uploaded']['name']). " has been uploaded".'<br>';

		 echo $_FILES['uploaded']['tmp_name'].'<br>';
		 echo $_FILES['uploaded']['type'].'<br>';
		 echo $_FILES['uploaded']['size'].'<br>';

 }
 else {
 echo "Sorry, there was a problem uploading your file.";
 }

	 }

	}

?>

 

where i am uploading image (thumbnail.jpg) to server

$_FILES[‘uploaded’][‘tmp_name’]  – which outputs temporary file address (C:\xampp\tmp\php569.tmp) , as well  php assign temporary name to the image (thumbnail.jpg) to php569.tmp. when you uploading files from local computer to server, php temporary stores that files in your computer local hard drive (C:\xampp\tmp\php569.tmp).
$_FILES[‘uploaded’][‘type’]  – which outputs the property of file you uploaded (image/jpeg)
$_FILES[‘uploaded’][‘size’]   – which outputs the size of the files you uploaded (7293 bytes)

$_FILES[‘uploaded’][‘name’]   – which outputs the name of the files you uploaded (thumbnail.jpg)

php file upload

.
Exit mobile version