tags:

views:

79

answers:

2

Hi,

I have to send a file to an API. The API documentation provides an example of how to do this through a file upload script ($_FILES). But, I have the files on the same machine, so would like to skip this step and just read in the file directly to save time.

How can I read in the file (a video) so that it will work with this code snippet?

I understand that FILES is an array, but I could set the other parts of it (the filename) seperately, I just really need the data part of it to be read in the same format to work with this code (do I use fread? file get contents?)

<?php

# Include & Instantiate
require('../echove.php');
$bc = new Echove(
    'z9Jp-c3-KhWc4fqNf1JWz6SkLDlbO0m8UAwOjDBUSt0.',
    'z9Jp-c3-KhWdkasdf74kaisaDaIK7239skaoKWUAwOjDBUSt0..'
);

# Create new metadata
$metaData = array(
    'name' => $_POST['title'],
    'shortDescription' => $_POST['shortDescription']
);

# Rename the video file
$file = $_FILES['video'];
rename($file['tmp_name'], '/tmp/' . $file['name']);
$file_location = '/tmp/' . $file['name'];

# Send video to Brightcove
$id = $bc->createVideo($file_location, $metaData);

?>

Thanks in advance for any help!

+2  A: 

Er - all you'd need to do is feed the location, not a file handle or anything, no?

$files = $_FILES['video'];
$filename = $files['name'];
$file_location = '/home/username/' . $filename; // maybe append the extension
$id = $bc->createVideo($file_location, $metaData);

Or more simply

$id = $bc->createVideo( '/foo/bar/baz.txt', $metaData );
meder
Wouldn't it be more efficient (and cleaner to read) to simply use `$_FILES['video']` and `$_FILES['name']` rather than assign them to variables and then call them?
EvilChookie
According to his code, `name` is a key in $_FILES['video'] unless I'm mistaken.
meder
yep. `$_FILES['video']['name'] == $filename`.
thephpdeveloper
Yes, but Meder's "more simply" is what I think you need. If the file is already on the server, you just need to, one way or another, get the pathname of the file, and just use that. No $_FILES at all, since the user isn't acutally uploading anything to your server.
timdev
thanks! works great!
stunnaman
+2  A: 

Looks like you don't need to do anything except point at the file on your local disk. What about:

<?php

# Include & Instantiate
require('../echove.php');
$bc = new Echove(
    'z9Jp-c3-KhWc4fqNf1JWz6SkLDlbO0m8UAwOjDBUSt0.',
    'z9Jp-c3-KhWdkasdf74kaisaDaIK7239skaoKWUAwOjDBUSt0..'
);

# Create new metadata
$metaData = array(
    'name' => $_POST['title'],
    'shortDescription' => $_POST['shortDescription']
);

# point at some file already on disk
$file_location = '/path/to/my/file.dat'; // <== if the file is already on the box, just set $file_location with the pathname and bob's yer uncle 

# Send video to Brightcove
$id = $bc->createVideo($file_location, $metaData);
timdev