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.
Result of php executing
Full code of photo.php.php
- <?php
- class Photo extends Db_object
- {
- protected static $db_table = "photos";
- protected static $db_table_fields = array('photo_id', 'title', 'description','filename','type','size');
- public $photo_id;
- public $title;
- public $description;
- public $filename;
- public $type;
- public $size;
- public $tmp_path;
- public $upload_directory = "images";
- public $errors = array();
- public $upload_errors_array = array(
- UPLOAD_ERR_OK =>"There is no error",
- UPLOAD_ERR_INI_SIZE =>"The uploaded file exceeds the upload_max_filesize directive in php.ini",
- UPLOAD_ERR_FORM_SIZE =>"The uploaded file exceeds the MAZ_FILE_SIZE directive that was specified in HTML form",
- UPLOAD_ERR_PARTIAL =>"The uploaded file was partially uploaded.",
- UPLOAD_ERR_NO_FILE =>"No file uploaded.",
- UPLOAD_ERR_NO_TMP_DIR =>"Missing a temporary folder.",
- UPLOAD_ERR_CANT_WRITE =>"Failed to write file to disk.",
- UPLOAD_ERR_EXTENSION =>"A php extension stopped the file upload.",
- );
- //this is passing $_FILES['uploaded_files'] as an argument
- public function set_file($file)
- {
- if(empty($file) || !$file || !is_array($file))
- {
- $this->errors[] = "there was no file uploaded here";
- return false;
- }
- else
- if($file['error'] !=0)
- {
- $this->errors[] = $this->upload_errors_array[$file['errors']];
- return false;
- }
- else
- {
- $this->filename = basename($file['name']);
- $this->tmp_path = $file['tmp_name'];
- $this->type = $file['type'];
- $this->size = $file['size'];
- }
- }
- public function save()
- {
- if($this->photo_id)
- {
- $this->update();
- }
- else
- {
- if(!empty($this->errors))
- {
- return false;
- }
- if(empty($this->filename) || empty($this->tmp_path))
- {
- $this->errors[] = "the file was not available";
- return false;
- }
- $target_path = SITE_ROOT . DS . 'admin' . DS . $this->upload_directory . DS . $this->filename;
- if(file_exists($target_path))
- {
- $this->errors[] = "the file {$this->filename} already exists";
- return false;
- }
- if(move_uploaded_file($this->tmp_path, $target_path))
- {
- if( $this->create())
- {
- unset($this->tmp_path);
- return true;
- }
- }
- else
- {
- $this->errors[] = "the file directory probably does not have permission";
- return false;
- }
- }
- }
- }
- ?>