Esempio n. 1
0
        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)));
            }
        }
Esempio n. 2
0
            private void Callback(IAsyncResult ar)
            {
                if (access == StreamAccessOptions.Read)
                {
                    int count = stream.EndRead(ar);
                    if (count > 0)
                    {
                        if (count != buffer.Length)
                        {
                            // TODO: improve streams
                            var buf = new byte[count];
                            Buffer.BlockCopy(buffer, 0, buf, 0, count);
                            phpStream.WriteBytes(buf);
                        }
                        else
                        {
                            phpStream.WriteBytes(buffer);
                        }

                        stream.BeginRead(buffer, 0, buffer.Length, callback, ar.AsyncState);
                    }
                    else
                    {
                        phpStream.Flush();
                        stream.Dispose();
                    }
                }
                else
                {
                    buffer = phpStream.ReadBytes(BufferSize);
                    if (buffer != null)
                    {
                        stream.BeginWrite(buffer, 0, buffer.Length, callback, ar.AsyncState);
                    }
                    else
                    {
                        stream.EndWrite(ar);
                        stream.Dispose();
                    }
                }
            }