Example #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)));
            }
        }
Example #2
0
		public static int WriteContents(string path, object data, WriteContentsOptions flags, PhpResource context)
		{
            StreamContext sc = StreamContext.GetValid(context, true);
			if (sc == null) return -1;

			string mode = (flags & WriteContentsOptions.AppendContents) > 0 ? "ab" : "wb";
			using (PhpStream to = PhpStream.Open(path, mode, ProcessOptions((FileOpenOptions)flags), sc))
			{
				if (to == null) return -1;

				// passing array is equivalent to file_put_contents($filename, join('', $array))
				PhpArray array = data as PhpArray;
				if (array != null)
				{
					int total = 0;

					foreach (object o in array.Values)
					{
						int written = to.WriteBytes(Core.Convert.ObjectToPhpBytes(o));
						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
				PhpResource resource = data as PhpResource;
				if (resource != null)
				{
					PhpStream from = PhpStream.GetValid(resource);
					if (from == null) return -1;

					return PhpStreams.Copy(from, to);
				}

				return to.WriteBytes(Core.Convert.ObjectToPhpBytes(data));
			}
		}
Example #3
0
		public static int WriteContents(string path, object data, WriteContentsOptions flags)
		{
			return WriteContents(path, data, flags, StreamContext.Default);
		}