Skip to content

Commit

Permalink
update: upload fix + delete post
Browse files Browse the repository at this point in the history
  • Loading branch information
didof committed Jan 17, 2020
1 parent 76e400e commit 6e859e5
Show file tree
Hide file tree
Showing 7 changed files with 223 additions and 65 deletions.
49 changes: 49 additions & 0 deletions includes/deletePost.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php
include_once "dbh.inc.php";

# Aggiungi goaway
# vedi se c'è l'immagine, evenutalmente devi cancellarla
# cancella dal database il post richesto

$post = $_GET["idPost"];

$sql = "SELECT * FROM post WHERE id=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "i", $post);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$image = mysqli_fetch_assoc($result);
$dir = $image["post_dir"];
$author = $image["post_author"];
$fileName = $image["post_file_name"];
echo $fileName;

if ($fileName == "") {
# Cancel from database
$sql = "DELETE FROM post WHERE id=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "i", $post);
mysqli_stmt_execute($stmt);
echo 'Cancellata solo dal database<br>';
header("Location: ../index.php?success=deletePost");
exit();
} else {
$path = '../uploads/post/' . $dir . '/' . $author . '/' . $fileName;
echo $path;
if (!unlink($path)) {
echo '>Error: file was not deleted.';
} else {
# Cancel from database too
$sql = "DELETE FROM post WHERE id=?";
$stmt = mysqli_stmt_init($conn);
mysqli_stmt_prepare($stmt, $sql);
mysqli_stmt_bind_param($stmt, "i", $post);
mysqli_stmt_execute($stmt);
mysqli_stmt_close($stmt);
echo 'Cancellata sia dal database che dalla cartella<br>';
header("Location: ../index.php?success=deletePost");
exit();
}
}
36 changes: 36 additions & 0 deletions includes/showPost.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php
include_once "dbh.inc.php";

$userId = $_SESSION["userId"];

$sql = "SELECT * FROM post ORDER BY post_date DESC";
$query = mysqli_query($conn, $sql);
if (mysqli_num_rows($query) > 0) {
while ($row = mysqli_fetch_assoc($query)) {
echo '<div class="post">
<div class="header">
<div>' . $row["post_title"] . '</div>
<div>' . $row["post_author"] . ' (' . $row["post_user_id"] . ')</div>';
if ($userId == $row["post_user_id"]) {
echo '<div><button>Modify</button></div>
<div><a href="includes/deletePost.inc.php?idPost=' . $row["id"] . '"><button>Cancel</button></a></div>';
} else {
echo '<div></div>
<div></div>';
}
echo '</div>'; // close class header
echo '<div class="content">';
if ($row["post_file_name"] == "") {
} else {
$imageAuthor = $row["post_author"];
$imageDir = $row["post_dir"];
$imageName = $row["post_file_name"];
$imagePath = "uploads/post/" . $imageDir . "/" . $imageAuthor . "/" . $imageName;
echo '<img src="' . $imagePath . '" width="100px" height="100px">';
}
echo $row["post_content"] . '<br><br>';
echo '<i>Posted on ' . $row["post_date"] . ' in /' . $row["post_dir"];
} // close if not set picture
echo '</div>'; // close class content
echo '</div>'; // close this post
} // close while
105 changes: 65 additions & 40 deletions includes/uploadPost.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,53 +22,78 @@
$fileType = $postFile["type"];
$fileTmp = $postFile["tmp_name"];

# Overwrite the name of the file with the title of the post and add the extention
$fileExploded = explode('.', $fileName);
$fileExtention = strtolower(end($fileExploded));
$fileName = $postTitle . "_" . uniqid('', true) . "." . $fileExtention;
echo '$fileName => ' . $fileName . "<br>";
echo '$_FILES => ';
print_r($_FILES);
echo '<br>';

