mysql - Storing images to directory using php -
i'm creating admin side website.
i have created add page , within page form few fields. 2 of these upload button me upload image. , other 1 fill box me enter url.
i have set both of them update same mysql field , fill box priority when both entered.
however. search box fine saves link correctly selecting image using select box saves file name in field in mysql example: xxxxxxxxxxxxx.png
so my website displaying these images, not display uploaded ones. because these saved on computer , not on cpanel.
so question is: how can upload image using upload box , have save /images folder have mysql display url link in relevant field?
my add.php code this:
<?php session_start(); include_once('../include/connection.php'); if (isset($_session['logged_in'])){ if (isset($_post['title'], $_post['content'])) { $title = $_post['title']; $content = nl2br($_post['content']); if (!empty($_post['image'])) { $image = $_post['image']; } else { $image = $_post['imageupload']; if (isset($_files['filedata'])) { $filename = $_files['filedata']['name']; $targetpath = "/images/" . $filename; //target directory relative script location $copy = copy($_files['filedata']['tmp_name'], $targetpath); if (!$copy) $error = "image not uploaded successfully"; } } $link = $_post['link']; $category = $_post['category']; $brand = $_post['brand']; if (empty($title) or empty($content)) { $error = 'all fields required!'; }else{ $query = $pdo->prepare('insert mobi (promo_title, promo_content, promo_image, promo_link, promo_cat, promo_name) values(?, ?, ?, ?, ?, ?)'); $query->bindvalue(1, $title); $query->bindvalue(2, $content); $query->bindvalue(3, $image); $query->bindvalue(4, $link); $query->bindvalue(5, $category); $query->bindvalue(6, $brand); $query->execute(); header('location: index.php'); } } ?> <?php if (isset($_files['filedata'])) { // , if ok if ($_files['filedata']['error'] !== upload_err_ok) exit('upload failed. error code: ' . $_files['image']['error']); $filename = $_files['filedata']['name']; $targetpath = "../img/news/" . $filename; //target directory relative script location $copy = copy($_files['filedata']['tmp_name'], $targetpath); } ?> <html> <head> <title>add article</title> <link rel="stylesheet" href="../other.css" /> </head> <body> <div class="container"> <a href="index.php" id="logo"><b>← back</b></a> <br /> <div align="center"> <h4>add article</h4> <?php if (isset($error)) { ?> <small style="color:#aa0000;"><?php echo $error; ?></small><br /><br /> <?php } ?> <form action="add.php" method="post" autocomplete="off"> <input type="text" name="title" placeholder="title" /><br /><br /> <textarea rows="15" cols="50" placeholder="content" name="content"></textarea><br /><br /> <input name="imageupload" type="file" id="image" placeholder="imageupload" /> <input type="text" name="image" placeholder="image" /><br /><br /> <input type="link" name="link" placeholder="link" /><br /><br /> <input type="category" name="category" placeholder="category" /><br /><br /> <input type="category" name="brand" placeholder="brand" /><br /><br /> <input type="submit" value="add article" /> </form> </div> </div> </body> </html> <?php }else{ header('location: index.php'); } ?>
please help. thanks.
try having @ manual uploading files.
Comments
Post a Comment