YouTube Uploader

This script no longer works. The correct way to do this now is to use YouTube’s API.

There are plenty of tutorials on how to use YouTube to get content. But after much searching I couldn’t find a way to upload videos to Youtube.

So with the help of a friend to get me started, and some diligent searching to fill in the missing pieces, I came up with this code.

It’s important to note that this is article is geared towards experienced programmers. If you are a typical user trying to upload your best bet is to use YouTube’s webpage. You will need to understand and modify some parts of this code in order to get it working in your environment.

Below is the main function that does the dirty work. We are using curl for our uploading. I’ve tried commenting the best I can.

The Posting Function



function http_post_form($url, $vars) {
  $ch = curl_init();
  $timeout = 0; // set to zero for no timeout
  curl_setopt($ch, CURLOPT_URL, $url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return to string instead of spewing to output
  curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);  // follow location header, not sure if this is needed.
  // I like chocolate chip
  curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
  curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");

  // Expect: 100-continue doesn't work properly with lightTPD
  // This fix by zorro
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:')); 

  // Debugging
  if(defined(DEBUG) && DEBUG == TRUE) {
    $mydebug = f-open('debug.txt','a');
    curl_setopt($ch, CURLOPT_STDERR, $mydebug);
    curl_setopt($ch, CURLOPT_VERBOSE, 1); 

  // Set method post
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);

  $file_contents = curl_e xec($ch);

  if(defined(DEBUG) && DEBUG == TRUE) {
    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    f write($mydebug,"/nHttp Code: $httpCode/n");


  if(defined(DEBUG) && DEBUG == TRUE) {
    f close($mydebug);

  return $file_contents;


That function essentially does the job of filling out a form and submitting it to the server for us. While there are a few lines of code to get it working with YouTube’s servers this function could certainly be useful for any automated posting means.

Logging In

The first step in our YouTube journey is logging in. $user and $password needs to be replaced with your YouTube login information.

$user = 'user';
$password = 'password';

/* don't touch these, we are just setting up our ugly strings with our information to pass onto our super function above. */
$param = 'current_form=loginForm&next=/my_videos_upload%3F&username='.$user.'&password='.$password.'&action_login=Log+In';
$url = '';

http_post_form($url, $param);

Posting Your Video

$title, $description and the filename of the video to be uploaded change every time. I insert this information using a simple form webpage. The other information stays the same for all of my ditties. You will need to adjust the variables for your situation.
$file is set to the path of the file to be uploaded to YouTube. (The file should already be uploaded to your server.)

/* Change me ! */
$title = stripslashes($_POST['title']);
$description = stripslashes($_POST['description']);
$keywords = 'insert your keywords here';
$file = '../uploads/' . $_POST['file'];
$privacy = 'private';
$category = '10';
$language = 'EN'

/* stop changing */
$post_vars = array();
$post_vars['field_privacy'] = $privacy;
$post_vars['field_myvideo_keywords'] = $keywords;
$post_vars['field_myvideo_title'] = $title;
$post_vars['field_myvideo_descr'] = $description;
$post_vars['field_myvideo_categories'] = $category;
$post_vars['language'] = $language;
$post_vars['field_uploadfile'] = "@$file";
$post_vars['field_command'] = 'myvideo_submit';
$post_vars['submit'] = 'Upload%20Video';

/* See note below about YouTube URL's */
$url = '';

$return = http_post_form($url, $post_vars);

Please note I’m fairly sure YouTube gives a random server number for the url listed above. As far as I know your safe using v1-v64. Please choose a number at random, come up with some nifty random function, or best yet figure out exactly what YouTube is up to and come up with the best solution.

For convenience YouTube’s current settings are copied below.

Autos & Vehicles
Film & Animation
Gadgets & Games
Howto & DIY
News & Politics
People & Blogs
Pets & Animals
Travel & Places







20 responses to “YouTube Uploader”

  1. Alix Axel Avatar

    Great work, way to go! ;)

    Congrats on your scripting!

  2. Fast Stumble Exchange Avatar

    Very nifty script, but normally you don’t want “private” do you?

  3. Llynix Avatar

    It can be changed easily to ‘public’. Your right though, generally there is no reason. Private just removes the shared code. The file can still be shared by us in the know in fact. When I first started out this endevour I knew nothing about YouTube and thought it would enable more protection.

    Thanks for adding another thing to my ever-growing to-do list. :)

  4. Orlando Avatar

    Hi, it’s possible to add a upload progress bar?

  5. Llynix Avatar

    Possible… sure..

    However PHP has some problems with file upload. I was actually planning to do a tutorial on it but ended up running in circles. Perl does it fairly well, but the source code that’s available is fairly rough.

    Remember with this script you’ve already uploaded the file to your server, so I’ve found that file transfer time is quite low even with large video files. I guess those who are unfortunate to have a decent webhost might run into problems, but it should be quite speedy.

  6. Tony Cai Avatar

    I’m new to this, I’ve been Googling around to find a Youtube like uploading script for my site and I ran into this. What is this exactly? Please give me the simplified version of what this does? I know a bit of PHP.

    Tony Cai

  7. Tony Cai Avatar

    Okay I understand it a little more now.

    So the PHP code above is the backend of what needs to be used with a template to get videos uploaded onto youtube without having to be on youtube. This is GENIUS!

    Does someone have an example of a template they can provide me?

    Thanks a lot!
    Tony Cai

  8. smaxor Avatar

    Hi great post I really enjoyed it. However I’m having occasionaly ( works about 50% ) issues when I try to upload. I’m get an error back that says the file is to large, please make it smaller. Unfortunately I wish it was to big that would be an easy solve. It uploads fine uploading the same video file with the browser. Do you have any ideas why this might be happening? The video is a standard 320×240, 30fps. I’m totally baffled on this one and it very sporadic.

  9. Llynix Avatar

    Wow, woke up this morning with comments and IM’s all about my little script here. I’ll try my best to help.


    The script is lacking a simple front end. I ended up creating a simple form page to fill in the needed information. Then, because my files were already uploaded to the server, it uses the information and the file to upload. Depending on how you’ll use the script you’ll want to modify the information you put in or the info that is hard coded. When I have time over this weekend I’ll see if I can’t revamp this article to include a simple form front end which people can work off of.


    Perhaps the occasional problem is the server you are uploading to? For my script I just picked a number and went with it, but I do vaguely recall having problems with the first number I randomly picked. Sporadic problems are so darn hard to debug. Feel free to send/post your code sample and I’ll be happy to take a look.

  10. Tony Cai Avatar


    Awesome man! I can’t wait! I will try to contact you via IM again.

    Thanks in advance!
    Tony Cai

  11. xqdqsd Avatar

    nice script, works fine. been adding some parser to the returned webpage to get the youtube link however (i think its what most people want)

  12. barakr Avatar

    does it work under PHP5 ?
    someone told me curl might have problems with cookiejar under PHP5…

    and i’m getting – ‘failed creating formpost data’ error.


  13. Mike Avatar

    Does anyone have just a downloadable solution??
    aka: very simple simple front end / a back end..

  14. brien Avatar

    hey, very nice. YouTube recently switched their server names to (e.g. XX=63, Everything else remains the same.

    We at added some return values to your script, so we could detect a bad login and the movie permalink …

  15. Andrew Avatar

    Hey this is awesome, especially appreciate Brien’s update. Thanks so much.

  16. RRC Avatar

    How can i know the video ID after uploading?

  17. Lucian Avatar


    I have tried this script but it does not upload any file on my youtube account. So I realy don’t know what the problem is but it doesn’t work

  18. xx Avatar

    it is an outdated script :) need to be modified

  19. IndiaGuy Avatar

    It’s not working now.
    Youtube has changes names of almost all hidden html fields.
    I’m working to create a new working script.

    I’ll post it once it’s successful.


  20. Liudvikas Avatar

    Use Zend Gdata and Youtube API.