Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function photoPlant($pID){
- // db
- include "includes/dbConfig.php";
- // init
- $out = null;
- // gen hash
- $varA = microtime();
- $varB = time();
- $varC = $varA . $varB;
- $hash = md5($varC);
- // prepare upload
- $currentDir = getcwd();
- $uploadDirectory = "/gallery/";
- $errors = []; // Store all foreseen and unforseen errors here
- $fileExtensions = ['jpeg','jpg','png', '']; // Get all the file extensions, including empty for mobile
- // reformat empty file extension
- if ($fileExtension === ""){
- $fileExtension = "jpg";
- }
- $fileName = $_FILES['photo']['name'];
- $fileTmpName = $_FILES['photo']['tmp_name'];
- $fileSize = $_FILES['photo']['size'];
- $fileType = $_FILES['photo']['type'];
- $fileExtension = strtolower(end(explode('.',$fileName)));
- // reformat filename
- $fileName = $hash . "." . $fileExtension;
- $uploadPath = $currentDir . $uploadDirectory . basename($fileName);
- if (! in_array($fileExtension,$fileExtensions)) {
- $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
- }
- if ($fileSize > 8000000) {
- $errors[] = "This file is more than 8MB. Sorry, it has to be less than or equal to 8MB";
- }
- if (empty($errors)) {
- $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
- if ($didUpload) {
- $out .= "ok"; // everything is ok give feedback ok
- } else {
- $out .= "An error occurred somewhere. Try again or contact the admin";
- }
- } else {
- foreach ($errors as $error) {
- $out .= $error . "These are the errors" . "\n";
- }
- }
- // store img on db
- // prepare data
- $timeStamp = time();
- // query
- $query = mysqli_query($con, "INSERT INTO photo_table (photo_parent_id, photo_name, photo_timestamp) VALUES ($pID, '$fileName', $timeStamp)");
- // run query
- if (!$query){
- $out = mysqli_error($con);
- }
- // return
- return $out;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement