Saturday, 17 August 2013

Images not uploading or being saved to database

Images not uploading or being saved to database

Summary: There are NO error messages, just no images are being uploaded,
and no values are being stored in post_image, post_image2, or post_image3.
So I have a site where members can post topics like a forum, I have added
the feature to upload image attachments with topics that display in the
view topic page as well as storing the image on the server as
"uploads/images/(username/".
I am trying to incorporate this into the posts section so users can post
images as responses, for for some reason I it is not storing or uploading
the images, here is the section of the page that makes replies.
<form method="post" action="add_reply.php" enctype="multipart/form-data">
<textarea name="post_content" cols="45" rows="3"
id="post_content"></textarea><br>
<input type="file" name="post_image" value="Browse"
accept="image/*">
<div id="toggleText" style="display: none">
<input type="file" name="post_image2" size="15px"
accept="image/*">
<br>
<input type="file" name="post_image3" size="15px"
accept="image/*">
</div>
<div id="more-less-images"><a id="displayText"
href="javascript:toggle();">More
images</a></div>
And here is the "add_reply.php" which processes it.
// get values that sent from form
$post_image = null;
//Get Timestamp to add in image name.
$date = new DateTime();
$timestamp = $date->getTimestamp();
//Check upload folder is created
if (!file_exists('uploads')) {
mkdir('uploads', 0777, true);
}
//Check user directory is created or not.
$user = $user_data['username'];
$uid = $_SESSION['user_id'];
if (!file_exists('uploads/images/' . $user)) {
mkdir('uploads/images/' . $user, 0777, true);
}
//Below Code will copy all three images to the Upload directory
and add path to the three variables.
$imageFile = null;
$imageFile2 = null;
$imagefile3 = null;
if (isset($_FILES['post_image']) && $_FILES['post_image']['size']
> 0) {
$info = pathinfo($_FILES['post_image']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $uid. $timestamp ."1." . $ext;
$target = 'uploads/images/' . $user . "/" . $newname;
move_uploaded_file($_FILES['post_image']['tmp_name'], $target);
$imageFile = $target;
}
if (isset($_FILES['post_image2']) &&
$_FILES['post_image2']['size'] > 0) {
$info = pathinfo($_FILES['post_image2']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $uid. $timestamp ."2." . $ext;
$target = 'uploads/images/' . $user . "/" . $newname;
move_uploaded_file($_FILES['post_image2']['tmp_name'], $target);
$imageFile2 = $target;
}
if (isset($_FILES['post_image3']) &&
$_FILES['post_image3']['size'] > 0) {
$info = pathinfo($_FILES['post_image3']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $uid. $timestamp ."3." . $ext;
$target = 'uploads/images/' . $user . "/" . $newname;
move_uploaded_file($_FILES['post_image3']['tmp_name'], $target);
$imageFile3 = $target;
}
$post_content = htmlspecialchars($_POST['post_content']);
$post_by = $_SESSION['user_id'];
$invisipost = isset($_POST['invisipost']) ? $_POST['invisipost'] : 0 ;
// Insert answer
$sql2="INSERT INTO `posts`(post_topic, post_content, post_image,
post_image2, post_image3, post_date, post_by, invisipost)VALUES('$id',
'$post_content',
'$imageFile','$imageFile2','$imageFile3',date_add(now(),INTERVAL 2 HOUR),
'$post_by', '$invisipost')";
$result2=mysql_query($sql2);
if($result2){
header("Location: view_topic.php?topic_id=$id");
Before anyone comments I am getting the whole site working first then
converting to MySQLi, Thanks for any advice.
This is my "add_topic.php" file, which successfully adds the images on
topics, just posts isn't working..
<?php
include 'core/init.php';
include 'includes/overall/header.php';
$tags = isset($_POST['tags']) ? $_POST['tags'] : null;
$topic_data = htmlentities($_POST['topic_data']);
if ($topic_data != "") {
if (mysql_num_rows(mysql_query("SELECT * FROM `topics` WHERE
`topic_data`='{$topic_data}'")) == 0) {
if (is_array($tags)) {
$image_attach = null;
//Get Timestamp to add in image name.
$date = new DateTime();
$timestamp = $date->getTimestamp();
//Check upload folder is created
if (!file_exists('uploads')) {
mkdir('uploads', 0777, true);
}
//Check user directory is created or not.
$user = $user_data['username'];
$uid = $_SESSION['user_id'];
if (!file_exists('uploads/images/' . $user)) {
mkdir('uploads/images/' . $user, 0777, true);
}
//Below Code will copy all three images to the Upload directory
and add path to the three variables.
$imageFile = null;
$imageFile2 = null;
$imagefile3 = null;
if (isset($_FILES['image_attach']) &&
$_FILES['image_attach']['size'] > 0) {
$info = pathinfo($_FILES['image_attach']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $uid. $timestamp ."1." . $ext;
$target = 'uploads/images/' . $user . "/" . $newname;
move_uploaded_file($_FILES['image_attach']['tmp_name'], $target);
$imageFile = $target;
}
if (isset($_FILES['image_attach2']) &&
$_FILES['image_attach2']['size'] > 0) {
$info = pathinfo($_FILES['image_attach2']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $uid. $timestamp ."2." . $ext;
$target = 'uploads/images/' . $user . "/" . $newname;
move_uploaded_file($_FILES['image_attach2']['tmp_name'],
$target);
$imageFile2 = $target;
}
if (isset($_FILES['image_attach3']) &&
$_FILES['image_attach3']['size'] > 0) {
$info = pathinfo($_FILES['image_attach3']['name']);
$ext = $info['extension']; // get the extension of the file
$newname = $uid. $timestamp ."3." . $ext;
$target = 'uploads/images/' . $user . "/" . $newname;
move_uploaded_file($_FILES['image_attach3']['tmp_name'],
$target);
$imageFile3 = $target;
}
//insert the topic first
$topic_data = htmlentities($_POST['topic_data']);
$posted_by = $_SESSION['user_id'];
$invisipost = isset($_POST['invisipost']) ? $_POST['invisipost'] : 0;
mysql_query("INSERT INTO topics(topic_data, image_attach,
image_attach2, image_attach3, posted_by, posted, view, reply,
invisipost)VALUES('{$topic_data}', '$imageFile', '$imageFile2',
'$imageFile3', '{$posted_by}', date_add(now(),INTERVAL 2 HOUR),
'0', '0', '{$invisipost}')");
$inserted_topic_id = mysql_insert_id();
foreach ($tags as $t) {
if ($t != '') {
if (mysql_num_rows(mysql_query("SELECT * FROM `tags` WHERE
`tags`='{$t}'")) == 0) {
$sql = "INSERT INTO tags (tags) VALUES('$t')";
mysql_query($sql);
$inserted_tag_id = mysql_insert_id();
$sqla = "INSERT INTO topic_tags (topic_id, tag_id)
VALUES('$inserted_topic_id', '$inserted_tag_id')";
mysql_query($sqla);
} else {
$queryData = mysql_fetch_array(mysql_query("SELECT *
FROM tags WHERE tags='$t'"));
$tag_id = $queryData['tag_id'];
$sqlb = "INSERT INTO topic_tags (topic_id, tag_id)
VALUES('$inserted_topic_id', '$tag_id')";
mysql_query($sqlb);
}
}
}
header("Location: topics.php?tags={$t}");
} else {
echo 'Invalid tag';
}
} else {
echo "<h2>Opps...</h2><p>Topic already existing.</p>";
}
} else {
echo "<h2>Opps...</h2><p>You did not fill out all the required
fields.</p>";
}
mysql_close();
include 'includes/overall/footer.php';
?>

No comments:

Post a Comment