KevHopwood Posted August 29, 2013 Share Posted August 29, 2013 Im creating a CMS for my site and in my admin page I have an add page that adds new content to my site LOCATED HERE and have added a few form fields. 2 of these are: IMAGE URL (text box) & UPLOAD IMAGE (select file button) When I fill in all the fileds and select an image using IMAGE URL and hit add article, it works fine and my form is saved to my database and is then displayed on my site. When I fill in all the fileds and select an image using UPLOAD IMAGE and hit add article, it adds the image to my selected folder in my cpanel but DOES NOT ADD TO DATABASE. My question is: How can I get it to add to the database? and save the new images location to the image field on the database? I have followed this tutorial when adding the upload file button to my page. Please do not show me links on how to do this as I already have read through them but I stuggle when it comes to adding this to my code. my add.php code is this. <?phpsession_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['imageupload'])){$errors = array();$allowed_ext = array('jpg', 'jpeg', 'png', 'gif');$file_name = $_FILES['imageupload'] ['name'];$file_ext = strtolower (end (explode ('.', $file_name)));$file_size = $_FILES['imageupload'] ['size'];$file_tmp = $_FILES['imageupload'] ['tmp_name'];if (in_array ($file_ext, $allowed_ext) === false) {$errors[] = 'File extension not allowed';}if ($file_size > 2097152) {$errors[] = 'File size must be under 2mb';}if (empty($errors)) {if (move_uploaded_file($file_tmp, 'images/'.$file_name)) {echo 'File uploaded';}}else{foreach ($errors as $error)echo $error, '<br />';}}}$link = $_POST['link'];$category = $_POST['category'];$brand = $_POST['brand'];if (empty($title) or empty($content)) {$error = 'All Fields Are Required!';}else{$query = $pdo->prepare('INSERT INTO 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');}}?><?phpif (isset($_FILES['Filedata'])){// And if it was okif ($_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 to 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" enctype="multipart/form-data"><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. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/ Share on other sites More sharing options...
fastsol Posted August 29, 2013 Share Posted August 29, 2013 You're not defining or reassigning the $image for a uploaded file. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447339 Share on other sites More sharing options...
KevHopwood Posted August 29, 2013 Author Share Posted August 29, 2013 So what do I need to add to do that? Help please. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447352 Share on other sites More sharing options...
fastsol Posted August 29, 2013 Share Posted August 29, 2013 (edited) if (move_uploaded_file($file_tmp, 'images/'.$file_name)) { echo 'File uploaded'; $image = $file_name; } Edited August 29, 2013 by fastsol Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447353 Share on other sites More sharing options...
KevHopwood Posted August 29, 2013 Author Share Posted August 29, 2013 (edited) I'll give that a try. Edited August 29, 2013 by KevHopwood Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447371 Share on other sites More sharing options...
KevHopwood Posted August 29, 2013 Author Share Posted August 29, 2013 This adds the article but does not add the image to my site. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447380 Share on other sites More sharing options...
KevHopwood Posted August 29, 2013 Author Share Posted August 29, 2013 What I need is for the file to save to the images folder like ur does then automatically add the saved location to the URL field so that that can be read correctly. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447382 Share on other sites More sharing options...
KevHopwood Posted August 30, 2013 Author Share Posted August 30, 2013 Can anyone help? Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447462 Share on other sites More sharing options...
mac_gyver Posted August 31, 2013 Share Posted August 31, 2013 Can anyone help? you haven't clearly stated exactly what the current problem is (we only see the information that you put in your posts.) you previously stated that the file was being uploaded, but not being added to the database table. the code change that fastsol handed you would have fixed that problem. if it didn't correct that problem, you would need to tell us what it did do, and if the problem changed, you would need to tell us that too. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447566 Share on other sites More sharing options...
KevHopwood Posted September 2, 2013 Author Share Posted September 2, 2013 I think it has all been explained above. Just not that clear. In a nut shell. The upload button with the code above works by adding the image to my images folder so thank you for that. But on a seperate note, let's say for example that I upload an image called "image123.jpg". It saves the image as image.jpg but not http://www.mysite.com/images/image123.jpg. Which means it does not display on my site. How can I upload the "image123.jpg" and have it saved In mysql as the full site link? Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447848 Share on other sites More sharing options...
fastsol Posted September 2, 2013 Share Posted September 2, 2013 if (move_uploaded_file($file_tmp, 'images/'.$file_name)) { echo 'File uploaded'; $image = 'www.mysite.com/images/'.$file_name; } See how easy that is when you actually describe the issue properly. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447849 Share on other sites More sharing options...
KevHopwood Posted September 3, 2013 Author Share Posted September 3, 2013 Ok. Thank you. But how do I get it to rename the saved image to the brand name because on my iPad every image is uploaded as image.jpg. So I would like to rename it to the brand name. Is that possable? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447902 Share on other sites More sharing options...
Solution fastsol Posted September 3, 2013 Solution Share Posted September 3, 2013 Ok, you need to move the $brand var higher in the script so that it's defined before the move_uploaded_file, good place would likely be where you define $title. $brand = $_POST['brand']; // Put this up by where you define $title $up_file = $brand.'.'.$file_ext; if (move_uploaded_file($file_tmp, 'images/'.$up_file)) { echo 'File uploaded'; $image = 'www.mysite.com/images/'.$up_file; } Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447904 Share on other sites More sharing options...
KevHopwood Posted September 3, 2013 Author Share Posted September 3, 2013 Thank you so much. This worked fine. Quote Link to comment https://forums.phpfreaks.com/topic/281670-article-adds-using-a-url-but-not-my-upload-box/#findComment-1447992 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.