PHP mysql image file upload || PHP File Uploading
×

PHP / MySql Image Upload

4296

In PHP, users can upload files to the server in easy way. To upload files, the PHP script used with an HTML form. Initially, files are uploaded into a temporary directory and then moved to a target destination by a PHP script.

Information in the phpinfo.php page depicts the temporary directory that is used for the process of file uploads as upload_tmp_dir and the utmost allowed size of files that can be uploaded is declared as upload_max_filesize. These parameters are placed into the php.ini PHP configuration file.

The procedure of uploading a file follows these steps -

  • The user opens the page containing an HTML form featuring a browse button, text files and a submit button.
  • The user presses the browse button and picks a file to upload from the local computer.
  • The complete path to the picked file appears in the text field, then the user presses the submit button.
  • The chosen file is sent to the temporary directory on the server.
  • The PHP script that was stated as the form handler in the form's action feature checks that the file has arrived and then copies the file into an intended directory.
  • The PHP script confirms the victory to the user.

As usual, when writing files, it is essential for both the temporary and final locations to have consents set that allow the file writing. If either is fixed to be read-only then the process will be unsuccessful.

An uploaded file could be an image file or text file or any document.

<?php
$connection=mysqli_connect("localhost","db_user","db_password","db_name");
if(isset($_POST['submit']))
{
$g1=time().$_FILES['image1']['name'];

// time() use for generate the unique name for image

$sql="insert into posts set image1='$g1'";
move_uploaded_file($_FILES['image1"]["tmp_name"],"upload/".$g1);

// upload is folder name, where your image will save

$data=mysqli_query($connection,$sql);
if($data)
{

?>

<script language="javascript">
alert("Your Image Uploaded Successfully.");
</script>

<?php

}
else
{
echo "ERROR";
}
}
?>

HTML FORM

<form method="post" action="" enctype="multipart/form-data">
<input type="file" name="image1" placeholder="upload image here" >
<br><br>
<input type="submit" name="submit">
</form>


Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments