Jump to content

Video Submitting-Please Help


masternick

Recommended Posts

Meh, I was just working on something like this for a client earlier today. How I did it was have the physical file uploaded to a directory, but save the LOCATION of the file into a DB table.

 

This is my upload script:

<form action="" method="post" enctype="multipart/form-data">
          <table width="400" cellpadding="3" >
            <tr>
              <td colspan="2"> </td>
            </tr>
            <tr>
              <td width="120">1. Select a file »</td>
              <td width="280"><input type="file" name="uploaded_file" /></td>
            </tr>
            <tr>
              <td>2. Add Description</td>
              <td><textarea name="description"></textarea></td>
            </tr>
            <tr>
            <td>3. Upload</td>
              <td><input type="submit" name="sendForm" value="Upload File" />
                <br /></td>
            </tr>
          </table>
        </form>
        <?php

  error_reporting(E_ALL ^ E_NOTICE); // Show all major errors.

  // Check to see if the button has been pressed
  if (!empty($_REQUEST['sendForm']))
  {
    // Assign the name to a variable
    $name = $_FILES['uploaded_file']['name'];
    // Assign the tmp_name to a variable
    $tmp_name = $_FILES['uploaded_file']['tmp_name'];
    // Assign the error to a variable
    $error = $_FILES['uploaded_file']['error'];
    // Assign the size to a variable
    $size = $_FILES['uploaded_file']['size'];
    // No trailing//
    $uploadFilesTo = '../media';
    // Create safe filename
    $name = ereg_replace('[^A-Za-z0-9.]', '-', $name);
    // Disallowed file extensions
    //what files you don't want upoad... leave this alone and you should be fine but you could add more
    $naughtyFileExtension = array("php", "php3", "asp", "inc", "txt", "wma","js", "exe", "jsp", "map", "obj", " ", "", "html", "cur", "ani");    // Returns an array that includes the extension
    $fileInfo = pathinfo($name);
    // Check extension
    if (!in_array($fileInfo['extension'], $naughtyFileExtension))
    {
      // Get filename
      $name = getNonExistingFilename($uploadFilesTo, $name);
      // Upload the file
      if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))
      {
  		$array = explode(".", $name);
		$type = $array[1];
  		$time = time();
		$author = $session->username;
  		$q = mysql_query("INSERT INTO files (id, author, type, name, description, time) VALUES ('', '$author', '$type', '$name', '$description', $time)");
          // Show success message
          echo '<center><p>Your File has uploaded successfully<br />'.$uploadFilesTo.'/'.$name.'</p></center>';
      }
      else
      {
          // Show failure message
          echo '<center><p>File failed to upload to /'.$name.'</p></center>';
      }
    }
    else
    {
        // Bad File type
        echo '<center><p>The file uses an extension we don\'t allow.</p></center>';
    }
  }

  // Functions do not need to be inline with the rest of the code
  function getNonExistingFilename($uploadFilesTo, $name)
  {
      if (!file_exists($uploadFilesTo . '/' . $name))
          return $name;
     
      return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name);
  }
?>

 

 

 

And the table:

 

