Easy File Uploads in PHP Application.
There are some simple steps, you can done file upload in PHP applications
Step 1 :
Create your Form, Additionally add attributes in form element.
Step 2:
When your form submits, action page will be redirected, that page received all form values
receive normal form values as usual format $var=$_POST["formcontrolname"]; metods
Note 1:
This section Using ,PHP File uplaods, have there are 4 methods
$_FILE["file"]["name"] // This is choosing filename
$_FILE["file"]["type"] // This file type, png,pdf,doc,jpg
$_FILE["file"]["size"] // this is file size , Its take KB, you can convert MB format
$_FILE["file"]["tmp_name"] // This is temp file,
Note 2:
If you restricted, allow some format files only uploaded in server, you can check if condition file type is JPG then upload files,
if you restricted, allow some limit size files only uploaded in server, you can check if condition file size is "1000KB" then upload files,
if your not check, that is any kind of files uploaded then , need not to check any conditions
Step 3:
Next create uploading file to save some directory or folder, that folder create in your root directory, and assign target file path with uploading filename,
$uploadfildir="../products/".$_FILE["file"]["name"];
$target_file = $uploadfildir;
Step 4:
Finally you can store values in database, write connection and query methods, if query executes, then file is uploaded in your server
use move_uploaded_file() method,
Step 5
Finish,
Source Code
The above steps are using below examples, you can copy paste run your server,
Create products folder in root directory in your server,
Create products table in mysql,
then create forms,
product.php
<form role="form" method="post" action="product_add.php" enctype="multipart/form-data">
Category Name <input type="text" name="categoryname" placeholder="Enter Category Name">
Product Name<input type="text" name="productname" placeholder="Enter Product Name">
Product Picture<input type="file" name="file" placeholder="Choose file Name">
<input type="submit" name="submit" value="ADD PRODUCT" class="btn btn-default"/>
</form>
in action page, receive all form values, and store mysql table, and file uploaded in products folder,
product-save.php
<?php
$categoryname=$_POST["categoryname"];
$productname=$_POST["productname"];
$pic="JS-".$productname."-".$_FILES["file"]["name"];
$uploadfildir="../products/".$pic;
$target_file = $uploadfildir;
include("config.php");
$insertsql="insert into products values('','$categoryname','$productname','$productpic')";
$insertsqlres=mysqli_query($con,$insertsql);
if($insertsqlres)
{
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
}
}
else
{
echo "Products not added!";
}
?>
There are some simple steps, you can done file upload in PHP applications
Step 1 :
Create your Form, Additionally add attributes in form element.
Step 2:
When your form submits, action page will be redirected, that page received all form values
receive normal form values as usual format $var=$_POST["formcontrolname"]; metods
Note 1:
This section Using ,PHP File uplaods, have there are 4 methods
$_FILE["file"]["name"] // This is choosing filename
$_FILE["file"]["type"] // This file type, png,pdf,doc,jpg
$_FILE["file"]["size"] // this is file size , Its take KB, you can convert MB format
$_FILE["file"]["tmp_name"] // This is temp file,
Note 2:
If you restricted, allow some format files only uploaded in server, you can check if condition file type is JPG then upload files,
if you restricted, allow some limit size files only uploaded in server, you can check if condition file size is "1000KB" then upload files,
if your not check, that is any kind of files uploaded then , need not to check any conditions
Step 3:
Next create uploading file to save some directory or folder, that folder create in your root directory, and assign target file path with uploading filename,
$uploadfildir="../products/".$_FILE["file"]["name"];
$target_file = $uploadfildir;
Step 4:
Finally you can store values in database, write connection and query methods, if query executes, then file is uploaded in your server
use move_uploaded_file() method,
Step 5
Finish,
Source Code
The above steps are using below examples, you can copy paste run your server,
Create products folder in root directory in your server,
Create products table in mysql,
then create forms,
product.php
<form role="form" method="post" action="product_add.php" enctype="multipart/form-data">
Category Name <input type="text" name="categoryname" placeholder="Enter Category Name">
Product Name<input type="text" name="productname" placeholder="Enter Product Name">
Product Picture<input type="file" name="file" placeholder="Choose file Name">
<input type="submit" name="submit" value="ADD PRODUCT" class="btn btn-default"/>
</form>
in action page, receive all form values, and store mysql table, and file uploaded in products folder,
product-save.php
<?php
$categoryname=$_POST["categoryname"];
$productname=$_POST["productname"];
$pic="JS-".$productname."-".$_FILES["file"]["name"];
$uploadfildir="../products/".$pic;
$target_file = $uploadfildir;
include("config.php");
$insertsql="insert into products values('','$categoryname','$productname','$productpic')";
$insertsqlres=mysqli_query($con,$insertsql);
if($insertsqlres)
{
if (move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))
{
echo "The file ". basename( $_FILES["file"]["name"]). " has been uploaded.";
}
}
else
{
echo "Products not added!";
}
?>
No comments:
Post a Comment