Simple PHP Code To Upload Files

  • Share
  • CevherShare
  • Share

In this tutorial i have given the simple php code to upload files into server directory. To upload file <form> should contain  enctype=”multipart/form-data”, input type is file.

$_FILES['photo']['name'] – This will give the name of the uploaded file

$_FILES['photo']['tmp_name'] – Temporary name will be created for file to be uplaoded. 

$_FILES["file"]["type"] - Returns file type i.e., format of the file. If the file is image (pic.jpg), then this will return .jpg

$_FILES["file"]["size"] - Returns size of the file, which we have uploaded.

$_FILES["file"]["error"] – Error generated during uploading the file.

move_uploaded_file() – this php function will be uploaded into the server directory 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<html>
<head>
</head>
<body>
<?php
if(isset($_POST['submit']))
{
$pic=$_FILES['photo']['name'];
$picc=$_FILES['photo']['tmp_name'];
$tar="img/".$pic;
move_uploaded_file($picc,$tar); // upload selected image into server diirectory
}
?>
<form action="a.php" method="post" enctype="multipart/form-data">
<input type="file" name="photo">
<input type="submit" name="submit">
</form>
</body>
</html>

In this file upload tutorial, the disadvantage is when file is uploading  if any of the file’s name present in the directory is same as uploading file’s name, then this script will thought that file to be upload is already present in the directory and won’t upload the file.

prasad

About prasad

Prasad K has written 111 post in this blog.

One Response to Simple PHP Code To Upload Files

  1. Gooooooooooooooooooooooood and easy tutorial. bunch of thank buddy