2009-10-05 161 views
0

我必须发送文件到API。 API documentation提供了一个如何通过文件上传脚本($ _FILES)执行此操作的示例。但是,我在同一台机器上有这些文件,所以想要跳过这一步,直接在文件中读取以节省时间。

我该如何读取文件(视频),以便它能够使用此代码段?我知道FILES是一个数组,但我可以单独设置它的其他部分(文件名),我真的需要它的数据部分以相同的格式读取以处理此代码(do我用的fread?文件中获得的内容?)

<?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); 

?>

在此先感谢您的帮助!

回答

2

呃 - 你需要做的只是提供位置,而不是文件句柄或任何东西,不是吗?

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

或者更简单地说

$id = $bc->createVideo('/foo/bar/baz.txt', $metaData); 
+0

那岂不是更高效的(和更清洁的阅读)简单地使用'$ _FILES [“视频”]'和'$ _FILES [“名”]',而不是将它们分配给变量然后打电话给他们? – EvilChookie 2009-10-05 02:20:31

+0

根据他的代码,除非我弄错了,'name'是$ _FILES ['video']中的一个关键字。 – 2009-10-05 02:23:48

+0

是的。 '$ _FILES ['video'] ['name'] == $ filename'。 – mauris 2009-10-05 02:40:30

2

看起来你并不需要在本地磁盘上的文件去做,除了点什么。怎么样:

<?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);