Jump to content

Uploading videos?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
doheja07

doheja07

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Hi. I have been following a tutorial that shows you how to build your own personal website similar to flickr. I am now trying to make changes to the script so that I will be able to upload videos. I tried increasing the max file size. That allowed me to upload it, but now all I see is a red x. What other things will I need to change so that I can upload and view videos?

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
What error do you get? What does your code look like? Without more details there is no way we can tell you what the problem is.

#3
doheja07

doheja07

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
This is the code for the uploading page:
<?php
require_once('../../includes/initialize.php');
if (!$session->is_logged_in()) { redirect_to("login.php"); }
?>
<?php 
$max_file_size = 26214400;

	if(isset($_POST['submit'])) {
		$photo = new Photograph();
		$photo->caption = $_POST['caption'];
		$photo->attach_file($_FILES['file_upload']);
		if($photo->save()) {
			$session->message("Photograph uploaded sucessfully.");
			redirect_to('list_photos.php');
		} else {
			$message = join("<br />", $photo->errors);
		}
	}
?>
<?php require_once('../layouts/admin_header.php'); ?>

	<h2>Photo Upload</h2>
    
    <?php echo output_message($message); ?>
    <form action="photo_upload.php" enctype="multipart/form-data" method="post">
     <input type="hidden" name="MAX_FILE_SIZE" value="<?php echo $max_file_size; ?>" />
     <p><input type="file" name="file_upload" /></p>
     <p>Caption: <input type="text" name="caption" value="" /></p>
     <input type="submit" name="submit" value="Upload" />
    </form>

<?php require_once('../layouts/admin_footer.php'); ?>

Here is the code for the Photograph object:

<?php
require_once("database.php");

class Photograph extends DatabaseObject {
	
	protected static $table_name="photographs";
	protected static $db_fields = array('id', 'filename', 'type', 'size', 'caption');
	public $id;
	public $filename;
	public $type;
	public $size;
	public $caption;
	
	private $temp_path;
	protected $upload_dir="images";
	public $errors=array();
	protected $upload_errors = array(
		UPLOAD_ERR_OK => "No errors.",
		UPLOAD_ERR_INI_SIZE => "Larger than upload_max_fliesize.",
		UPLOAD_ERR_FORM_SIZE => "Larger than form MAX_FILE_SIZE.",
		UPLOAD_ERR_PARTIAL => "Partial upload.",
		UPLOAD_ERR_NO_FILE => "No file.",
		UPLOAD_ERR_NO_TMP_DIR => "No temporary directory.",
		UPLOAD_ERR_CANT_WRITE => "Can't write to disk.",
		UPLOAD_ERR_EXTENSION => "File upload stopped be extensios."
	);
	
	public function save() {
		if(isset($this->id)) {
			$this->update();
		} else {
			if(!empty($this->errors)) { return false; }
			if(strlen($this->caption) > 255) {
				$this->errors[] = "The caption can only be 255 characters long.";
				return false;
			}
			if(empty($this->filename) || empty($this->temp_path)) {
				$this->errors[] = "The file location was not available.";
				return false;
			}
			$target_path = SITE_ROOT .DS. 'public' .DS. $this->upload_dir .DS. $this->filename;
			if(file_exists($target_path)) {
				$this->errors[] = "The file {$this->filename} already exists.";
				return false;
			}
			if(move_uploaded_file($this->temp_path, $target_path)) {
				if($this->create()) {
					unset($this->temp_path);
					return true;
				}
			} else {
				$this->errors[] = "The file upload failed, possibly due to incorrect permissions on the upload folder.";
				return false;
			}
		}
	}
	
	public function destroy() {
		if($this->delete()) {
			$target_path = SITE_ROOT.DS.'public'.DS.$this->image_path();
			return unlink($target_path) ? true : false;
		} else {
			return false;
		}
	}
	
	public function image_path() {
		return $this->upload_dir.DS.$this->filename;
	}
	
	public function size_as_text() {
		if($this->size < 1024) {
			return "{$this->size} bytes";
		} elseif($this->size < 1048576) {
			$size_kb = round($this->size/1024);
			return "{$size_kb} KB";
		} else {
			$size_mb = round($this->size/1048576, 1);
			return "{$size_mb} MB";
		}
	}
		
	public function attach_file($file) {
		if(!$file || empty($file) || !is_array($file)) {
			$this->errors[] = "No file was uploaded.";
			return false;
		} elseif($file['error'] != 0) {
			$this->errors[] = $this->upload_errors[$file['error']];
			return false;
		} else {
			$this->temp_path = $file['tmp_name'];
			$this->filename = basename($file['name']);
			$this->type = $file['type'];
			$this->size = $file['size'];
			return true;
		}
	}

	
	private function has_attribute($attribute) {
		$object_vars = $this->attributes();
		return array_key_exists($attribute, $object_vars);
	}
	
	public function comments() {
		return Comment::find_comments_on($this->id);
	}
	
	public static function find_all() {
		return self::find_by_sql("SELECT * FROM ".self::$table_name);
	}
	
	public static function find_by_id($id=0) {
		global $database;
		$result_array = self::find_by_sql("SELECT * FROM ".self::$table_name." WHERE id=".$database->escape_value($id)." LIMIT 1");
		return !empty($result_array) ? array_shift($result_array) : false;
	}
	
	public static function find_by_sql($sql="") {
		global $database;
		$result_set = $database->query($sql);
		$object_array = array();
		while ($row = $database->fetch_array($result_set)) {
			$object_array[] = self::instantiate($row);
		}
		return $object_array;
	}
	
