upload_media.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. // Return a slug from the entered string
  3. function slugify($str) {
  4. $search = array('Ș', 'Ț', 'ş', 'ţ', 'Ş', 'Ţ', 'ș', 'ț', 'î', 'Î', 'ï', 'Ï', 'â', 'Â', 'à', 'À', 'ă', 'Ă', 'ë', 'Ë', 'ê', 'Ê', 'è', 'È', 'é', 'É', 'û', 'Û', 'ô', 'Ô');
  5. $replace = array('s', 't', 's', 't', 's', 't', 's', 't', 'i', 'i', 'i', 'i', 'a', 'a', 'a', 'a', 'a', 'a', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'e', 'u', 'u', 'o', 'o');
  6. $str = str_ireplace($search, $replace, strtolower(trim($str)));
  7. $str = preg_replace('/[^\w\d\-\ ]|^ +| +$/', '', $str);
  8. $str = str_replace(' ', '-', $str);
  9. return preg_replace('/\-{2,}/', '-', $str);
  10. }
  11. // Move the image to its final destination
  12. function uploadImage($file, $fileDestination = "./images/")
  13. {
  14. $fileName = $file['name'];
  15. $fileType = $file['type'];
  16. $fileTempName = $file['tmp_name'];
  17. $fileError = $file['error'];
  18. $fileSize = $file['size'];
  19. $fileExt = explode('.', $fileName);
  20. $fileActualExt = strtolower(end($fileExt));
  21. $allowedExts = array(
  22. 'jpg',
  23. 'jpeg',
  24. 'png',
  25. 'svg',
  26. 'gif'
  27. );
  28. if (in_array($fileActualExt, $allowedExts))
  29. {
  30. if ($fileError === 0)
  31. {
  32. if ($fileSize < 2000000)
  33. {
  34. $fileNewName = uniqid("", true) . "." . $fileActualExt;
  35. $fileDestination = $fileDestination . $fileNewName;
  36. move_uploaded_file($fileTempName, $fileDestination);
  37. return $fileNewName;
  38. }
  39. else
  40. {
  41. return false; // error: file size too big
  42. }
  43. }
  44. else
  45. {
  46. return false; // error: error uploading file
  47. }
  48. }
  49. else
  50. {
  51. return false; // error: file ext not allowed
  52. }
  53. }
  54. ?>