2

我正在尝试编写一些代码,这些代码将在服务器上的指定文件夹中搜索具有特定文件扩展名(在我的情况下为 .zip)的最新文件,并将该文件传输到 Rackspace 的云文件。下面的代码是我得到的,我不断收到错误:

致命错误:在 /home/test/public_html/cloudapi/cloudfiles.php:1952 中,未捕获的异常“IOException”带有消息“无法打开文件进行读取:资源 ID #8”堆栈跟踪:#0 /home/test/public_html/ final.php(60): CF_Object->load_from_filename(Resource id #8) #1 {main} 在第 1952 行的 /home/test/public_html/cloudapi/cloudfiles.php 中抛出

我在下面使用的代码最初是为通过 html 上传表单上传内容而完成的,我正在尝试调整相同的代码以使用本地服务器文件而不是上传的文件。您将看到以前用于上传脚本的注释代码,以向您展示上传脚本的工作方式。

<?php

// include the Cloud API.
require('cloudapi/cloudfiles.php');


// START - Script to find recent file with the extension .zip in current folder
$show = 2; // Leave as 0 for all
$dir = ''; // Leave as blank for current

if($dir) chdir($dir);
$files = glob( '*.zip');
usort( $files, 'filemtime_compare' );

function filemtime_compare( $a, $b )
{
return filemtime( $b ) - filemtime( $a );
}

$i = 0;
foreach ( $files as $file )
{
++$i;
if ( $i == $show ) break;
$value = $file;  //Variable $value contains the filename of the recent file to be used in Cloud Files API
}
// END - Script to find recent file with the extension .zip in current folder



// START - Rackspace API code to upload content to cloud files container
// Rackspace Connection Details;
$username = "randomusername"; // put username here
$key = "234887347r93289f28h3ru283h2fuh23093402398"; // api key

// Connect to Rackspace
$auth = new CF_Authentication($username, $key);

$auth->authenticate();
$conn = new CF_Connection($auth);

//Set the Container you want to use
$container = $conn->get_container('Backups');

//Temp store the file
//$localfile = $_FILES['uploadfile']['tmp_name'];
//$filename = $_FILES['uploadfile']['name'];
$localfile = fopen($value, "r");
$filename = $value;


//Uploading to Rackspace Cloud
$object = $container->create_object($filename);
$object->load_from_filename($localfile);

echo "Your file has been uploaded";

// END - Rackspace API code to upload content to cloud files container
?>
4

2 回答 2

2

我知道这是一个旧线程。但是为了那些在寻找解决方案时可能会登陆此页面的人的利益......

您的代码的问题是:

以下语句打开文件进行读取

$localfile = fopen($value, "r");

但是,当您调用 load_from_filename 时,例程会再次尝试打开同一个文件并失败,因为您已经在 $localfile 中打开了它。

所以注释掉前面的命令,应该就能成功运行脚本了。

于 2011-05-31T10:07:54.627 回答
0

由于未从读取文件中定义内容类型,因此引发错误。

于 2011-03-10T07:35:59.970 回答