ftp_get() expects parameter 2 to be a valid path, resource given - php

Why am I receiving this error? According to the ftp_get docs, the function is defined like so:
bool ftp_fget ( resource $ftp_stream , resource $handle , string $remote_file , int $mode [, int $resumepos = 0 ] )
However, when I pass this function a file resource, I receive the error:
ftp_get() expects parameter 2 to be a valid path, resource given
Here is my code:
$ftp = ftp_connect(FTP_HOST);
if (ftp_login($ftp, FTP_USERNAME, FTP_PASSWORD)) {
$file = tmpfile();
if(ftp_get($ftp, $file, FTP_FILENAME, FTP_ASCII)) {
file_get_contents($file);
}
}
Why is the error suggesting that the function expects a path, when the docs show it defined as expecting a resource? I am using PHP 5.5.9.

Because you use the doc of ftp_fget instead of ftp_get:
http://php.net/manual/en/function.ftp-get.php
bool ftp_get ( resource $ftp_stream , string $local_file , string $remote_file , int $mode [, int $resumepos = 0 ] )

Related

How to pass options to stream_open in google cloud PHP stream wrapper

I'm trying to serve large audio files from google cloud storage with seeking support.
I have difficulties understanding php fopen and google stream wrapper working together.
When fopen is called it immediately calls stream_open from google StreamWrapper class.
However im unable to pass options to it through fopen context. I would like to set bitwise option 0b10000 as its STREAM_MUST_SEEK option. $flags parameter is always 0.
https://www.php.net/manual/en/streamwrapper.stream-open
Documentation shows there are atleast two options you can set, but it doesnt tell where you can set them.
Without $flag set to 0b10000 im getting:
PHP Warning: stream_copy_to_stream(): Failed to seek to position 85721088 in the stream in /home/project/src/Classes/StreamResponse.php on line 296
If i set $flags to 0b10000 it works and supports seeking.
$opts = array(
'gs' => array('key' => 'value')
);
$context = stream_context_create($opts);
$out = fopen('php://output', 'wb');
$file = fopen($this->file->getPathname(), 'rb', false, $context);
stream_copy_to_stream($file, $out, $this->maxlen, $this->offset);
fclose($out);
fclose($file);
/**
* Callback handler for when a stream is opened. For reads, we need to
* download the file to see if it can be opened.
*
* #param string $path The path of the resource to open
* #param string $mode The fopen mode. Currently only supports ('r', 'rb', 'rt', 'w', 'wb', 'wt')
* #param int $flags Bitwise options STREAM_USE_PATH|STREAM_REPORT_ERRORS|STREAM_MUST_SEEK
* #param string $openedPath Will be set to the path on success if STREAM_USE_PATH option is set
* #return bool
*/
public function stream_open($path, $mode, $flags, &$openedPath)
{
$client = $this->openPath($path);
// strip off 'b' or 't' from the mode
$mode = rtrim($mode, 'bt');
$options = [];
if ($this->context) {
$contextOptions = stream_context_get_options($this->context);
if (array_key_exists($this->protocol, $contextOptions)) {
$options = $contextOptions[$this->protocol] ?: [];
}
}
if ($mode == 'w') {
$this->stream = new WriteStream(null, $options);
$this->stream->setUploader(
$this->bucket->getStreamableUploader(
$this->stream,
$options + ['name' => $this->file]
)
);
} elseif ($mode == 'r') {
try {
// Lazy read from the source
$options['restOptions']['stream'] = true;
$this->stream = new ReadStream(
$this->bucket->object($this->file)->downloadAsStream($options)
);
// Wrap the response in a caching stream to make it seekable
if (!$this->stream->isSeekable() && ($flags & STREAM_MUST_SEEK)) {
$this->stream = new CachingStream($this->stream);
}
} catch (ServiceException $ex) {
return $this->returnError($ex->getMessage(), $flags);
}
} else {
return $this->returnError('Unknown stream_open mode.', $flags);
}
if ($flags & STREAM_USE_PATH) {
$openedPath = $path;
}
return true;
}

gzopen throwing exception for existing, valid file

