added link to img upload to new_twt

This commit is contained in:
sørenpeter 2025-02-13 19:14:37 +01:00
parent e2122741fa
commit d77d088687
3 changed files with 124 additions and 76 deletions

View file

@ -106,6 +106,7 @@ if (isset($_POST['submit'])) {
<article id="new_twt">
<form method="POST">
<div id="posting">
<small class="right" style="margin-top: 0.25rem ;"><a href="./upload" target="_blank">Upload images</a></small>
<div id="toolbar"></div>
<textarea class="textinput" id="new_post" name="new_post"
rows="4" cols="100" autofocus required onfocus="var val=this.value; this.value=''; this.value= val;"

View file

@ -8,81 +8,119 @@ $title = "Upload - $title";
include_once 'partials/header.php';
$media_upload = getcwd() . "/" . $config["media_upload"] . "/";
?>
<article>
<form action="" method="post" enctype="multipart/form-data">
<label>Select image to upload</label>
<input type="file" name="fileToUpload" id="fileToUpload"><br>
<input type="submit" value="Upload Image" name="submit">
<article id="new_twt">
<form method="POST" enctype="multipart/form-data">
<strong>Select images to upload:</strong>
<input type="file" name="files[]" multiple style="width: 100%;border: 1px solid var(--border); margin: 0.5rem 0;"><br>
<input type="submit" name="submit" value="Upload">
</form>
</article>
<?php
$media_upload = getcwd() . "/" . $config["media_upload"] . "/";
// Credits: https://www.geeksforgeeks.org/how-to-select-and-upload-multiple-files-with-html-and-php-using-http-post/
if (!empty($_POST)) {
// Based on code from: https://www.w3schools.com/php/php_file_upload.asp
//echo getcwd() ."<br>";
//echo __DIR__ . "<br>";
//echo "upload path: " . $config["media_upload"];
$target_file = $media_upload . basename($_FILES["fileToUpload"]["name"]);
$target_file = str_replace(' ', '-', strtolower($target_file)); // Replace spaces with dashes and set all lower case
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
// Check if form was submitted
if(isset($_POST['submit'])) {
echo "<p class='notice'>";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
//echo "File is an image - " . $check["mime"] . ".<br>";
$uploadOk = 1;
} else {
echo "File is not an image.<br>";
$uploadOk = 0;
// Configure upload directory and allowed file types
$upload_dir = $media_upload.DIRECTORY_SEPARATOR;
$allowed_types = array('jpg', 'png', 'jpeg', 'gif');
// Define maxsize for files i.e 2MB
$maxsize = 10 * 1024 * 1024;
// Checks if user sent an empty form
if(!empty(array_filter($_FILES['files']['name']))) {
// Loop through each file in files[] array
foreach ($_FILES['files']['tmp_name'] as $key => $value) {
$file_tmpname = $_FILES['files']['tmp_name'][$key];
$file_name = $_FILES['files']['name'][$key];
$file_size = $_FILES['files']['size'][$key];
$file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
// Apped date to filename
//$file_name = date('Y-m-d_').$file_name;
// Replace spaces with dashes and set all lower case
$file_name = str_replace(' ', '-', strtolower($file_name));
// Set upload file path
$file_path = $upload_dir.$file_name;
// Check file type is allowed or not
if(in_array(strtolower($file_ext), $allowed_types)) {
// Verify file size - 2MB max
if ($file_size > $maxsize)
echo "Error: File size is larger than the allowed limit.";
// If file with name already exists then append time in
// front of name of the file to avoid overwriting of file
if(file_exists($file_path)) {
echo "Error uploading {$file_name} - File already exists! <br />";
// $file_path = $upload_dir.date('Y-m-d_').$file_name;
// if( move_uploaded_file($file_tmpname, $file_path)) {
// echo "{$file_name} successfully uploaded <br />";
// }
// else {
// echo "Error uploading {$file_name} - File already exists! <br />";
// }
}
else {
if(move_uploaded_file($file_tmpname, $file_path)) {
//$full_url = "http://{$_SERVER['HTTP_HOST']}/twtxt/".$file_name;
$public_file = $public_media.basename($file_path);
/*
echo '<table class="center"><tr class="preview">';
echo '<td><a href="'.$public_file.'">';
echo '<img src="'.$public_file.'">';
echo '</a></td>';
//$file = str_replace('../', $base_url, $file);
echo '<td><code>![]('.$public_file.')</code></td>';
echo '</tr></table>';
*/
echo "<strong>{$file_name}</strong> successfully uploaded<br>";
//echo "<img src='{$file_path}' width='48' height='48'>";
//echo "<code>![]({$full_url})</code><br />";
//echo "<img src=".$file_path." height=200 width=300 />";
}
else {
echo "Error uploading <strong>{$file_name}</strong><br>";
}
}
}
else {
// If file extension not valid
echo '<p class="warning">';
echo "Error uploading {$file_name} for unknown reason";
echo "({$file_ext} file type is not allowed)<br / >";
echo '</p>';
}
}
}
// Check if file already exists
if (file_exists($target_file)) {
echo "Sorry, file already exists.<br>";
$uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "Sorry, your file is too large.<br>";
$uploadOk = 0;
}
// Allow certain file formats
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
&& $imageFileType != "gif" ) {
echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.<br>";
$uploadOk = 0;
}
// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
echo "Sorry, your file was not uploaded.<br>";
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file <code>". htmlspecialchars( basename($target_file)). "</code> has been uploaded.<br>";
} else {
echo "Sorry, there was an error uploading your file.<br>";
}
else {
// If no files selected
echo "No files selected.";
}
echo "</p>";
}
@ -101,7 +139,7 @@ foreach ($imgs_on_server as $img) {
echo '<tr class="preview">';
echo '<td><a href="'.$public_file.'">';
echo '<img src="'.$public_file.'" style="width=50px;">';
echo '<img src="'.$public_file.'" style="width=50px;">';
echo '</a></td>';
//$img = str_replace('../', $base_url, $img);

View file

@ -8,6 +8,20 @@ $title = "Upload - $title";
include_once 'partials/header.php';
?>
<article>
<form action="" method="post" enctype="multipart/form-data">
<label>Select image to upload</label>
<input type="file" name="fileToUpload" id="fileToUpload"><br>
<input type="submit" value="Upload Image" name="submit">
</form>
</article>
<?php
$media_upload = getcwd() . "/" . $config["media_upload"] . "/";
if (!empty($_POST)) {
@ -17,20 +31,21 @@ if (!empty($_POST)) {
//echo __DIR__ . "<br>";
//echo "upload path: " . $config["media_upload"];
//$media_upload = getcwd()."/media/";
$media_upload = getcwd().$config["media_upload"];
$target_file = $media_upload . basename($_FILES["fileToUpload"]["name"]);
$target_file = str_replace(' ', '-', strtolower($target_file)); // Replace spaces with dashes and set all lower case
$uploadOk = 1;
$imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION));
echo "<p class='notice'>";
// Check if image file is a actual image or fake image
if(isset($_POST["submit"])) {
$check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($check !== false) {
echo "File is an image - " . $check["mime"] . ".";
//echo "File is an image - " . $check["mime"] . ".<br>";
$uploadOk = 1;
} else {
echo "File is not an image.";
echo "File is not an image.<br>";
$uploadOk = 0;
}
}
@ -43,7 +58,7 @@ if (!empty($_POST)) {
// Check file size
if ($_FILES["fileToUpload"]["size"] > 5000000) {
echo "<p class='notice'>Sorry, your file is too large.</p>";
echo "Sorry, your file is too large.<br>";
$uploadOk = 0;
}
@ -60,24 +75,18 @@ if (!empty($_POST)) {
// if everything is ok, try to upload file
} else {
if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
echo "The file ". htmlspecialchars( basename( $_FILES["fileToUpload"]["name"])). " has been uploaded.<br>";
echo "The file <code>". htmlspecialchars( basename($target_file)). "</code> has been uploaded.<br>";
} else {
echo "Sorry, there was an error uploading your file.<br>";
}
}
echo "</p>";
}
?>
<form action="" method="post" enctype="multipart/form-data">
Select image to upload:<br>
<input type="file" name="fileToUpload" id="fileToUpload"><br>
<input type="submit" value="Upload Image" name="submit">
</form>
<?php
// Show images already on server and markdown code
$imgs_on_server = glob($media_upload."*.{jpg,jpeg,png,gif}", GLOB_BRACE);
@ -92,7 +101,7 @@ foreach ($imgs_on_server as $img) {
echo '<tr class="preview">';
echo '<td><a href="'.$public_file.'">';
echo '<img src="'.$public_file.'" style="width=50px;">';
echo '<img src="'.$public_file.'" style="width=50px;">';
echo '</a></td>';
//$img = str_replace('../', $base_url, $img);