I have gotten a php file where users can create their own user ids and login and out with them and it saves their name in mysql, now i want these users to be able to upload small videos, can someone please tell me a php code for how a user can log in to their account and upload a video and next time they log in they can see what videos they uploaded and can view other peoples videos.

Recommended Answers

All 6 Replies

Member Avatar for diafol

DO you want to upload videos themselves or just upload links (e.g. youtube/vimeo etc) to videos. Hosting your own videos can be very expensive wrt. bandwidth. You may consider getting a media server.

Anyway, if you're certain you need to upload physical video files, just have a form with a file field. The file gets loaded to a 'videos' directory. I suggest giving it a unique name on the server, e.g. username_unix_timestamp. This name is then entered into a DB table, e.g.

video_id
video_filename
video_title
video_description
user_id
upload_timestamp
tags

WHen the user enters a page you list his/her videos via simple SELECT query. You can use any technique to play the video. Flowplayer, html5 video etc.

I have an account through go daddy and they say i have unlimeted bandwidth so I dont think thats a problem, but i see what your saying and it makes sense but I have no idea how to type that into code. The reason I got the user id login form is from a website which gives it away for free. Do you think you would be able to write me a small code so this is possible for me?

Member Avatar for diafol

Do you think you would be able to write me a small code so this is possible for me?

Erm, that's not really the point of these forums. Have a go at some code and we'll lend a hand. What you're looking for to start is a standard upload form and INSERT MySQL query.

Okay, heres my basic upload html form.

<html>
<body>

<form action="upload_file.php" method="post"
enctype="multipart/form-data">
<label for="file">Filename:</label>
<input type="file" name="file" id="file" /> 
<br />
<input type="submit" name="submit" value="Submit" />
</form>

</body>
</html>

Heres the php script which uploads the files.

<?php
if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br />";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br />";
  echo "Type: " . $_FILES["file"]["type"] . "<br />";
  echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
  echo "Stored in: " . $_FILES["file"]["tmp_name"];
  }
?>

This is how it saves the file. It saves it to a file called upload, but how do i get it to save to mysql and linked with a user id. This example is with images.

<?php
$allowedExts = array("jpg", "jpeg", "gif", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 20000)
&& in_array($extension, $allowedExts))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
    echo "Upload: " . $_FILES["file"]["name"] . "<br />";
    echo "Type: " . $_FILES["file"]["type"] . "<br />";
    echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br />";
    echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br />";

    if (file_exists("upload/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
    }
  }
else
  {
  echo "Invalid file";
  }
?>
Member Avatar for diafol

Seems OK, only given a cursory look though.

You can use the image example for video files.

Now you need to insert some info into the DB. I'm using mysql, but you should consider using mysqli or PDO.

//include db connection details here first
mysql_query("INSERT INTO videos SET filepath = '$filepath', ...(fill in other fields)...)";
if(mysql_affected_rows()>0){
    echo "Stored OK";
}else{
    echo "Problem with storing";
}

The other fields could include the title, description, video length, video type, uploader username, date uploaded, etc.

You now have to decide on how you're going to serve the videos. You can use:

video.js: http://videojs.com/
flowplayer: http://flowplayer.org/
jw/longtail: http://www.longtailvideo.com/players
jcplayer: http://www.jcplayer.com/
flvplayer: http://flv-player.net/

I could go on. It depends which type of files you need to play. FLV, MP4, AVI (some are just wrappers - so they may not work as expected).

This may help:
http://camendesign.com/code/video_for_everybody

This is not my area, so - any media gurus out there - if I'm wrong, please point out my mistakes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.