public static int file_put_contents(Context ctx, string path, PhpValue data, WriteContentsOptions flags = WriteContentsOptions.Empty, PhpResource context = null) { StreamContext sc = StreamContext.GetValid(context, true); if (sc == null) { return(-1); } string mode = (flags & WriteContentsOptions.AppendContents) > 0 ? "ab" : "wb"; using (PhpStream to = PhpStream.Open(ctx, path, mode, ProcessOptions(ctx, (FileOpenOptions)flags), sc)) { if (to == null) { return(-1); } // passing array is equivalent to file_put_contents($filename, join('', $array)) var array = data.ArrayOrNull(); if (array != null) { int total = 0; var enumerator = array.GetFastEnumerator(); while (enumerator.MoveNext()) { int written = to.WriteBytes(enumerator.CurrentValue.ToBytes(ctx)); if (written == -1) { return(total); } total += written; } return(total); } // as of PHP 5.1.0, you may also pass a stream resource to the data parameter var resource = data.AsResource(); if (resource != null) { PhpStream from = PhpStream.GetValid(resource); if (from == null) { return(-1); } return(PhpStreams.stream_copy_to_stream(from, to)); } return(to.WriteBytes(data.ToBytes(ctx))); } }
/// <summary> /// Copies a file (even accross different stream wrappers). /// </summary> /// <remarks> /// If the destination file already exists, it will be overwritten. /// <para> /// Note: As of PHP 4.3.0, both source and dest may be URLs if the /// "fopen wrappers" have been enabled. See <c>fopen()</c> for more details. /// If dest is an URL, the copy operation may fail if the wrapper does /// not support overwriting of existing files. /// </para> /// </remarks> /// <param name="ctx">Runtime context.</param> /// <param name="source">Source URL.</param> /// <param name="dest">Destination URL.</param> /// <returns><c>true</c> on success or <c>false</c> on failure.</returns> public static bool copy(Context ctx, string source, string dest) { StreamWrapper reader, writer; if ((!PhpStream.ResolvePath(ctx, ref source, out reader, CheckAccessMode.FileExists, CheckAccessOptions.Empty)) || (!PhpStream.ResolvePath(ctx, ref dest, out writer, CheckAccessMode.FileExists, CheckAccessOptions.Empty))) { return(false); } if ((reader.Scheme == "file") && (writer.Scheme == "file")) { // Copy the file. try { File.Copy(source, dest, true); return(true); } catch (System.Exception) { return(false); } } else { // Copy the two files using the appropriate stream wrappers. using (PhpResource from = reader.Open(ctx, ref source, "rb", StreamOpenOptions.Empty, StreamContext.Default)) { if (from == null) { return(false); } using (PhpResource to = writer.Open(ctx, ref dest, "wb", StreamOpenOptions.Empty, StreamContext.Default)) { if (to == null) { return(false); } int copied = PhpStreams.stream_copy_to_stream(from, to); return(copied >= 0); } } } }
/// <summary> /// Reads a file, decompresses it and writes it to standard output. /// </summary> /// <param name="ctx">Runtime context.</param> /// <param name="filename"> /// The file name. This file will be opened from the filesystem and its contents written to standard output. /// </param> /// <param name="use_include_path"> /// You can set this optional parameter to 1, if you want to search for the file in the include_path too. /// </param> /// <returns> /// Returns the number of (uncompressed) bytes read from the file. If an error occurs, FALSE is returned and /// unless the function was called as @readgzfile, an error message is printed. /// </returns> public static int readgzfile(Context ctx, string filename, int use_include_path = 0) { var fs = (PhpStream)gzopen(ctx, filename, "r", use_include_path); return(PhpStreams.stream_copy_to_stream(fs, InputOutputStreamWrapper.ScriptOutput(ctx))); }