Advertisement
Guest User

Untitled

a guest
Jan 7th, 2019
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.81 KB | None | 0 0
  1. function photoPlant($pID){
  2. // db
  3. include "includes/dbConfig.php";
  4. // init
  5. $out = null;
  6. // gen hash
  7. $varA = microtime();
  8. $varB = time();
  9. $varC = $varA . $varB;
  10. $hash = md5($varC);
  11. // prepare upload
  12. $currentDir = getcwd();
  13. $uploadDirectory = "/gallery/";
  14. $errors = []; // Store all foreseen and unforseen errors here
  15. $fileExtensions = ['jpeg','jpg','png', '']; // Get all the file extensions, including empty for mobile
  16. // reformat empty file extension
  17. if ($fileExtension === ""){
  18.   $fileExtension = "jpg";
  19. }
  20. $fileName = $_FILES['photo']['name'];
  21. $fileTmpName = $_FILES['photo']['tmp_name'];
  22. $fileSize = $_FILES['photo']['size'];
  23. $fileType = $_FILES['photo']['type'];
  24. $fileExtension = strtolower(end(explode('.',$fileName)));
  25. // reformat filename
  26. $fileName = $hash . "." . $fileExtension;
  27. $uploadPath = $currentDir . $uploadDirectory . basename($fileName);
  28. if (! in_array($fileExtension,$fileExtensions)) {
  29.     $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
  30. }
  31. if ($fileSize > 8000000) {
  32.     $errors[] = "This file is more than 8MB. Sorry, it has to be less than or equal to 8MB";
  33. }
  34. if (empty($errors)) {
  35.     $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
  36.     if ($didUpload) {
  37.         $out .= "ok"; // everything is ok give feedback ok
  38.     } else {
  39.         $out .= "An error occurred somewhere. Try again or contact the admin";
  40.     }
  41. } else {
  42.     foreach ($errors as $error) {
  43.         $out .= $error . "These are the errors" . "\n";
  44.     }
  45. }
  46.   // store img on db
  47.   // prepare data
  48.   $timeStamp = time();
  49.   // query
  50.   $query = mysqli_query($con, "INSERT INTO photo_table (photo_parent_id, photo_name, photo_timestamp) VALUES ($pID, '$fileName', $timeStamp)");
  51.   // run query
  52.   if (!$query){
  53.     $out = mysqli_error($con);
  54.   }
  55. // return
  56. return $out;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement