diff options
| author | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2026-07-12 15:51:52 +0300 |
|---|---|---|
| committer | Geo Halkiadakis <gchalkiadakis@sklavenitis.co.gr> | 2026-07-12 15:51:52 +0300 |
| commit | 02608271bb2a9f3a65ed536984d306ee14b48e34 (patch) | |
| tree | 3f23ec48a694ec014e845c4f70d9b5f1c065403c /public/upload.php | |
| download | kalassa-master.tar.gz kalassa-master.tar.bz2 kalassa-master.zip | |
Diffstat (limited to 'public/upload.php')
| -rw-r--r-- | public/upload.php | 193 |
1 files changed, 193 insertions, 0 deletions
diff --git a/public/upload.php b/public/upload.php new file mode 100644 index 0000000..1ed278d --- /dev/null +++ b/public/upload.php @@ -0,0 +1,193 @@ +<?php +// Upload image +// proccess is called via url: +// upload.php ? id = X [&sec = 1] [&del = Y] +// X : item ID +// Y : old image file +// &sec=1 : declares the image as "2nd" image of the item +// --- -- -- - - - +// Workline: +// 1. Validate input +// 2. Check common errors +// -- If everything is ok: +// 3. Upload image +// 4. Make a thumbnail of the image +// 5. Update database record +// --- +// last update: 2020-06-21 +//////////////////////////////////////////////////////////////////////////////// + + +include("config/parametres.php"); + + // #1. VALIDATE input and init paremetres of the script + // --- -- -- - - - + // valid record id exist + // validate delete image (if exist) + // calculate filenames, file-extensions, paths etc. + ////////////////////////////////////////////////////////////////////////////// + + // validate 'id' + // --------------------------------------------------------------------------- + if (isset($_GET['id']) && is_numeric($_GET['id']) && (($_GET['id'] != '0'))) { + $id = $_GET['id']; // id of photo to upload + $sub_dir = intdiv($id, FOLDER_CPC) .'/'; // subdirectory to put the image + // each subdir hosts up to 256 items and 1024 images + // 256 items x (2 imgs + 2 thumbs) = 1024 + } + else { + echo '{"success": false, "err" : "ID ERROR"}'; die(); + } + + // validate 'sec' parameter (if this is the SECOND image of the item) + // --------------------------------------------------------------------------- + if (isset($_GET['sec']) && is_numeric($_GET['sec']) && (($_GET['sec'] == '1'))) { + define("IS_2ND_IMG", true); // is second image of item + } + else { + define("IS_2ND_IMG", false); + } + + // validate 'del' imagefile + // --------------------------------------------------------------------------- + if ( isset($_GET['del']) + && ( preg_match('/^(\d+)_([A-Fa-f0-9]{5})\.jpg/',$_GET['del']) ) // match filename format: ID_HEX{5}.jpg + && ( explode( "_", $_GET['del'] )[0] == $id ) // ID part of file is same as $id + && file_exists( UPLOAD_DIR. $sub_dir . $_GET['del'] ) ) { // file exists (on the right folder) + $delete_old = true; + $del = strtolower($_GET['del']); // old image-file to delete (format: int _ HEX{5} . jpg) + } + else { + $delete_old = false; + } + + // Calculate file-names, file-extensions, paths etc. + // --------------------------------------------------------------------------- + $rnd = substr(strtolower(md5(time())),0,5) ."."; + $ext = strtolower(end(explode(".", $_FILES["fileToUpload"]["name"]))); + + $localFN = $id ."_". $rnd.$ext; // local filename (new uploaded image) + $thumbFN = "pi". $localFN; // thumbnail (pico image) + $path_file = UPLOAD_DIR . $sub_dir . $localFN; // target path + filename + $path_thumb = UPLOAD_DIR . $sub_dir . $thumbFN; // thumbnail path + filename + + $uploadOk = 1; // flag to catch uploading errors + + + // TODO: + // If is 'second' image, check that 1st image exists + // --- Depricated: + // Can be handled in presentation; + // if 1st image is absend, then the second is used instead + + + // Check common errors; then upload image if everything is ok + ////////////////////////////////////////////////////////////////////////////// + + // Check if image file is a actual image or fake image + // --------------------------------------------------------------------------- + $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); + if ($check !== false) { + // File is an image + $uploadOk = 1; + } + else { + // File is not an image + $uploadOk = -1; + echo '{"success": false, "err" : "MIME ERROR"}'; die(); + } + + // Check if file already exists + // if (file_exists($path_file)) { + // unlink($path_file); // delete it if exists + // } + + // Check file size + // --------------------------------------------------------------------------- + if ($_FILES["fileToUpload"]["size"] > 1048576) { // limit filesize to 1MB + $uploadOk = -2; + echo '{"success": false, "err" : "SIZE ERROR"}'; die(); + } + + // Allow certain file formats (only jpeg) + // --------------------------------------------------------------------------- + if ($ext != "jpg" && $ext != "jpeg" ) { // file extension shall be [ jpg | jpeg ] + $uploadOk = -3; + echo '{"success": false, "err" : "JPEG ERROR"}'; die(); + } + // Check if $uploadOk is set to 0 by an error + // (though this would have ended the script already) ------------------------- + if ($uploadOk < 0) { + echo '{"success": false, "err" : "IMG-ERR:'. (-$uploadOk) .'"}'; die(); + + } + // if everything is ok, try to upload the file info folder + // --------------------------------------------------------------------------- + else { + + if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $path_file)) { + // image uploaded; + + // delete old image + old thumbnail + ////////////////////////////////////////////////////////////////////// + if ($delete_old) { + unlink(UPLOAD_DIR. $sub_dir . $del); // delete old image + unlink(UPLOAD_DIR. $sub_dir .'pi'. $del); // delete old thumbnail + } + + // Create thumbnail + ////////////////////////////////////////////////////////////////////// + + // load image and get image size + $img = imagecreatefromjpeg( $path_file ); + $width = imagesx( $img ); + $height = imagesy( $img ); + + // calculate thumbnail size + $new_width = THUMBWIDTH; + $new_height = floor( $height * ( THUMBWIDTH / $width ) ); + + // create a new temporary image + $tmp_img = imagecreatetruecolor( $new_width, $new_height ); + + // copy and resize old image into new image + imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); + + // save thumbnail into a file + imagejpeg( $tmp_img, $path_thumb ); + + + // Update item record on database + ////////////////////////////////////////////////////////////////////// + + // init database connection + $_dbc = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_DBMS); + if ($_dbc->connect_errno) { + echo "Database connection failed; Please try in a few minutes."; + if (TESTING) echo $_dbc->connect_error; + $_dbc->close(); die(); + } + $_dbc->set_charset("utf8"); + + if (IS_2ND_IMG) + $_upd = $_dbc->prepare("UPDATE items SET img2 = ? WHERE id = ?"); + else + $_upd = $_dbc->prepare("UPDATE items SET img = ? WHERE id = ?"); + $_upd->bind_param("sd", $localFN, $id); + $_upd->execute(); + $_upd->close(); + $_dbc->close(); + + + + // send image thumbnail for preview + // ------------------------------------------------------------------- + + echo '{ "success" : true, "img" : "'. $localFN .'", "sec" : "'. (IS_2ND_IMG ? 'true' : 'false') .'", "thumbnail" : "'. $path_thumb .'" }'; + die(); // everything done! + + } + else { + echo '{"success": false, "err" : "UPLOAD ERR"}'; die(); + } + } |
