I'm working on CMS that allows the user to insert articles and, if he wants he can upload an image for the correspondent article, the articles text works fine the problem is that the images aren't beeing displayed and even if i don't upload any image the word "array" appears in the place where the image is supose to be, i fallowed the same logic as the articles texts input. The images are beeing stored in mysql with the type 'blob'. Any tips? thanks
PHP:
<?php
session_start();
include_once('../includes/connection.php');
if(isset($_SESSION['logged_in'])) {
// display add page
if (isset($_POST['title'], $_POST['content'])) {
$title = $_POST['title'];
$content = $_POST['content'];
$image = $_FILES['image']['name'];
if (empty($title) || empty($content)) {
$error = 'Todos os campos têm de ser preenchidos!';
}
else {
$query = $pdo->prepare('INSERT INTO articles(article_title, article_content, article_img, article_timestamp) VAlUES (?, ?, ?, ?)');
$query->bindValue(1, $title);
$query->bindValue(2, $content);
$query->bindValue(3, $image);
$query->bindValue(4, time());
$query->execute();
header('Location:index.php');
}
}
}
else {
header('Location:index.php');
}
?>
<html>
<head>
<title>AdminPT</title>
<meta charset="UTF-8">
<link rel ="stylesheet" href="../assets/style.css"/>
</head>
<body>
<div class="container">
CMS
<br>
<h4 id ="add">Adicionar Artigo</h4>
<?php
if (isset($error)) { ?>
<small style="color:#aa0000"><?php echo $error; ?></small>
<?php } ?>
<form action="add.php" method="post" autocomplete="off" enctype="multipart/form-data">
<input id="title" type="text" name="title" placeholder="Título"/><br><br>
<textarea id="content" rows="15" placeholder="Conteúdo" name="content"></textarea>
<input type="file" name="image"/><br><br>
<input id="addBtn" type="submit" value="Adicionar Artigo"/>
</form>
<a id="back" href="../admin">← Voltar</a>
</div>
</body>
</html>
display content (HTML):
<div id="news">
<?php foreach ($articles as $article) { ?>
<div><h2><?php echo utf8_encode($article['article_title']); ?></h2><div id="newsImg"><?php echo $article['article_img']; ?></div><br><span id="date">Publicado
<?php
setlocale(LC_ALL, 'pt_BR.utf8', 'Portuguese_Brazil');
//setlocale(LC_ALL, NULL);
date_default_timezone_set('Europe/Lisbon');
$uppercaseMonth = ucfirst(gmstrftime('%B'));
echo utf8_encode(strftime( '%a, '.$uppercaseMonth.' %d de %Y'/* - %H:%M'*/, $article['article_timestamp']));
?></span><p><?php echo utf8_encode($article['article_content']); ?><br><br><span id="print"><a onclick="javascript:window.print();" href="#">Imprimir</a></span><span id="link"><a href="#">Enviar link</a></p></div>
<?php } ?>
</div>
Please change this line ( $image = $_FILES['image']; ) to $image = $_FILES['image']["name"];
in addition to $image = $_FILES['image']["name"]; you need to add the rest of the relative / absolute path before it, e.g
$image = "/uploadedimages/" . $_FILES['image']['name'];
a) When you submit form then image is stored in temp space of server, so first you need to move that image to your project directory, you can also change name of image when you move it to directory
b) Once image is moved successfully, then store name of image in database
c) when you need to show image then make complete http url of image by adding baseurl like (http://localhost/sample-project) and image directory (uploads) and image name (image1.jpg which is saved in database) so it becomes http://localhost/sample-project/uploads/image1.jpg