From e2122741faf721e2d613cafad692f700d0f02850 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?s=C3=B8renpeter?= Date: Thu, 13 Feb 2025 18:20:27 +0100 Subject: [PATCH] Image upload works now --- views/upload_img.php | 41 +++++++++------ views/upload_img_w3c.php | 109 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+), 16 deletions(-) create mode 100644 views/upload_img_w3c.php diff --git a/views/upload_img.php b/views/upload_img.php index 68545a4..cc0a2bd 100644 --- a/views/upload_img.php +++ b/views/upload_img.php @@ -8,6 +8,20 @@ $title = "Upload - $title"; include_once 'partials/header.php'; +?> + +
+ +
+ +
+ +
+ +
+ +"; //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 "

"; + // 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"] . ".
"; $uploadOk = 1; } else { - echo "File is not an image."; + echo "File is not an image.
"; $uploadOk = 0; } } @@ -43,7 +58,7 @@ if (!empty($_POST)) { // Check file size if ($_FILES["fileToUpload"]["size"] > 5000000) { - echo "

Sorry, your file is too large.

"; + echo "Sorry, your file is too large.
"; $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.
"; + echo "The file ". htmlspecialchars( basename($target_file)). " has been uploaded.
"; } else { echo "Sorry, there was an error uploading your file.
"; } } + echo "

"; + } -?> -
- Select image to upload:
-
- -
- - -'; echo ''; - echo ''; + echo ''; echo ''; //$img = str_replace('../', $base_url, $img); diff --git a/views/upload_img_w3c.php b/views/upload_img_w3c.php new file mode 100644 index 0000000..68545a4 --- /dev/null +++ b/views/upload_img_w3c.php @@ -0,0 +1,109 @@ +"; + //echo __DIR__ . "
"; + //echo "upload path: " . $config["media_upload"]; + + //$media_upload = getcwd()."/media/"; + $media_upload = getcwd().$config["media_upload"]; + $target_file = $media_upload . basename($_FILES["fileToUpload"]["name"]); + $uploadOk = 1; + $imageFileType = strtolower(pathinfo($target_file,PATHINFO_EXTENSION)); + + // 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"] . "."; + $uploadOk = 1; + } else { + echo "File is not an image."; + $uploadOk = 0; + } + } + + // Check if file already exists + if (file_exists($target_file)) { + echo "Sorry, file already exists.
"; + $uploadOk = 0; + } + + // Check file size + if ($_FILES["fileToUpload"]["size"] > 5000000) { + echo "

Sorry, your file is too large.

"; + $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.
"; + $uploadOk = 0; + } + + // Check if $uploadOk is set to 0 by an error + if ($uploadOk == 0) { + echo "Sorry, your file was not uploaded.
"; + // 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.
"; + } else { + echo "Sorry, there was an error uploading your file.
"; + } + } + +} + +?> + +
+ Select image to upload:
+
+ +
+ + + -(filemtime($a) - filemtime($b))); + +echo ''; + +foreach ($imgs_on_server as $img) { + + $public_file = $config["public_media"] . "/" . basename($img); + + echo ''; + echo ''; + + //$img = str_replace('../', $base_url, $img); + echo ''; + echo ''; +} + +echo '
'; + echo ''; + echo '![]('.$public_file.')
'; + +?> + + + +