$sql = "INSERT INTO post (post_user_id, post_title, post_content, post_author, post_file_name, post_dir)
VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo '>Error: statement and request not associated.<br><br>';
} else if (!mysqli_stmt_bind_param($stmt, "isssss", $postId, $postTitle, $postContent, $postName, $fileName, $postDir)) {
echo '>Error: data not binded.<br><br>';
} else if (!mysqli_stmt_execute($stmt)) {
echo '>Error: query not executed.<br><br>';
if ($_FILES["post_file"]["size"] == 0) { // check if there is or not the image
echo '$_FILES is null';
$sql = "INSERT INTO post (post_user_id, post_title, post_content, post_author, post_dir)
VALUES (?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo '>Error: statement and request not associated.<br><br>';
} else if (!mysqli_stmt_bind_param($stmt, "issss", $postId, $postTitle, $postContent, $postName, $postDir)) {
echo '>Error: data not binded.<br><br>';
} else if (!mysqli_stmt_execute($stmt)) {
echo '>Error: query not executed.<br><br>';
} else {
echo '>Success: data inserted in db.<br><br>';
mysqli_stmt_close($stmt);
header("Location: ../index.php?success=uploadPost");
exit();
} // success inserting data in db (without picture)
} else {
echo '>Success: data inserted in db.<br><br>';
echo '$_FILES is not null';

# Send in tipology folder, possibly make it first
$path = "../uploads/post/" . $postDir . "/";
echo $path;
echo '<br><br>';
# Overwrite the name of the file with the title of the post and add the extention
$fileExploded = explode('.', $fileName);
$fileExtention = strtolower(end($fileExploded));
$fileName = $postTitle . "_" . uniqid('', true) . "." . $fileExtention;
echo '<br>$fileName => ' . $fileName . "<br>";

if (!file_exists($path)) {
mkdir("../uploads/post/" . $postDir, 0777);
echo "The directory {$postDir} was successfully created.<br><br>";
$sql = "INSERT INTO post (post_user_id, post_title, post_content, post_author, post_file_name, post_dir)
VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo '>Error: statement and request not associated.<br><br>';
} else if (!mysqli_stmt_bind_param($stmt, "isssss", $postId, $postTitle, $postContent, $postName, $fileName, $postDir)) {
echo '>Error: data not binded.<br><br>';
} else if (!mysqli_stmt_execute($stmt)) {
echo '>Error: query not executed.<br><br>';
} else {
echo "The directory {$postDir} exists.<br><br>";
}
echo '>Success: data inserted in db.<br><br>';

// Set a personal folder for this user
$userPath = $path . $postName . "/";
echo $userPath . '<br>';
# Send in tipology folder, possibly make it first
$path = "../uploads/post/" . $postDir . "/";
echo $path;
echo '<br><br>';

if (!file_exists($userPath)) {
mkdir($userPath, 0777);
echo "The directory {$userPath} was successfully created.<br><br>";
} else {
echo "The directory {$userPath} exists.<br><br>";
}
if (!file_exists($path)) {
mkdir("../uploads/post/" . $postDir, 0777);
echo "The directory {$postDir} was successfully created.<br><br>";
} else {
echo "The directory {$postDir} exists.<br><br>";
}

// Set a personal folder for this user
$userPath = $path . $postName . "/";
echo $userPath . '<br>';

if (!file_exists($userPath)) {
mkdir($userPath, 0777);
echo "The directory {$userPath} was successfully created.<br><br>";
} else {
echo "The directory {$userPath} exists.<br><br>";
}

#Thus, insert into folder the pic
$fileDestination = $userPath . $fileName;
move_uploaded_file($fileTmp, $fileDestination);
#Thus, insert into folder the pic
$fileDestination = $userPath . $fileName;
move_uploaded_file($fileTmp, $fileDestination);

mysqli_stmt_close($stmt);
header("Location: ../index.php?success=uploadPost");
exit();
} // success inserting data in db
mysqli_stmt_close($stmt);
header("Location: ../index.php?success=uploadPost");
exit();
} // success inserting data in db (with picture)
} // close if file exist or not
} // close submit-upload
45 changes: 24 additions & 21 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,30 +22,30 @@
$sql = "SELECT * FROM profileimg WHERE userId=?";
$stmt = mysqli_stmt_init($conn);
if (!mysqli_stmt_prepare($stmt, $sql)) {
echo '<img src="uploads/error_profile_img.png" height="100px" width="100px">';
echo '<div class="profile-pic"><img src="uploads/error_profile_img.png" height="100px" width="100px"></div>';
} else {
mysqli_stmt_bind_param($stmt, "i", $_SESSION["userId"]);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
$row = mysqli_fetch_assoc($result);
if (!($row > 0)) {
# there is not the row associated with this user
echo '<img src="uploads/error_profile_img.png" height="100px" width="100px">';
echo '<div class="profile-pic"><img src="uploads/error_profile_img.png" height="100px" width="100px"></div>';
} else {
# there istus the row associated with this user
if ($row["status"] == 0) {
# thus, the user didn't uploaded yet
echo '<img src="uploads/default_profile_img.jpg" height="100px" width="100px">';
echo '<div class="profile-pic"><img src="uploads/default_profile_img.jpg" height="100px" width="100px"></div>';
} else {
# this, the user already uploaded the pic
echo "<img src='uploads/" . $row["name"] . "?" . mt_rand() . "' height='150px' width='200px'>";
echo "<div class='profile-pic'><img src='uploads/" . $row["name"] . "?" . mt_rand() . "' height='150px' width='200px'></div>";
// some browser can remember the picture and this force the user to refresh the page
# FIX -> add mt_rand()
// <img src='uploads/profile_image_user_1.jpg?218728917'>
}
}
}
echo $_SESSION["userName"];
echo 'Username: ' . $_SESSION["userName"];
?>
<!-- <img src="uploads/default_profile_img.jpg" height="100px" width="100px"> -->

