Skip to content

Commit

Permalink
upload: uploadPost - work in progress
Browse files Browse the repository at this point in the history
  • Loading branch information
didof committed Jan 17, 2020
1 parent 32fa2fc commit 76e400e
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 11 deletions.
1 change: 0 additions & 1 deletion footer.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
</head>

<body>
<h3>Postagma says:</h3>
<?php require "displayError.php" ?>

</body>
Expand Down
2 changes: 1 addition & 1 deletion header.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
</ul>
<?php } else if (!isset($_SESSION["userId"])) { ?>
<form action="includes/login.inc.php" method="post">
<input type="test" name="mail" placeholder="e-mail">
<input type="test" name="mail" placeholder="e-mail or username">
<input type="password" name="password" placeholder="password">
<button type="submit" name="login-submit">Login</button>
<input type="checkbox" name="remember" value=0>Remember me
Expand Down
1 change: 0 additions & 1 deletion includes/changePic.inc.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,6 @@
$fileNewName = "profile_image_user_" . $userId . '.' . $fileExtention;
echo '$fileNewName => ' . $fileNewName . '<br><br>';


# Update in the db
include_once "dbh.inc.php";
$newStatus = 1;
Expand Down
File renamed without changes
74 changes: 74 additions & 0 deletions includes/uploadPost.inc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php
include_once "dbh.inc.php";
session_start();

# Cancel button
if (isset($_POST["cancel-uploadPost"])) {
header("Location: ../index.php");
exit();
} else if (isset($_POST["draft-uploadPost"])) {
// create draft code
echo 'fai bozza';
} else if (isset($_POST["submit-uploadPost"])) {
echo '<b>Upload code</b><br><br>';
$postId = $_SESSION["userId"];
$postTitle = $_POST["post_title"];
$postContent = $_POST["post_content"];
$postName = $_SESSION["userName"];
$postDir = $_POST["post_dir"];

$postFile = $_FILES["post_file"];
$fileName = $postFile["name"];
$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>";

$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 '>Success: data inserted in db.<br><br>';

# Send in tipology folder, possibly make it first
$path = "../uploads/post/" . $postDir . "/";
echo $path;
echo '<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);

mysqli_stmt_close($stmt);
header("Location: ../index.php?success=uploadPost");
exit();
} // success inserting data in db
} // close submit-upload
52 changes: 46 additions & 6 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
# Logged in
?>
<div class="grid">
<div class="empty">empty</div>
<div class="empty2">empty2</div>
<div class="empty"></div>
<div class="empty2">User in db:</div>
<div class="searchBar">
<form action="index.php" method="post">
<input type="text" name="searchInput" placeholder="Look for an username/e-mail">
Expand Down Expand Up @@ -45,7 +45,7 @@
}
}
}

echo $_SESSION["userName"];
?>
<!-- <img src="uploads/default_profile_img.jpg" height="100px" width="100px"> -->

Expand Down Expand Up @@ -85,12 +85,52 @@
<li>choose website theme</li>
</ul>
</li>

</ul>
</div>
<div class="photos">
Show photos.
<div class="uploadPost">
<?php if (!isset($_GET["order"])) : ?>
<form action="index.php?order=uploadPost" method="post">
<table class="table-post">
<tr>
<td><input text="text" disabled></td>
<td><button type="submit">Post</button></td>
</tr>

</table>
</form>
<?php else : ?>
<form action="includes/uploadPost.inc.php" method="post" enctype="multipart/form-data">
<table class="table-post">
<tr>
<td><input text="text" name="post_title" placeholder="title"></td>
<td><button type="submit" name="cancel-uploadPost">Cancel</button></td>
</tr>
<tr>
<td><input type="text" name="post_content" placeholder="content"></td>
<td><button type="submit" name="draft-uploadPost">Draft</button></td>
</tr>
<tr>
<td><input type="file" name="post_file" value="add photo(s)"></td>
<td><button type="submit" name="submit-uploadPost">Post</button></td>
</tr>
<tr>
<td>Repository: </td>
<td><select name="post_dir">
<option value="myself">my self/</option>
<option value="anonymous">anonymous/</option>
<option value="opinion">opinion</option>
<option value="ads">ads/</option>
<!-- is beein sold? -->
<option value="recipes">recipes</option>
</select></td>
</tr>

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

</form>
</div>
<?php endif; ?>
</div>
</div>
<?php
} else {
Expand Down
16 changes: 14 additions & 2 deletions style.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ button {
"wantTo photos resultUser";
background-color: #e2d3d3;
padding: 10px;
margin: 3px 8px;
}

.empty {
Expand Down Expand Up @@ -77,10 +78,14 @@ button {
grid-row: 1 / auto; */
justify-content: start;
align-items: flex-start;
background-color: #eeeeee;
border-radius: 50px;
border-bottom: 5px solid black;
margin: 3px 8px;
}

.resultUsers div:nth-child(odd) {
background-color: #bbbbbb;
background-color: #ffffff;
}

.canDo {
Expand Down Expand Up @@ -113,7 +118,7 @@ button {
background-color: #fedce6;
}

.photos {
.uploadPost {
grid-area: photos;
/* grid-column: 2 / 3;
grid-row: 5 / 6; */
Expand All @@ -124,3 +129,10 @@ button {
justify-content: space-between;
align-items: center;
}

.table-post {
width: 100%;
border: collapse;
padding: 3px 8px;
margin: 5px;
}

0 comments on commit 76e400e

Please sign in to comment.