CREATE TABLE `files` (
  `id` int(11) NOT NULL auto_increment,
  `author` varchar(64) default NULL,
  `type` varchar(40) default NULL,
  `name` varchar(128) default NULL,
  `description` blob,
  `time` varchar(32) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

 

How you retrieve the file names from the db is up to you, but I run a loop to select all the file names from the db, and then display them as a table with links to a page that will load the location of the file in the browser and play the file that way. Then all you have to do for a rating system is have another db table with each rating associated with the ID. Or just create another row in the first table. Idk.

 

I hope this helped.

Oh sweet, it automatically cuts down the height of your coding boxes. Here, I will post my entire source.

 

Upload.php

<form action="" method="post" enctype="multipart/form-data">
          <table width="400" cellpadding="3" >
            <tr>
              <td colspan="2"> </td>
            </tr>
            <tr>
              <td width="120">1. Select a file »</td>
              <td width="280"><input type="file" name="uploaded_file" /></td>
            </tr>
            <tr>
              <td>2. Add Description</td>
              <td><textarea name="description"></textarea></td>
            </tr>
            <tr>
            <td>3. Upload</td>
              <td><input type="submit" name="sendForm" value="Upload File" />
                <br /></td>
            </tr>
          </table>
        </form>
        <?php

  error_reporting(E_ALL ^ E_NOTICE); // Show all major errors.

  // Check to see if the button has been pressed
  if (!empty($_REQUEST['sendForm']))
  {
    // Assign the name to a variable
    $name = $_FILES['uploaded_file']['name'];
    // Assign the tmp_name to a variable
    $tmp_name = $_FILES['uploaded_file']['tmp_name'];
    // Assign the error to a variable
    $error = $_FILES['uploaded_file']['error'];
    // Assign the size to a variable
    $size = $_FILES['uploaded_file']['size'];
    // No trailing//
    $uploadFilesTo = '../media';
    // Create safe filename
    $name = ereg_replace('[^A-Za-z0-9.]', '-', $name);
    // Disallowed file extensions
    //what files you don't want upoad... leave this alone and you should be fine but you could add more
    $naughtyFileExtension = array("php", "php3", "asp", "inc", "txt", "wma","js", "exe", "jsp", "map", "obj", " ", "", "html", "cur", "ani");    // Returns an array that includes the extension
    $fileInfo = pathinfo($name);
    // Check extension
    if (!in_array($fileInfo['extension'], $naughtyFileExtension))
    {
      // Get filename
      $name = getNonExistingFilename($uploadFilesTo, $name);
      // Upload the file
      if (move_uploaded_file($tmp_name, $uploadFilesTo.'/'.$name))
      {
  		$array = explode(".", $name);
		$type = $array[1];
  		$time = time();
		$author = $session->username;
  		$q = mysql_query("INSERT INTO files (id, author, type, name, description, time) VALUES ('', '$author', '$type', '$name', '$description', $time)");
          // Show success message
          echo '<center><p>Your File has uploaded successfully<br />'.$uploadFilesTo.'/'.$name.'</p></center>';
      }
      else
      {
          // Show failure message
          echo '<center><p>File failed to upload to /'.$name.'</p></center>';
      }
    }
    else
    {
        // Bad File type
        echo '<center><p>The file uses an extension we don\'t allow.</p></center>';
    }
  }

  // Functions do not need to be inline with the rest of the code
  function getNonExistingFilename($uploadFilesTo, $name)
  {
      if (!file_exists($uploadFilesTo . '/' . $name))
          return $name;
     
      return getNonExistingFilename($uploadFilesTo, rand(100, 200) . '_' . $name);
  }
?>

 

Videos.php

          
<p class="header">Video Archive</p>        
<?php
$q = mysql_query("SELECT * FROM photos");
$num = mysql_num_rows($q);
if($num == 0){
echo "<br><br><br><center>No photos uploaded yet! Check back soon!<center>";
}
else
{
$query = mysql_query("SELECT * FROM files");
$i = 0;
echo "<table align=\"center\">";
while($data = mysql_fetch_array($query)){
if($col == 0){
echo "<tr>";
}
echo "<td onclick=\"parent.location.href='viewvideo.php?id=".$data['id']."'\" width=\"125px\" height=\"100px\" style=\"border:1px #000000 solid;\"><img src=\"images/movies.png\" width=\"123px\" height=\"98px\"></td>";
if($col == 3){
echo "</tr>";
$i = 0;
$num--;
}
else
{
$i--;
$col++;
}
}
}
echo "</table>";
?>

 

Viewvideo.php

<?php
$id = $_GET['id'];
$q = mysql_query("SELECT * FROM files WHERE id = '$id'");
$data = mysql_fetch_array($q);
echo "
<center>
<object id=\"MediaPlayer1\" width=\"450\" height=\"450\"
standby=\"Loading Windows Media Player components...\"
data=\"media/".$data['name']."\"  
type=\"application/x-mplayer2\"
title=\"Title of this video object\" 
style=\"background-color: #000000;\" >
	<param name=\"filename\" value=\"media/".$data['name']."\" />
	<param name=\"height\" value=\"450\" />
	<param name=\"width\" value=\"450\" />
	<param name=\"autoStart\" value=\"1\" /> 
	<param name=\"autoPlay\" value=\"1\" /> 
	<param name=\"AnimationatStart\" value=\"1\" />
	<param name=\"showdisplay\" value=\"0\" />
	<param name=\"TransparentAtStart\" value=\"0\" />
	<param name=\"ShowControls\" value=\"1\" />
	<param name=\"ShowStatusBar\" value=\"1\" />
	<param name=\"ClickToPlay\" value=\"0\" />
	<param name=\"bgcolor\" value=\"#000000\" />
	<param name=\"volume\" value=\"100%\" />
	<param name=\"InvokeURLs\" value=\"0\" />
	<param name=\"loop\" value=\"0\" />
      <em>Description of this video object</em>
</object>
</center>
";
?>

 

Lastly, the DB table:

CREATE TABLE `files` (
  `id` int(11) NOT NULL auto_increment,
  `author` varchar(64) default NULL,
  `type` varchar(40) default NULL,
  `name` varchar(128) default NULL,
  `description` blob,
  `time` varchar(32) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

 

 

You may have to change a few of the URL's around here and there, but just look over it and you will see that it's very easy to edit.

Correction on videos.php:

           
<p class="header">Video Archive</p>        
<?php
$q = mysql_query("SELECT * FROM files");
$num = mysql_num_rows($q);
if($num == 0){
echo "<br><br><br><center>No Videos uploaded yet! Check back soon!<center>";
}
else
{
$query = mysql_query("SELECT * FROM files");
$i = 0;
echo "<table align=\"center\">";
while($data = mysql_fetch_array($query)){
if($col == 0){
echo "<tr>";
}
echo "<td onclick=\"parent.location.href='viewvideo.php?id=".$data['id']."'\" width=\"125px\" height=\"100px\" style=\"border:1px #000000 solid;\"><img src=\"images/movies.png\" width=\"123px\" height=\"98px\"></td>";
if($col == 3){
echo "</tr>";
$i = 0;
$num--;
}
else
{
$i--;
$col++;
}
}
}
echo "</table>";
?>

You can execute the .sql file in phpmyadmin i think, there should be a text area where you key in the db info

 

CREATE TABLE `files` (
  `id` int(11) NOT NULL auto_increment,
  `author` varchar(64) default NULL,
  `type` varchar(40) default NULL,
  `name` varchar(128) default NULL,
  `description` blob,
  `time` varchar(32) default NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1

 

then "execute" or "run", im not to sure where it is in phpmyadmin tho, you should be able to google it  :)

That is a very basic query. You could probally use it even with MySQL 1.0 lol. If there was a 1.0.

 

You need to do it yourself. Go to the MySQL forum and ask how to access your MySQL panel and run basic create table queries.

 

If all else fails, you can always IM me on MSN or AIM.

Basically, you need to create a connect.php file. This contains information on how to connect to mysql. Once you connect, everything is pretty easy.

 

I use godaddy and they automatically create this file for you. I"m sure your host does something similar, so call them up and find out.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.