Uploading Images Into A Specific Folder Php
So I'm doing an online store for a project and I need help doing one thing: after I add a product in the admin panel, I have to manualy pick the image and paste on the specific fol
Solution 1:
I'll leave here an example on how to upload a photo to a folder:
HTML:
<html><head><title>test</title></head><bodybgcolor="black"><formaction="upload.php"method="post"enctype="multipart/form-data"><inputtype="file"name="file"id="file"><inputtype="submit"value="Submit"></form></body>
PHP:
<?php
move_uploaded_file($_FILES["file"], 'p/file.jpg');
?>
$_FILES["file"]
= The file you get from the page;
p/file.jpg
= The path of where you upload the photo
To upload to more folders just copy paste the code like so:
move_uploaded_file($_FILES["file"], 'firstFolder/file.jpg');
copy('firstFolder/file.jpg', 'secondFolder/file.jpg');
copy('firstFolder/file.jpg', 'thirdFolder/file.jpg');
Solution 2:
Check the following code:
<?php
session_start();
include_once("config.php");
$code = ($_POST['product_code']);
$name = ($_POST['product_name']);
$cat = ($_POST['category']);
$cat_type = ($_POST['cat_type']);
$fees = ($_POST['price']);
$quant = ($_POST['product_qty']);
$photo = ($_POST['product_img_name']);
$sql = "INSERT INTO products(product_code,product_name,category,cat_type,price,product_qty,product_img_name) VALUES ('$code','$name','$cat','$cat_type','$fees','$quant', '$photo')";
if (mysqli_query($mysqli,$sql)){
$dir = false;
if($cat_type == 'Drones'){
$dir = 'res/got/';
}
elseif($cat_type == 'Computers'){
$dir = 'res/hp/';
}
elseif($cat_type == 'Laptops'){
$dir = 'res/hg/';
}
if($dir){
move_uploaded_file($_FILES["file"], $dir.time().'_file.jpg');
}
echo'<script type="text/javascript">alert("New product added. Add more!");</script>';
header("Location:http://localhost/Fly With US/add_product.php");
}
else{
echo'<script type="text/javascript">alert("Error Occured");</script>';
header("Location:http://localhost/Fly With US/add_product.php");
}
?>
Post a Comment for "Uploading Images Into A Specific Folder Php"