Problem to make upload images in blogpost.php
Posted: Tue Jul 26, 2016 7:46 pm
I have a question, it is so that I have made an edit function to my blog where you can upload a picture and then see it in blogread.php.
Everything works so long, but now I want to be able to upload photos directly when I create a post. So I can see the photos directly on bloglist.php.
I have used the same thing as I did when I created the function of editpost.php.
It works to upload the image to the folder, but it receives no ID, and it is broken when you open it. I'm close, in that I can upload the image, but that said it is broken and has no ID.
I do not know if I make a mistake I'll add the path to the image in my array into get_posts () functionen?
Please can someone help me with what I should do to be able to upload pictures from blogpost.php.
Here is the blogpost.php
[syntax=php]<?php
require("core/init.inc.php");
if(isset($_POST['user'], $_POST['title'], $_POST['body']))
{
$errors = array();
if(empty($_POST['user']))
{
$errors[] = "<span class='errors'>Du har glömt att fylla i ditt namn.</span>";
}
if(empty($_POST['title']))
{
$errors[] = "<span class='errors'>Du har glömt fylla i titeln för denna produkt.</span>";
}
if(empty($_POST['body']))
{
$errors[] = "<span class='errors'>Du har glömt fylla i produkt informationen i textrutan.</span>";
}
if(empty($_FILES['avatar']['tmp_name']) === false)
{
$file_ext = end(explode('.', $_FILES['avatar']['name']));
if(in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false)
{
$errors[] = "Din avatar måste vara en bild.";
}
}
if(empty($errors))
{
add_post($_POST['user'], $_POST['title'], $_POST['body'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']);
header("Location: http://rege.se/rege/func/productblog/bloglist.php");
die();
}
}
?>
[/syntax]
[syntax=xhtml]
<form class="productForm" action="" method="post" enctype="multipart/form-data">
<p>
<label class="user" for="user">Namn</label>
<input type="text" class="user" name="user" value="<?php if(isset($_POST['user'])) echo htmlentities($_POST['user']); ?>" />
</p>
<p>
<label class="title" for="title">Titel</label>
<input type="text" class="title" name="title" value="<?php if(isset($_POST['title'])) echo htmlentities($_POST['title']); ?>" />
</p>
<p>
<textarea class="body" name="body" rows="20" cols="60"><?php if(isset($_POST['body'])) echo $_POST['body']; ?></textarea>
</p>
<p>
<label class="avatar" for="avatar">Product Pic</label>
<input type="file" class="avatar" name="avatar" />
</p>
<p>
<input type="submit" class="submit" name="submit" value="Skapa">
</p>
</form> <!-- END PRODUCT FORM -->
[/syntax]
Here is the add_post function
[syntax=php]
function add_post($name, $title, $body, $avatar)
{
$name = mysql_real_escape_string(htmlentities($name));
$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));
if(file_exists($avatar))
{
$src_size = getimagesize($avatar);
if($src_size['mime'] === 'image/jpeg')
{
$src_img = imagecreatefromjpeg($avatar);
}
elseif($src_size['mime'] === 'image/png')
{
$src_img = imagecreatefrompng($avatar);
}
elseif($src_size['mime'] === 'image/gif')
{
$src_img = imagecreatefromgif($avatar);
}
else
{
$src_img = false;
}
if($src_img !== false)
{
$thumb_width = 200;
if($src_size[0] <= $thumb_width)
{
$thumb = $src_img;
}
else
{
$new_size[0] = $thumb_width;
$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
}
imagejpeg($thumb, "{$GLOBALS['path']}/avatars/{$_GET['pid']}.jpg");
}
}
mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES('{$name}', '{$title}', '{$body}', NOW())");
}
[/syntax]
And get_posts function
[syntax=php]
function get_posts()
{
$sql = "SELECT
`posts`.`post_id` AS `id`,
`posts`.`post_title` AS `title`,
LEFT(`posts`.`post_body`, 512) AS `preview`,
`posts`.`post_user` AS `user`,
DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
`comments`.`total_comments`,
DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
FROM `posts`
LEFT JOIN(
SELECT
`post_id`,
COUNT(`comment_id`) AS `total_comments`,
MAX(`comment_date`) AS `last_comment`
FROM `comments`
GROUP BY `post_id`
) AS `comments`
ON `posts`.`post_id` = `comments`.`post_id`
ORDER BY `posts`.`post_date` DESC";
$posts = mysql_query($sql);
$rows = array();
while(($row = mysql_fetch_assoc($posts)) !== false)
{
$rows[] = array(
'id' => $row['id'],
'title' => $row['title'],
'preview' => $row['preview'],
'user' => $row['user'],
'date' => $row['date'],
'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
'avatar' => ($row['avatar'] = (file_exists("{$GLOBALS['path']}/avatars/{$row['id']}.jpg")) ? "core/avatars/{$row['id']}.jpg" : "core/avatars/default.jpg"),
'last_comment' => ($row['last_comment'] === null) ? 'Aldrig' : $row['last_comment']
);
}
return $rows;
}
[/syntax]
Thanks
Everything works so long, but now I want to be able to upload photos directly when I create a post. So I can see the photos directly on bloglist.php.
I have used the same thing as I did when I created the function of editpost.php.
It works to upload the image to the folder, but it receives no ID, and it is broken when you open it. I'm close, in that I can upload the image, but that said it is broken and has no ID.
I do not know if I make a mistake I'll add the path to the image in my array into get_posts () functionen?
Please can someone help me with what I should do to be able to upload pictures from blogpost.php.
Here is the blogpost.php
[syntax=php]<?php
require("core/init.inc.php");
if(isset($_POST['user'], $_POST['title'], $_POST['body']))
{
$errors = array();
if(empty($_POST['user']))
{
$errors[] = "<span class='errors'>Du har glömt att fylla i ditt namn.</span>";
}
if(empty($_POST['title']))
{
$errors[] = "<span class='errors'>Du har glömt fylla i titeln för denna produkt.</span>";
}
if(empty($_POST['body']))
{
$errors[] = "<span class='errors'>Du har glömt fylla i produkt informationen i textrutan.</span>";
}
if(empty($_FILES['avatar']['tmp_name']) === false)
{
$file_ext = end(explode('.', $_FILES['avatar']['name']));
if(in_array(strtolower($file_ext), array('jpg', 'jpeg', 'png', 'gif')) === false)
{
$errors[] = "Din avatar måste vara en bild.";
}
}
if(empty($errors))
{
add_post($_POST['user'], $_POST['title'], $_POST['body'], (empty($_FILES['avatar']['tmp_name'])) ? false : $_FILES['avatar']['tmp_name']);
header("Location: http://rege.se/rege/func/productblog/bloglist.php");
die();
}
}
?>
[/syntax]
[syntax=xhtml]
<form class="productForm" action="" method="post" enctype="multipart/form-data">
<p>
<label class="user" for="user">Namn</label>
<input type="text" class="user" name="user" value="<?php if(isset($_POST['user'])) echo htmlentities($_POST['user']); ?>" />
</p>
<p>
<label class="title" for="title">Titel</label>
<input type="text" class="title" name="title" value="<?php if(isset($_POST['title'])) echo htmlentities($_POST['title']); ?>" />
</p>
<p>
<textarea class="body" name="body" rows="20" cols="60"><?php if(isset($_POST['body'])) echo $_POST['body']; ?></textarea>
</p>
<p>
<label class="avatar" for="avatar">Product Pic</label>
<input type="file" class="avatar" name="avatar" />
</p>
<p>
<input type="submit" class="submit" name="submit" value="Skapa">
</p>
</form> <!-- END PRODUCT FORM -->
[/syntax]
Here is the add_post function
[syntax=php]
function add_post($name, $title, $body, $avatar)
{
$name = mysql_real_escape_string(htmlentities($name));
$title = mysql_real_escape_string(htmlentities($title));
$body = mysql_real_escape_string(nl2br(htmlentities($body)));
if(file_exists($avatar))
{
$src_size = getimagesize($avatar);
if($src_size['mime'] === 'image/jpeg')
{
$src_img = imagecreatefromjpeg($avatar);
}
elseif($src_size['mime'] === 'image/png')
{
$src_img = imagecreatefrompng($avatar);
}
elseif($src_size['mime'] === 'image/gif')
{
$src_img = imagecreatefromgif($avatar);
}
else
{
$src_img = false;
}
if($src_img !== false)
{
$thumb_width = 200;
if($src_size[0] <= $thumb_width)
{
$thumb = $src_img;
}
else
{
$new_size[0] = $thumb_width;
$new_size[1] = ($src_size[1] / $src_size[0]) * $thumb_width;
$thumb = imagecreatetruecolor($new_size[0], $new_size[1]);
imagecopyresampled($thumb, $src_img, 0, 0, 0, 0, $new_size[0], $new_size[1], $src_size[0], $src_size[1]);
}
imagejpeg($thumb, "{$GLOBALS['path']}/avatars/{$_GET['pid']}.jpg");
}
}
mysql_query("INSERT INTO `posts` (`post_user`, `post_title`, `post_body`, `post_date`) VALUES('{$name}', '{$title}', '{$body}', NOW())");
}
[/syntax]
And get_posts function
[syntax=php]
function get_posts()
{
$sql = "SELECT
`posts`.`post_id` AS `id`,
`posts`.`post_title` AS `title`,
LEFT(`posts`.`post_body`, 512) AS `preview`,
`posts`.`post_user` AS `user`,
DATE_FORMAT(`posts`.`post_date`, '%d/%m/%Y %H:%i:%s') AS `date`,
`comments`.`total_comments`,
DATE_FORMAT(`comments`.`last_comment`, '%d/%m/%Y %H:%i:%s') AS `last_comment`
FROM `posts`
LEFT JOIN(
SELECT
`post_id`,
COUNT(`comment_id`) AS `total_comments`,
MAX(`comment_date`) AS `last_comment`
FROM `comments`
GROUP BY `post_id`
) AS `comments`
ON `posts`.`post_id` = `comments`.`post_id`
ORDER BY `posts`.`post_date` DESC";
$posts = mysql_query($sql);
$rows = array();
while(($row = mysql_fetch_assoc($posts)) !== false)
{
$rows[] = array(
'id' => $row['id'],
'title' => $row['title'],
'preview' => $row['preview'],
'user' => $row['user'],
'date' => $row['date'],
'total_comments' => ($row['total_comments'] === null) ? 0 : $row['total_comments'],
'avatar' => ($row['avatar'] = (file_exists("{$GLOBALS['path']}/avatars/{$row['id']}.jpg")) ? "core/avatars/{$row['id']}.jpg" : "core/avatars/default.jpg"),
'last_comment' => ($row['last_comment'] === null) ? 'Aldrig' : $row['last_comment']
);
}
return $rows;
}
[/syntax]
Thanks