Skip to content

Instantly share code, notes, and snippets.

@ronakjain2012
Last active July 1, 2016 07:26
Show Gist options
  • Save ronakjain2012/c71cfec1261508348f3ff4325a0f3bf5 to your computer and use it in GitHub Desktop.
Save ronakjain2012/c71cfec1261508348f3ff4325a0f3bf5 to your computer and use it in GitHub Desktop.
Uploading File using php (Single File)
<!-- HTML PART -->
<form enctype="multipart/form-data" action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" name="changer">
<input name="MAX_FILE_SIZE" value="1002400" type="hidden"> <!-- File size -->
<input name="image" accept="image/jpeg" type="file" value="">
<input value="Submit" type="submit">
<?php
if(!isset($_FILES['image']))
{
echo '<p>Please select a file</p>';
}
else
{
try{
upload();
echo '<p>Thank you for submitting</p>';
}
catch(Exception $e)
{
echo '<h4>'.$e->getMessage().'</h4>';
}
}
function upload() {
$con = mysqli_connect("localhost","root","root","test");
if(!$con) {
echo "Error -> ".mysqli_connect_error();
die();
}
echo "Connected ! <br>";
print_r($_FILES['image']['size']);
if($_FILES['image']['size'] > 100) {
$target_dir = "uploads/";
$target_file = $target_dir.basename($_FILES['image']['name']);
print_r("<br>".$target_file);
$name = $_FILES['image']['name'];
print_r("<br>".$name);
$size = getimagesize($_FILES['image']['tmp_name']);
$size = $size['mime'];
print_r("<br>".$size);
$imageType = pathinfo($target_file,PATHINFO_EXTENSION);
print_r("<br>".$imageType);
if($imageType == "jpg" || $imageType == "png" || $imageType == "jpeg") {
if (file_exists($target_file)) {
echo " <script> alert(\"Sorry, file already exists\") </script>";
}
else {
$statement = "insert into img_upload (img_path,name,type) values('$target_file','$name','$size')";
mysqli_query($con,$statement);
move_uploaded_file($_FILES['image']['tmp_name'], $target_file);
echo "<br /> <br />The file ". basename( $_FILES['image']['name']). " has been uploaded.";
}
}
else { echo "Not Supported Extention "; }
}
mysqli_close($con);
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment