photo.php - PHP Online

Form of PHP Sandbox

Enter Your PHP code here for testing/debugging in the Online PHP Sandbox. As in the usual PHP files, you can also add HTML, but do not forget to add the tag <?php in the places where the PHP script should be executed.



Your result can be seen below.

Result of php executing





Full code of photo.php.php

  1. <?php
  2. class Photo extends Db_object
  3. {
  4. protected static $db_table = "photos"; 
  5.         protected static $db_table_fields = array('photo_id', 'title', 'description','filename','type','size');
  6.         public $photo_id;
  7.         public $title;
  8.         public $description;
  9.         public $filename;
  10.         public $type;
  11.     public $size;
  12.     public $tmp_path;
  13.     public $upload_directory = "images";
  14.     public $errors = array();
  15.     public $upload_errors_array = array(
  16.      UPLOAD_ERR_OK         =>"There is no error",
  17.      UPLOAD_ERR_INI_SIZE   =>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
  18.      UPLOAD_ERR_FORM_SIZE  =>"The uploaded file exceeds the MAZ_FILE_SIZE directive that was specified in HTML form",
  19.      UPLOAD_ERR_PARTIAL    =>"The uploaded file was partially uploaded.",
  20.      UPLOAD_ERR_NO_FILE    =>"No file uploaded.",
  21.      UPLOAD_ERR_NO_TMP_DIR =>"Missing a temporary folder.",
  22.      UPLOAD_ERR_CANT_WRITE =>"Failed to write file to disk.",
  23.      UPLOAD_ERR_EXTENSION  =>"A php extension stopped the file upload.",
  24.      );
  25.     //this is passing $_FILES['uploaded_files'] as an argument
  26.     public function set_file($file)
  27.     {
  28.             if(empty($file) || !$file || !is_array($file))
  29.             {
  30.                     $this->errors[] = "there was  no file uploaded here";
  31.                     return false;
  32.             }
  33.             else 
  34.                     if($file['error'] !=0)
  35.                     {
  36.                             $this->errors[] = $this->upload_errors_array[$file['errors']];
  37.                             return false;
  38.                     }
  39.                     else
  40.                     {
  41.                             $this->filename =  basename($file['name']);
  42.                             $this->tmp_path = $file['tmp_name'];
  43.                             $this->type = $file['type'];
  44.                             $this->size = $file['size'];
  45.                     }
  46.     }
  47.         public function save()
  48.         {
  49.                 if($this->photo_id)
  50.                 {
  51.                         $this->update();
  52.                 }
  53.                 else
  54.                 {
  55.                         if(!empty($this->errors))
  56.                         {
  57.                                 return false;
  58.                         }
  59.                         if(empty($this->filename) || empty($this->tmp_path))
  60.                         {
  61.                                 $this->errors[] = "the file was not available";
  62.                                 return false;
  63.                         }
  64.                         $target_path = SITE_ROOT . DS . 'admin' . DS . $this->upload_directory . DS . $this->filename;
  65.                         if(file_exists($target_path))
  66.                         {
  67.                                 $this->errors[] = "the file {$this->filename} already exists";
  68.                                 return false;
  69.                         }
  70.                         if(move_uploaded_file($this->tmp_path, $target_path))
  71.                         {
  72.                                 if( $this->create())
  73.                                 {
  74.                                         unset($this->tmp_path);
  75.                                         return true;
  76.                                 }
  77.                         }
  78.                         else
  79.                         {
  80.                                 $this->errors[] = "the file directory probably does not have permission";
  81.                                 return false;
  82.                         }
  83.                        
  84.                 }
  85.         }
  86.  
  87. }
  88. ?>
File Description
  • photo.php
  • PHP Code
  • 28 Sep-2020
  • 2.71 Kb
You can Share it: