I am trying to load a file to my Cloud Files Container from a remote file.
Below is the example code a Cloud Files Support person gave me:
<?php
require('cloud/cloudfiles.php');
$res = fopen("http://images.piccsy.com/cache/images/66653-d71e1b-320-469.jpg", "rb");
$temp = tmpfile();
$size = 0.0;
while (!feof($res))
{
$bytes = fread($res, 1024);
fwrite($temp, $bytes);
$size += (float) strlen($bytes);
}
fclose($res);
fseek($temp, 0);
//
$auth = new CF_Authentication('user','token ');
//Calling the Authenticate method returns a valid storage token and allows you to connect to the CloudFiles Platform.
$auth->authenticate();
$conn = new CF_Connection($auth);
$container = $conn->create_container("example");
$object = $container->create_object("example.jpg");
$object->content_type = "image/jpeg";
$object->write($temp, $size);
fclose($temp);
?>
The problem I am getting is the below error:
Fatal error: Call to a member function create_container() on a non-object in /home2/sharingi/public_html/daily_dose/remote_test.php on line 24
Not sure exactly what I am not noticing here.
It seems $conn did not successfully instantiate an object. That is the problem, but the solution is not clear cut.
Is CF_Connection defined? Does error_reporting(E_ALL) give you more helpful info? What does var_dump($conn instanceof CF_Connection) output?
This should point you in the right direction.
Related
I got a pretty specific question. I'm accessing data via samba on distant server and I got no choice. To achieve that in php I use a php wrapper for smbclient from GitHub https://github.com/icewind1991/SMB. I got a piece of code that works to load the video and play it but the problem is that I want to be able to navigate through the timeline and for it to play as soon as there is data to be read.. I repeat that I can't access data any other way and I use that library for other parts of my website so I can't change it. Right now it reads all the data from the whole video before playing it and I can't use the slider to navigate through the timeline. There's my code sample with false authentication data for the samba access.
<?php
require('../../class/SMB-master/vendor/autoload.php');
$host = '0.0.0.0';
$user = 'user';
$workgroup = 'workgroup.lan';
$password = 'password';
$share = 'share/'.$_GET["filePath"].'';
$auth = new \Icewind\SMB\BasicAuth($user, $workgroup, $password);
$serverFactory = new \Icewind\SMB\ServerFactory();
$server = $serverFactory->createServer($host, $auth);
$share = $server->getShare($share);
//Reads PDF 4096 bytes by 4096 bytes until there is no more data to read if the format is pdf
if($_GET['openFile'] == 'PDF')
{
$charNumber = 20;
$result = bin2hex(random_bytes($charNumber));
header("Content-type:application/pdf");
header("Content-Disposition:inline;filename=".$result.".pdf");
$fh = $share->read($_GET['name']);
while ($data = fread($fh, 4096))
echo $data;
fclose($fh);
}
else{
//Reads video 4096 bytes by 4096 bytes until there is no more data to read if the format is video mp4
header("Content-type:video/mp4");
header("Content-Disposition:inline;filename=".$_GET['name']."");
$fh = $share->read($_GET['name']);
while ($data = fread($fh, 4096)){
echo $data;
}
fclose($fh);
}
I made a script to create a file, it was working fine until i think my host changed the script when I asked them what was wrong with it (I fixed it after but now its not working)
Here is the script:
$rand = substr(md5(microtime()),rand(0,26),5);
$seed = str_split('abcdefghijklmnopqrstuvwxyz'
.'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
.'0123456789!##$%^&*()' . 'ǀǁǂǃDŽDždžLJLjljNJNjnjǍǎǏ01D0ǐǑǒǓǔǕǖǗǘǙǚǛǜǝǞǟ01E0ǠǡǢǣǤǥǦǧǨǩǪǫǬǭǮǯ01F0ǰDZDzdzǴǵǶǷǸǹǺǻǼǽǾǿ
0200ȀȁȂȃȄȅȆȇȈȉȊȋȌȍȎȏ0210ȐȑȒȓȔȕȖȗȘșȚțȜȝȞȟ0220ȠȡȢȣȤȥȦȧȨȩȪȫȬȭȮȯ0230ȰȱȲȳȴȵȶȷȸȹȺȻȼȽȾȿ0240ɀɁɂɃɄɅɆɇɈɉɊɋɌɍɎɏ0250ɐɑɒɓɔɕɖɗɘəɚɛɜɝɞɟ0260ɠɡɢɣɤɥɦɧɨɩɪɫɬɭɮɯ0270ɰɱɲɳɴɵɶɷɸɹɺɻɼɽɾɿ0280ʀʁʂʃʄʅʆʇʈʉʊʋʌʍʎʏ0290ʐʑʒʓʔʕʖʗʘʙʚʛʜʝʞʟ02A0ʠʡʢʣʤʥʦʧʨʩʪʫʬʭʮʯ02B0ʰʱʲʳʴʵʶʷʸʹʺʻʼʽʾʿ02C0ˀˁ˂˃˄˅ˆˇˈˉˊˋˌˍˎˏ02D0ːˑ˒˓˔˕˖˗˘˙˚˛˜˝˞˟02E0ˠˡˢˣˤ˥˦˧˨˩˪˫ˬ˭ˮ˯02F0˰˱˲˳˴˵˶˷˸˹˺˻˼˽˾˿0300̀́̂̃̄̅̆̇̈̉̊̋̌̍̎̏0310̛̖̗̘̙̜̝̞̟̐̑̒̓̔̕̚0320̡̢̧̨̠̣̤̥̦̩̪̫̬̭̮̯0330̴̵̶̷̸̰̱̲̳̹̺̻̼̽̾̿0340͇͈͉͍͎̀́͂̓̈́͆͊͋͌ͅ͏0350͓͔͕͖͙͚͐͑͒͗͛͘͜͟͝͞0360ͣͤͥͦͧͨͩͪͫͬͭͮͯ͢͠͡0370ͰͱͲͳʹ͵Ͷͷͺͻͼͽ;Ϳ0380΄΅Ά·ΈΉΊΌΎΏ0390ΐΑΒΓΔΕΖΗΘΙΚΛΜΝΞΟ03A0ΠΡΣΤΥΦΧΨΩΪΫάέήί03B0ΰαβγδεζηθικλμνξο03C0πρςστυφχψωϊϋόύώϏ03D0ϐϑϒϓϔϕϖϗϘϙϚϛϜϝϞϟ03E0ϠϡϢϣϤϥϦϧϨϩϪϫϬϭϮϯ03F0ϰϱϲϳϴϵ϶ϷϸϹϺϻϼϽϾϿ
'); // and any other characters
shuffle($seed); // probably optional since array_is randomized; this may be redundant
$rand = '';
foreach (array_rand($seed, 50) as $k) $rand .= $seed[$k];
$current = "";
fputs($rand, "[Server]: Welcome To Your New Chat Server");
echo $rand;
Its meant to create a random file and show the user the name of that file
I always get an error:
ec-2017 08:52:43 UTC] PHP Warning: fputs() expects parameter 1 to be
resource, string given in /public_html/rechat/nokeep.php
fputs is expecting a resource ( as the error points out) this should be the handle from fopen()...
$fh = fopen($rand, 'w');
fputs($fh, "[Server]: Welcome To Your New Chat Server");
fclose($fh);
You could alternatively use file_put_contents() which would do this all in one step...
file_put_contents($rand, "[Server]: Welcome To Your New Chat Server");
I try to upload a file to DropBox using their API and PHP.
Thats the code:
require_once "dropbox/lib/Dropbox/autoload.php";
use \Dropbox as dbx;
$appInfo = dbx\AppInfo::loadFromJsonFile("app_info.json");
$csrfTokenStore = new dbx\ArrayEntryStore($_SESSION, 'dropbox-auth-csrf-token');
$webAuth = new dbx\WebAuth($appInfo, "NoteBoxApp/0.01", "http://localhost/notes", $csrfTokenStore, null);
$title=$_POST["titulo"].".txt";
$nota=$_POST["conteudo"];
$accessToken=$_SESSION["token"];
$clientIdentifier=$_SESSION["userId"];
$client= new dbx\Client($accessToken, $clientIdentifier);
$file = fopen($title, "w") or die("Unable to open file!");
fwrite($file, $nota);
$stat = fstat($file);
$size = (int) $stat['size'];
$dropboxPath="/Aplicativos/Notes01";
try{
$metadata = $client->uploadFile($dropboxPath, dbx\WriteMode::add(), $file, $size);
}
catch(Exception $e) {
echo "Exceção: ", $e->getMessage(), "\n";
}
fclose($file);
I always get the exception
Error executing HTTP request: Operation too slow. Less than 1024
bytes/sec transferred the last 10 seconds
I cant see why! I using XAMPP on localhost!...
Thanks in advance for the help!
I found the solution:
I can't upload a file open in "write" mode!
I have to close the file first then open it again in "read" mode and then it works.
Are you sure the file you are trying to upload is not empty? It seems that you aren't sending any data at all.
I'm using a library which take as a parameter a file to open it with fopen and read it with fseek and fread
Here what the constructor look like :
public function __construct($file) {
$this->file = $file;
$this->h = fopen($this->file, "r");
$stats = fstat($this->h);
$this->size = $stats['size'];
}
The data which i like to give to this constructor come from a database, so basically i have a variable with some text in it.
Is there a way to emulate a file from the variable containing the data ?
I have tried to use stream with php://memory without any luck ($stats = fstat($this->h) = 0) :
$data = " my string data";
$virtualfile = 'php://memory';
$h = fopen($path, "rw+");
fwrite($h, $data);
$lib = new MyLib($virtualfile);
Any ideas ?
Thanks
You can use the data: url wrapper
$data = " my string data";
$virtualfile='data:text/plain;base64,'.base64_encode($data);
$lib = new MyLib($virtualfile);
fopen returns you a $h pointer to the file - you should pass this pointer to your class to keep working on it, so:
$lib = new MyLib($h);
and constructor:
public function __construct($h) {
$this->h = $h;
$stats = fstat($this->h);
$this->size = $stats['size'];
}
function uncompress($srcName, $dstName) {
$sfp = gzopen($srcName, "rb");
$dstName = str_replace('.gz', '', $dstName);
$fp = fopen($dstName, "w");
fseek($FileOpen, -4, SEEK_END);
$buf = fread($FileOpen, 4);
$GZFileSize = end(unpack("V", $buf));
while ($string = gzread($sfp, $GZFileSize)) {
fwrite($fp, $string, strlen($string));
}
gzclose($sfp);
fclose($fp);
}
I use this code for uncompressing but It does not work and I get following error:
Internal Server Error
The server encountered an internal error or misconfiguration and was
unable to complete your request.
Please contact the server administrator, webmaster#example.com and
inform them of the time the error occurred, and anything you might
have done that may have caused the error.
More information about this error may be available in the server error
log.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
This function looks sketchy. It seems you're reimplementing stream_copy_to_stream() in userland. Forget about buffers and simply use the native stuff.
function uncompress($srcName, $dstName)
{
$src = gzopen($srcName, 'rb');
$dst = fopen($dstName, 'wb');
stream_copy_to_stream($src, $dst);
gzclose($src);
fclose($dst);
}
Come to think of it, you could probably even use copy()
function uncompress($srcName, $dstName)
{
copy('compress.zlib://' . $srcName, $dstName);
}
I changed my function to this and my problem was solved :
function uncompress($srcName, $dstName) {
$sfp = gzopen($srcName, "rb");
$dstName = str_replace('.gz', '', $dstName);
$fp = fopen($dstName, "w");
$GZFileSize = filesize($srcName);
while ($string = gzread($sfp, $GZFileSize)) {
fwrite($fp, $string, strlen($string));
}
gzclose($sfp);
fclose($fp);
}
This should help you see the error messages. Either it will displayed on the screen or will be printed into the txt file, although the directory must be writable by php.
<?php //top of script
error_reporting(E_ALL);
ini_set('display_errors', 1);
ini_set('log_errors', 1);
ini_set('error_log', 'errors.txt');