php教程

FTP context options

FTP context optionsFTP context option listing

说明

Context options for ftp:// and ftps:// transports.

可选项

overwrite boolean

Allow overwriting of already existing files on remote server. Applies to write mode (uploading) only.

Defaults to FALSE.

resume_pos integer

File offset at which to begin transfer. Applies to read mode (downloading) only.

Defaults to 0 (Beginning of File).

proxy string

Proxy FTP request via http proxy server. Applies to file read operations only. Ex: tcp://squid.example.com:8000.

更新日志

版本 说明
5.1.0 Added proxy.

注释

Note: Underlying socket stream context options
Additional context options may be supported by the underlying transport For ftp:// streams, refer to context options for the tcp:// transport. For ftps:// streams, refer to context options for the ssl:// transport.

User Contributed Notes

dev at codesatori dot com 24-Nov-2016 07:01
Apparently the overwrite-option only registers as false if it's absent. If I specify it as false, it acts the same as true and overwrites. Then,
<?php
if (empty($opts['overwrite'])) {
    unset(
$opts['overwrite']);
}
?>
...was the simple fix for my FTP stream wrapper class sporting default options. This is quite unexpected though; false means false even if I say so? Apparently PHP (7.0.2) just checks if the option exists, not its boolean value.
php dot net at misterchucker dot com 15-Apr-2009 06:19
This is an example of how to allow fopen() to overwrite a file on an FTP site. If the stream context is not modified, an error will occur: "...failed to open stream: Remote file already exists and overwrite context option not specified...".

<?php
// The path to the FTP file, including login arguments
$ftp_path = 'ftp://username:password@example.com/example.txt';

// Allows overwriting of existing files on the remote FTP server
$stream_options = array('ftp' => array('overwrite' => true));

// Creates a stream context resource with the defined options
$stream_context = stream_context_create($stream_options);

// Opens the file for writing and truncates it to zero length
if ($fh = fopen($ftp_path, 'w', 0, $stream_context))
{
   
// Writes contents to the file
   
fputs($fh, 'example contents');
   
   
// Closes the file handle
   
fclose($fh);
}
else
{
    die(
'Could not open file.');
}
?>

CopyRight © 2008-2022 verySource.Com All Rights reserved. 京ICP备17048824号-1 京公网安备:11010502034788