Jump to content

article adds using a URL but not my upload box


Recommended Posts

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.





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'];
$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';
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!';
$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);

header('location: index.php');


if (isset($_FILES['Filedata']))
// And if it was 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 to script location

$copy = copy($_FILES['Filedata']['tmp_name'], $targetpath);

<title>Add Article</title>
<link rel="stylesheet" href="../other.css" />

<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" />


header('location: index.php');



please help. thanks.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
Link to comment
Share on other sites


This topic is now archived and is closed to further replies.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.