Thứ Bảy, 27 tháng 4, 2019

How To Upload A Image Or File In PHP



Hey There I'm ZB King And In this post i'm gonna tell you that ow you can upload a image or a file in php so guy's if you don't know about what PHP means then please first learn php course and then read this article...So Now Let's Start That Follow Bellow


Also Read: https://codewithzb.blogspot.com/2019/04/bioscope-free-download.html

Add The HTML Form

These Codes Below Will Be Written In The HTML page So I Using The 'uploadimage.html'...


<!DOCTYPE html> <html><head><title> Uploading Image In PHP <title/><head/> <body> <form action="upload.php" method="POST" enctype="multipart/form-data">
<label for="image"> Select Image <label/>
<input type="file" id="image" name="image" />
<input type="submit" value="Submit" name="submit" /><form/> <body/><html/>


so guys as you see that we can created a form and we gave the action file "upload.php" and give the method as POST and also the most important thing for uploading file or a image in enctype that we have given above "multipart/form-data" ...
So guys and also we give a input in form tag that is describing the input name and type
we have give the name = "image" and type = "file" because the input is for image or a file and if that was not for a file or a image then we have to give text,email or number etc. and also we have given a submit button that if anyone click on that button then take action and we have named the submit button "submit" so nest thing we do is to add the php script so that's below

Adding PHP Script

Now We working int PHP file that's name "upload.php" because above we have given the action in "upload.php" file...
So now guys as you type the html form in your uploading html file then now we have to add the php
Now just follow bellow what i'm doing


<?php $con = mysqli_connect('localhost', 'root', '', 'images'); if(isset($_POST['submit']){ $image_name = $_FILES['image']['name']; $image_size = $_FILES['image']['size']; $image_tmp = $_FILES['image']['tmp_name']; if($profile_size > 25097152){ echo "<script>window.alert('File size must be excately 25 MB');</script>"; } if(empty($image_name)){ echo "<script>window.alert('Sorry, Fields Are Empty');</script>"; }else{ $insert_query = "INSERT INTO `image` (`id`, `image`) VALUES ('NULL', '$image_name')"; if(mysqli_query($con, $insert_query)){ echo '<script>window.alert("Added Successfully");</script>'; move_uploaded_file($image_tmp,"../img/".$image_name); } } }
?>

So Guys I Hope You Have Understand How To Upload Image Or File In PHP But I have To Explain That
So Guys I First Line We Have Explain The Image or File Name And It's Tmp And Also Size And
if($profile_size > 25097152){ echo "<script>window.alert('File size must be excately 25 MB');</script>"; }
Is Explaining That If The Size Is Above 25 MB Then It Will Not Submit It Show A Error


if(empty($image_name)){echo "<script>window.alert('Sorry, Fields Are Empty');</script>"; }
Is Explaining That If The Field Is Empty That Show The Error That "Sorry, Fields Are Empty";


else{ $insert_query = "INSERT INTO `image` (`id`, `image`) VALUES ('NULL', '$image_name')"; if(mysqli_query($con, $insert_query)){ echo '<script>window.alert("Added Successfully");</script>'; move_uploaded_file($image_tmp,"../img/".$image_name); } }

Is Explaining That If The Fields Are Not Empty Then Insert The Image In The Table named `image` id, image And Values Should Be NULL for id And $image_name for image
And If It's Added Then Show The Successful Message That "Added Successfully"...And Move The Image Where You Want Like I Have Linked The Folder "../img" And If Your Folder Name Is Images Then You can do Like This "../images" or "images"


0 nhận xét:

Đăng nhận xét