I am trying to build a log parser using the zLib functions, and am running into a problem. This is my code:
$filename = '/Users/awallace/AccessLogs/access.log.6.gz';
$handle = gzopen( $filename, 'r');
while ( $buffer = gzgets( $handle, 2048 ) )
{
if ( strpos($buffer, "Leadbuilder.") !== false )
{
print $buffer . "\n";
}
gzclose($handle);
}
(I have removed error checking code). WhenI run this, I get a warning:
Warning: gzgets(): 5 is not a valid stream resource in /Users/awallace/test.php on line 22
If I dump out the handle after gzopen, I get: "Resource id #5". Any idea why this isn't working?
PHP v:5.5.29
MacOS 10.10.5
Ouput of "file" command:
/Users/awallace/AccessLogs/access.log.6.gz: gzip compressed data, from
Unix, last modified: Wed Feb 24 23:35:20 2016
Thanks..
You close the handle inside your loop, so on the second loop iteration $handle is invalid.
Instead do this:
$handle = gzopen( $filename, 'r');
while ( !gzeof($handle) )
{
$buffer = gzgets( $handle, 2048 );
if ( strpos($buffer, "Leadbuilder.") !== false )
{
print $buffer . "\n";
}
}
gzclose($handle);

fwrite() expects parameter 1 to be resource, string given in c://

I am using latest version of php.. and i stuck on this error
Warning: fwrite() expects parameter 1 to be resource, string given in c:\ this error shows me 6 times
Warning: fclose() expects parameter 1 to be resource, string given in // this error reapeat only one time.
I am trying to get last id of last line but i am facing that error.. here is my php code :
<?php
include_once('../../fucrenzione.php');
/*
$codeAlphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
$codeAlphabet.= "0123456789";*/
$filename ="data.txt" or die("Unable to open file!");
$line="";
fopen($filename, 'a+');
for($i=0;$i<=5;$i++ ){
$va=rand(1,20);
$re= rand(2,50);
$data = [
'val1' => $va,
'val2' => $re,
'body' => getToken(10),
'Id'=> $i,
'timestamp' => time()
];
/* echo "<pre>".*/$line = serialize($data);
$line .= "\n";
fwrite($filename, $line);
}
fclose($filename);
?>
I tried to use also fputs() but i still get that error.
The error tells you the issue. fopen() returns a resource:
$handle = fopen($filename, 'a+');
Then fwrite() expects the first argument to be that resource:
fwrite($handle, $line);
Also, I think the or die("Unable to open file!"); would be better on the fopen() line rather than the assignment line.

What do Null and true do for new SimpleXMLElement?

I've seen PHP code like this, but I've not found anything that explains it:
$xml = new SimpleXMLElement($url, null, true);
What do null and true do? Is there a way to check to see if the requested $url failed? Please provide an PHP example of the last two options being used. Thanks!
The signature of SimpleXMLElement::_construct is
final public SimpleXMLElement::__construct() ( string $data [, int
$options = 0 [, bool $data_is_url = false [, string $ns = "" [, bool
$is_prefix = false ]]]] )
The default value of third parameter is false, so if you want to set it to true, you will also need to provide the second parameter.
Example:
// Create a SimpleXMLElement object from a URL
$sxe = new SimpleXMLElement('http://example.org/document.xml', null, true);
echo $sxe->asXML();

What is Delphi equivalent to "fsockopen" function in php?

What is Delphi equivalent to "fsockopen" function in php?
in PHP Manual:
resource fsockopen ( string $hostname [, int $port = -1 [, int &$errno [, string &$errstr [, float $timeout = ini_get("default_socket_timeout") ]]]] )
Initiates a socket connection to the resource specified by hostname.
My code is fully:
function accountcreate($username, $password, $connection, $bandwidth, $disabledate, $disabletime)
{
$adminpassword='';
$adminport=82;
$proxyaddress='127.1.1.1';
$fp = fsockopen($proxyaddress, $adminport, &$errno, &$errstr, 1000);
if(!$fp)
{
echo "$errstr ($errno)<br>\n";
}
else
{
$url_ = "/account";
$url = "add=1"."&";
.
.
$url = $url."userid=-1";
$len = "Content-Length: ".strlen($url);
$auth = "Authorization: Basic ".base64_encode("admin:".$adminpassword);
$msg = "POST ".$url_." HTTP/1.0\r\nHost: ".$proxyaddress."\r\n".$auth."\r\n".$len."\r\n"."\r\n".$url;
fputs($fp,$msg);
echo $msg;
fclose($fp);
}
that code make a new account in ccproxy.
Perhaps have a look at the Indy Components that ship with Delphi (assuming at least Delphi7). You would need :
TIdTCPConnection Documentation
which contains an IOHandler property (TIdIOHandler) where you specify the parameters used in fsockopen:
TIdTCPConnection.IOHandler Property
TIdIOHandler Documentation
As others have noted, however, you probably would be well served by looking at the bigger picture in your PHP code and perhaps implementing its functionality with a higher level tool like TIdHTTP :
TIdHTTP Documentation

Categories