Expand All @@ -60,12 +60,12 @@
<li><a href="changePwd.php">Change password</a></li>
<li><a href="changePic.php">Set/unset profile picture</a></li>
<li><a href="deleteUser.php">Delete account</a></li>
<li><a href="index.php?order=uploadPost">Upload picture</a></li>
</ul>
</div>
<div class="workingOn">
<h3>Working on:</h3>
<ul>
<li><a href="uploadPic.php">Upload picture</a></li>
<li><a href="changeMail.php">Change e-mail</a></li>
<li><a href="changeUsername.php">Change username</a></li>
</ul>
Expand Down Expand Up @@ -118,29 +118,32 @@
<td><select name="post_dir">
<option value="myself">my self/</option>
<option value="anonymous">anonymous/</option>
<option value="opinion">opinion</option>
<option value="opinion">opinion/</option>
<option value="ads">ads/</option>
<!-- is beein sold? -->
<option value="recipes">recipes</option>
<option value="recipes">recipes/</option>
</select></td>
</tr>

<!-- if there is photo, show it in gallery, with associated this post -->

</table>
</form>
<?php
endif;
?>
</div>
<?php endif; ?>
</div>
</div>
<?php
<div class="showPost">
<?php require "includes/showPost.inc.php" ?>
</div>
<!-- if there is photo, show it in gallery, with associated this post -->

<?php
} else {
?>
<!-- Not logged in -->
Welcome to my social network.
<a href="signup.php">Signup</a>
<?php
?>
<!-- Not logged in -->
Welcome to my social network.
<a href="signup.php">Signup</a>
<?php
}
?>
?>
</main>

<?php
Expand Down
53 changes: 49 additions & 4 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ input {
}

button {
background-color: #a583c9;
background-color: #c299ee;
border: none;
border-radius: 50px;
color: white;
Expand All @@ -33,15 +33,19 @@ button {
cursor: pointer;
}

button:disabled {
background-color: #46424b;
}

.grid {
display: grid;
grid-template-columns: 1fr 2fr 1fr;
/* grid-template-rows: 30px 1fr 1fr 1fr; */
grid-template-areas:
"empty searchbar empty2"
"canDo profilePic resultUser"
"workingOn photos resultUser"
"wantTo photos resultUser";
"workingOn uploadPost resultUser"
"wantTo showPost resultUser";
background-color: #e2d3d3;
padding: 10px;
margin: 3px 8px;
Expand Down Expand Up @@ -119,7 +123,7 @@ button {
}

.uploadPost {
grid-area: photos;
grid-area: uploadPost;
/* grid-column: 2 / 3;
grid-row: 5 / 6; */
}
Expand All @@ -136,3 +140,44 @@ button {
padding: 3px 8px;
margin: 5px;
}

.showPost {
grid-area: showPost;
}

.post {
background-color: blanchedalmond;
border: none;
border-radius: 15px;
margin: 3px 8px;
padding: 5px;
font-family: "Lucida Sans", "Lucida Sans Regular";
}
.post img {
vertical-align: top;
border-radius: 10px;
margin: 3px;
}

.post .header {
display: grid;
grid-template-columns: 3fr 3fr 1fr 1fr;
background-color: #46424b;
font-size: 20px;
font-family: Arial;
color: white;
padding: 3px 5px;
border-radius: 5px;
height: 40px;
}

.profile-pic {
background-color: #46424b;
border-radius: 15px;
margin: 10px;
}

.profile-pic img {
border-radius: 15px;
margin: 3px;
}
File renamed without changes
Binary file added uploads/profile_image_user_81.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 6e859e5

Please sign in to comment.