	public static function count_all() {
		global $database;
		$sql = "SELECT COUNT(*) FROM ".self::$table_name;
		$result_set = $database->query($sql);
		$row = $database->fetch_array($result_set);
		return array_shift($row);
	}
	
	protected function attributes() {
		$attributes = array();
		foreach(self::$db_fields as $field) {
			if(property_exists($this, $field)) {
				$attributes[$field] = $this->$field;
			}
		}
		return $attributes;
	}
	
	protected function sanitized_attributes() {
		global $database;
		$clean_attributes = array();
		foreach($this->attributes() as $key => $value){
			$clean_attributes[$key] = $database->escape_value($value);
		}
		return $clean_attributes;
	}
	
//	public function save() {
//		return isset($this->id) ? $this->update() : $this->create();
//	}
	
	public function create() {
		global $database;
		$attributes = $this->sanitized_attributes();
		$sql = "INSERT INTO ".self::$table_name." (";
		$sql .= join(", ", array_keys($attributes));
		$sql .= ") VALUES ('";
		$sql .= join("', '", array_values($attributes));
		$sql .= "')";
		if($database->query($sql)) {
			$this->id = $database->insert_id();
			return true;
		} else {
			return false;
		}
	}
	
	public function update() {
		global $database;
		$attributes = $this->sanitized_attributes();
		$attribute_pairs = array();
		foreach($attributes as $key => $value) {
			$attribute_pairs[] = "{$key}='{$value}'";
		}
		$sql = "UPDATE ".self::$table_name." SET ";
		$sql .= join(", ", $attribute_pairs);
		$sql .= " WHERE id=". $database->escape_value($this->id);
		$database->query($sql);
		return ($database->affected_rows() == 1) ? true : false;
	}
	
	public function delete() {
		global $database;
		$sql = "DELETE FROM ".self::$table_name." ";
		$sql .= "WHERE id=". $database->escape_value($this->id);
		$sql .= " LIMIT 1";
		$database->query($sql);
		return ($database->affected_rows() == 1) ? true : false;
	}
	
	private static function instantiate($record) {
		$object = new self;

		foreach($record as $attribute=>$value) {
			if($object->has_attribute($attribute)) {
				$object->$attribute = $value;
			}
		}
		return $object;
	}
}

?>
And here is the code for the Database object:

<?php

class MySQLDatabase {

	private $connection;
	public $last_query;
	private $magic_quotes_active;
	private $real_escape_string_exists;
	
	function __construct() {
		$this->open_connection();
		$this->magic_quotes_active = get_magic_quotes_gpc();
		$this->real_escape_string_exists = function_exists( "mysql_real_escape_string" );
	}
	public function open_connection() {
		$this->connection = mysql_connect(localhost, root, mmfwcl);
		if (!$this->connection) {
			die("Database connection failed: " . mysql_error());
        } else {
			$db_select = mysql_select_db(photo_gallery, $this->connection);
			if (!$db_select) {
				die("Database selection failed: " . mysql_error());
			}
		}
	}
	
	public function close_connection() {
		if(isset($this->connection)) {
			mysql_close($this->connection);
			unset($this->connection);
		}
	}
	
	public function escape_value( $value ) {
		
		if( $this->real_escape_string_exists ) {
			if( $this->magic_qoutes_active = 1 ) { $value = stripslashes( $value ); }
			$value = mysql_real_escape_string( $value );
		} else {
			if( $this->magic_qoutes_active = 0 ) { $value = addslashes( $value ); }
		}
		return $value;
	}
	
	public function query($sql) {
		$this->last_query = $sql;
		$result = mysql_query($sql, $this->connection);
		$this->confirm_query($result);
		return $result;
	}
	
	public function fetch_array($result_set) {
		return mysql_fetch_array($result_set);
	}
	
	public function num_rows($result_set) {
		return mysql_num_rows($result_set);
	}
	
	public function insert_id() {
		return mysql_insert_id($this->connection);
	}
	
	public function affected_rows() {
		return mysql_affected_rows($this->connection);
	}
	
	private function confirm_query($result) {
		if (!$result) {
			$output = ("Database query failed: " . mysql_error() . "<br /><br />");
			$output .= ("Last SQL query: " . $this->last_query);
			die ($output);
		}
	}
}

$database = new MySQLDatabase();

?>

I don't get an error. I think the problem is that it is expecting a photo instead of a movie. I think that is why I am seeing the red x, but I don't know how to get it so that it expects to receive a movie.

Edited by Jaan, 28 May 2009 - 10:45 AM.
Please use code tags when you're posting your codes!


#4
doheja07

doheja07

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
It's not exactly that I can't upload videos. I just can't display them. I was following a tutorial. The project was building a website for a personal online photo album. Now, instead of uploading photos, I want to upload videos. I tried increasing max file size to allow for the larger video files and I don't know what other changes I need to make to the script to allow for video uploads.

#5
LevonNikox

LevonNikox

    Newbie

  • Members
  • PipPip
  • 10 posts
you want to display the video after downloading?

#6
doheja07

doheja07

    Learning Programmer

  • Members
  • PipPipPip
  • 31 posts
Yeah. Pretty much. At this point, all I get is a red x because it is expecting a photo and I don't know how to tell it to expect a video.

#7
LevonNikox

LevonNikox

    Newbie

  • Members
  • PipPip
  • 10 posts
then it isn't a php error! It's just a tag that you need in html.
Use this tags.Try wich one is better.
<embed src="video.avi" />
<object data="video.avi" type="video/avi" />