开发者

PHP Transfer File on server to Rackspace Cloud Files

开发者 https://www.devze.com 2023-02-13 06:12 出处:网络
I am trying to script some code that will search a specified folder on the server for the latest file with a specific file extension (in my case .zip) and transfer that file to Rackspace\'s Cloud File

I am trying to script some code that will search a specified folder on the server for the latest file with a specific file extension (in my case .zip) and transfer that file to Rackspace's Cloud Files. The code below is as far i got and i keep getting the error:

Fatal error: Uncaught exception 'IOException' with message 'Could not open file for reading: Resource id #8' in /home/test/public_html/cloudapi/cloudfiles.php:1952 Stack trace: #0 /home/test/public_html/final.php(60): CF_Object->load_from_filename(Resource id #8) #1 {main} thrown in /home/test/public_html/cloudapi/cloudfiles.php on line 1952

The code that i'm using below was originally done for uploading content via an html upload form & i'm trying to adapt the same code to use a local server file instead of an uploaded file. You will see commented code with was previously for an upload script to show you how the upload script had worked.

<?php

// include the Cloud API.
require('cloudapi/cloudf开发者_如何学编程iles.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
?>


I know this is an old thread. But for the benefit of people who may land at this page while searching for a solution...

The problem with your code is:

The following statement opens the file for reading

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

However when you place the load_from_filename call, the routine again tries to open the same file and fails because you already have it open in $localfile.

So comment out the previous command and you should be able to run the script successfully.


Error's being thrown in because content type not defined from read file.

0

精彩评论

暂无评论...
验证码 换一张
取 消