HOW TO UPLOAD A FILE IN PHP

Uploading is a process of transfering (data) from one computer to another, typically to one that is larger or remote from the user or functioning as a server.

Lets understand it by writting a PHP code for uploading a file:

<?php
if(isset($_FILES[‘image’])){
$errors= array();
$file_name = $_FILES[‘image’][‘name’];
$file_size =$_FILES[‘image’][‘size’];
$file_tmp =$_FILES[‘image’][‘tmp_name’];
$file_type=$_FILES[‘image’][‘type’];
$file_ext=strtolower(end(explode(‘.’,$_FILES[‘image’][‘name’])));

$expensions= array(“jpeg”,”jpg”,”png”);

if(in_array($file_ext,$expensions)=== false){
$errors[]=”extension not allowed, please choose a JPEG or PNG file.”;
}

if($file_size > 2097152){
$errors[]=’File size must be excately 2 MB';
}

if(empty($errors)==true){
move_uploaded_file($file_tmp,”images/”.$file_name);
echo “Success”;
}
else{
print_r($errors);
}
}
?>
<html>
<body>

<form action=”” method=”POST” enctype=”multipart/form-data”><input type=”file” name=”image” />

<input type=”submit”/></form></body></html>

One can also use the concept of switch cases for implementing the above code.

SynapseIndia claim top notch services in the field of IT sectors within tight deadlines.Employee at SynapseIndia never gets complaints from clients for deadlines, So, it is one of the trusted offshore software development company, that is easily completed the projects with a minimum span of time. IT Mentor & an Entrepreneur SynapseIndia CEO Shamit Khemka is responsible to make this organization robust and reliable.You can check feedback and reviews given by clients on various projects completed by us.