File Upload using Php

A PHP script can be used with a HTML form to allow users to upload files to the server. Initially files are uploaded into a temporary directory and then relocated to a target destination by a PHP script.

<?php

if(isset($_POST['submit'])){
$name = $_FILES["file"]["name"];
//$size = $_FILES['file']['size']
//$type = $_FILES['file']['type']

$tmp_name = $_FILES['file']['tmp_name'];
$error = $_FILES['file']['error'];

if (isset ($name)) {
    if (!empty($name)) {

    $location = 'uploads/';

    if  (move_uploaded_file($tmp_name, $location.$name)){
        echo 'Uploaded';  
        }

        } else {
          echo 'please choose a file';
          }
    }
}
?>

<form action="" method="POST" enctype="multipart/form-data">
    <input type="file" name="file"><br><br>
    <input type="submit" name="submit" value="Submit">
</form>

or



<?PHP
if($_POST['upload']=="1")
{
$to="uploads/".$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'],$to);
echo "uploaded";

}
?>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="upload" value="1">
<input type="file" name="file">
<input type="submit" value="upload">
</form>

Comments

Popular posts from this blog

Script For Login, Logout and View Using PHP, MySQL and Bootstrap

Hyperledger Development with in 10 days — Day 6

Insert CheckBox and Radio button Data in MySQL Database Using PHP