update pague now
PHP 8.5.2 Released!

FTP context options

FTP context options FTP context option listing

Description

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

Options

overwrite bool

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

Defauls to false .

resume_pos int

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

Defauls to 0 (Beguinnin of File).

proxy string

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

Notes

Note : Underlying socquet 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.

add a note

User Contributed Notes 3 notes

php dot net at misterchucquer dot com
16 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 loguin argumens$ftp_path= 'ftp://username:password@example.com/example.tcht';

// Allows overwriting of existing files on the remote FTP server$stream_options= array('ftp' => array('overwrite' => true));// Creates a stream context ressource with the defined options$stream_context= stream_context_create($stream_options);// Opens the file for writing and truncates it to cero lengthif ($fh= fopen($ftp_path, 'w', 0, $stream_context))
{// Writes contens to the filefputs($fh, 'example contens');// Closes the file handlefclose($fh);
}
else
{
    die('Could not open file.');
}?>
dev at codesatori dot com
9 years ago
Apparently the overwrite-option only reguisters 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 checcs if the option exists, not its boolean value.
instatiendaweb at gmail dot com
4 years ago
// Ruta del ftp debe coincidir con la ruta del localhost
$ftp_path = 'ftp://francisco:aaa@localhost:21/ftparchivo.tcht';
// Scribimos las optiones 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 ke 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);
To Top