Advertisement
firestorm_dev

uploads.php

May 17th, 2017
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 7.81 KB | None | 0 0
  1. <?php
  2. /*
  3. UserSpice 4
  4. An Open Source PHP User Management System
  5. by the UserSpice Team at http://UserSpice.com
  6. This program is free software: you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation, either version 3 of the License, or
  9. (at your option) any later version.
  10.  
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with this program.  If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. require_once '../init.php';
  20. require_once '../classes/class.upload.php';
  21.  
  22. $db = DB::getInstance();
  23.  
  24. $userID = $user->data()->id;
  25.  
  26. $profileQ = $db->query("SELECT * FROM profiles WHERE user_id = ?",array($userID));
  27. $thisProfile = $profileQ->first();
  28. $id = $thisProfile->id;
  29.  
  30. $path_type = Input::get('path');
  31. $dir_dest = (isset($_GET['dir']) ? $_GET['dir'] : $path_type);
  32.  
  33. if ((isset($_POST['action']) ? $_POST['action'] : (isset($_GET['action']) ? $_GET['action'] : '')) == 'image') {
  34.  
  35.     // ---------- IMAGE UPLOAD ---------- //
  36.  
  37.     //:: we create an instance of the class, giving as argument the PHP object
  38.     //:: corresponding to the file field from the form
  39.     //:: All the uploads are accessible from the PHP object $_FILES
  40.     $handle = new Upload($_FILES['avatars']);
  41.  
  42.     //:: then we check if the file has been uploaded properly
  43.     //:: in its *temporary* location in the server (often, it is /tmp)
  44.     if ($handle->uploaded) {
  45.        
  46.         //:::::::::::::::::::::::::: see here for more options :::::::::::::::::::::::::::::::::::::::::://
  47.         //::::                 https://github.com/verot/class.upload.php                           ::::://
  48.         //::::                                                                                     :::://
  49.         //::::                      yes, the file is on the server                                 ::://
  50.         //::::  below are some example settings which can be used if the uploaded file is an image. ://
  51.         //::::::::::::::::::::::::::::: Uncomment to use ::::::::::::::::::::::::::::::::::::::::::://
  52.        
  53.        
  54.         //::::::::::::::: determines is an image will be resized :::::::::::::::::::::::::://
  55.        
  56.         $handle->image_resize = true;
  57.        
  58.         //::::::::::::::: destination image width (default: 150) :::::::::::::::::::::::::://
  59.         //::::::::::::::::  used only if image_resize == true  ::::::::::::::::::::::::::://
  60.        
  61.         $handle->image_x = 230;
  62.        
  63.         //::::::::::::::: destination image height (default: 150) ::::::::::::::::::::::::://
  64.         //::::::::::::::::  used only if image_resize == true  ::::::::::::::::::::::::::://
  65.        
  66.         $handle->image_y = 230;
  67.        
  68.         //:::::::::::::::::::::::; image will be converted  :;;:::::::::::::::::::::::::://
  69.         //:::::: (possible values : ''|'png'|'jpeg'|'gif'|'bmp'; default: '') :::::::::://
  70.        
  71.         //$handle->image_convert = 'jpg';
  72.        
  73.         //::::::::::::::::  text label on the image, value is a string :::::::::::::::::::://
  74.        
  75.         //$handle->image_text = 'test';
  76.        
  77.         //::::::::::::::::  text label on the image, value is a string :::::::::::::::::::://
  78.         //:::::::::: watermark on the image, value is a local filename. :::::::::::::::::://
  79.         //:::::::::: accepted files are GIF, JPG, BMP, PNG and PNG alpha :::::::::::::::://
  80.        
  81.         //$handle->image_watermark = 'watermark.png';
  82.        
  83.         //::::::::::::::::  watermark position withing the image :::::::::::::::::::://
  84.         //::::: combination of one or two from 'TBLR': top, bottom, left, right :::://
  85.        
  86.         //$handle->image_watermark_position = 'LR';
  87.        
  88.         //::::::::::::::::  Format is either in pixels or percentage :::::::::::::::::::://
  89.         //:::::::::::::::::::::: such as 40, '40', '40px' or '40%' ::::::::::::::::::::://
  90.        
  91.         //$handle->image_reflection_height = '25%';
  92.        
  93.         //::::::::::::::::  in pixels between the source image and the reflection :::::://
  94.         //::::::::::::::::::::::::::::::: can be negative ::::::::::::::::::::::::::::://
  95.        
  96.         //$handle->image_reflection_space = 3;
  97.        
  98.         //:::::::::::::::: opacity level at which the reflection starts :::::://
  99.         //:::::::::::::::::::::::: integer between 0 and 100 :::::::::::::::://
  100.        
  101.         //$handle->image_reflection_opacity = 60;
  102.        
  103.         //::::::::::::: Compression level for PNG images ::::::::::::::::::::::::::::::::::::://
  104.         //:::::::::::: between 1 (fast but large files) and 9 (slow but smaller files) :::::://
  105.        
  106.         //$handle->png_compression = 9;
  107.        
  108.         //::::::::::::: Compression quality for JPEG images (default: 85) :::::::::::::::::://
  109.        
  110.         $handle->jpeg_quality = 99;
  111.        
  112.         //::::::::::::: if set to a size in bytes, ::::::::::::::::::::::::::::::::::::::::::://
  113.         //::::: will approximate jpeg_quality so the output image fits within the size :::::://
  114.        
  115.         $handle->jpeg_size = 1072;
  116.        
  117.         //::::: 1024 = 1KB, 51200 = 50kb :::::::::::::::::::::::::::::::::::::::::::::::::::::://
  118.        
  119.         //$handle->file_max_size = '51200';
  120.        
  121.         //::::: Allowed mime types || 'application/pdf','application/msword', 'image/*' :::::://
  122.         //:::::::::::::::::::::::  wildcard accepted, as in image/* ::::::::::::::::::::::::://
  123.        
  124.         $handle->allowed = array('image/jpg', 'image/png');
  125.        
  126.         //::::: Forbidden mime types || 'application/pdf','application/msword', 'image/*' :::::://
  127.         //:::::::::::::::::::::::  wildcard accepted, as in image/* ::::::::::::::::::::::::::://
  128.        
  129.         $handle->forbidden = array('application/*');
  130.  
  131.         //::::: now, we start the upload 'process'. That is, to copy the uploaded file ::::://
  132.         //::::: from its temporary location to the wanted location  ::::::::::::::::::::::://
  133.         //::::: It could be something like $handle->Process('/home/www/my_uploads/'); :::://
  134.        
  135.         $handle->Process('../uploads/'. $path_type);
  136.  
  137.         //:: we check if everything went OK
  138.         if ($handle->processed) {
  139.             //:: everything was fine !
  140.             $old_photo = Input::get('old_photo');
  141.             $path_type = Input::get('path');
  142.             $id = Input::get('id');
  143.            
  144.             if($old_photo !='smile.png'){
  145.                 unlink($path_type.'/'. $old_photo);
  146.             }
  147.            
  148.             //::::::::::: Store the file name & extension to db :::::::::://
  149.             $db->update('profiles',$id,['avatar'=>$handle->file_dst_name]);
  150.            
  151.             //:::::::::: Store complete path + filename & extension to db :::::://
  152.             //$db->update('profiles',$id,['avatar'=>$handle->file_dst_pathname]);
  153.            
  154.             //:::::::::: On Success Redirect to existing page / or another if prefered :::::://
  155.             Redirect::to('../edit_profile.php');
  156.  
  157.  
  158.         } else {
  159.             //:: one error occured
  160.             echo '<p class="result">';
  161.             echo '  <b>File not uploaded to the wanted location</b><br />';
  162.             echo '  Error: ' . $handle->error . '';
  163.             echo '</p>';
  164.         }
  165.  
  166.  
  167.         //:: we delete the temporary files
  168.         $handle-> Clean();
  169.  
  170.     } else {
  171.         //:: if we're here, the upload file failed for some reasons
  172.         //:: i.e. the server didn't receive the file
  173.         echo '<p class="result">';
  174.         echo '  <b>File not uploaded on the server</b><br />';
  175.         echo '  Error: ' . $handle->error . '';
  176.         echo '</p>';
  177.     }
  178.  
  179.  
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement