javascript - Image and data upload to database,using php -
got code upload new products db since im new php,ive got copied several sources seems on place,and syntax alright,but still no success in uploading image & inserting data db
upload.php page -
<?php require "dbconn.php"; require "functions.php"; if(isset($_post['submit'])) { $product_name = strip_tags($_post['name']); $product_price = strip_tags($_post['price']); $category = strip_tags($_post['category']); } $target_dir = "img/'.$category.'/"; $target_file = $target_dir . basename($_files["filetoupload"]["name"]); $uploadok = 1; $imagefiletype = pathinfo($target_file,pathinfo_extension); // check if image file actual image or fake image if(isset($_post["submit"])) { $check = getimagesize($_files["filetoupload"]["tmp_name"]); if($check !== false) { echo "file image - " . $check["mime"] . "."; $uploadok = 1; } else { echo "file not image."; $uploadok = 0; } } // check if file exists if (file_exists($target_file)) { echo "sorry, file exists."; $uploadok = 0; } // check file size if ($_files["filetoupload"]["size"] > 500000) { echo "sorry, file large."; $uploadok = 0; } // allow file formats if($imagefiletype != "jpg" && $imagefiletype != "png" && $imagefiletype != "jpeg" && $imagefiletype != "gif" ) { echo "sorry, jpg, jpeg, png & gif files allowed."; $uploadok = 0; } // check if $uploadok set 0 error if ($uploadok == 0) { echo "sorry, file not uploaded."; // if ok, try upload file } else { if (move_uploaded_file($_files["filetoupload"]["tmp_name"], $target_file)) { insert_new_product($product_name, $product_price, $target_file, $category); header("location: add_product.php?cname=$category"); } else { echo "sorry, there error uploading file."; } } ?>
html
<table> <form action="upload.php" method="post" enctype="multipart/form-data"> <tr> <td> <input name="name" type="text">name:</br> </td> </tr> <tr> <td> <input name="price" type="text">price:</br> </td> </tr> <tr> <td> <input name="category" value="<?php echo $category; ?>" type="hidden"> </td> </tr> <tr> choose file: <input type="file" name="filetoupload" id="filetoupload"> <input type="submit" value="upload image" name="submit"> </form> </table>
i advice change
$target_dir = "img/'.$category.'/";
to
$target_dir = "img/".$category."/";
also, try seeing request going when submit form. need base tag ideally specify php controller form action without full path.
Comments
Post a Comment