PHP 7.4.25 Released!

FTP 上下文选项

FTP 上下文选项FTP 上下文选项列表

说明

ftp://ftps:// 传输的上下文选项。

可选项

overwrite bool

允许覆盖远程服务器上已有的文件。仅适用于写入模式(上传)。

默认为 false

resume_pos int

开始传输的文件偏移量。仅适用于读取模式(下载)。

默认为 0 (文件开头)。

proxy string

通过 http 代理服务器代理 FTP 请求。 仅适用于文件读取操作。 例如:tcp://squid.example.com:8000

注释

注意: 底层套接字流上下文选项
底层传输 可能支持其他上下文选项 对于 ftp:// 流,请参阅 tcp:// 传输的上下文选项。 对于 ftps:// 流,请参阅 ssl:// 传输的上下文选项。

add a note add a note

User Contributed Notes 3 notes

up
14
php dot net at misterchucker dot com
12 years ago
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.');
}
?>
up
0
instatiendaweb at gmail dot com
7 months ago
// Ruta del ftp debe coincidir con la ruta del localhost
$ftp_path = 'ftp://francisco:aaa@localhost:21/ftparchivo.txt';
// Escribimos las opciones de stream
$stream_options = array('ftp' => array('overwrite' => TRUE));
// Creamos un contexto
$stream_context = stream_context_create($stream_options);
\escribir::verifacionnota($stream_context, 'STREAMCONTEXT');
// Abrimos el fichero que vamos a leer
$flujo = fopen($ftp_path, 'r', false, $stream_context);
//Funcion creada :vardump + string
\escribir::verifacionnota($flujo, 'VARDUMP FOPEN');

$contenido = '';
//Traduccion mientras el contenido del archivo no este al final del fichero ==> feof
while (!feof($flujo)) {
//Entonces lee 8192 bytes del archivo y añadelos a la variable contenido
$contenido .= fread($flujo, 8192);
}
\escribir::verifacionnota($contenido, 'VARDUMP CONTENIDO');
//Cerramos el flujo
fclose($flujo);
up
-2
dev at codesatori dot com
4 years ago
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.
To Top