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.');
}
?>
Opciones de contexto para FTP
Opciones de contexto para FTP — Listado de opciones de contexto para FTP
Descripción
Opciones de contexto para transportes ftp:// y ftps://
Historial de cambios
| Versión | Descripción |
|---|---|
| 5.1.0 |
Se añadió proxy.
|
| 5.0.0 |
Se añadió overwrite y resume_pos.
|
Notas
Nota: Opciones subyacentes del contexto del flujo del socket
Opciones adicionales de contexto pueden se soportadas por el transporte subyacente Para flujos ftp://, remitirse a las opciones de contexto para el transporte tcp://. Para flujos ftps://, remitirse a las opciones de contexto para el transporte ssl://.
php dot net at misterchucker dot com ¶
4 years ago
