In PHP, i will write (create) the file using file_put_contents($filename, $data);
It is ok, but i want to detect the finish event of process.
Currently, the page is showing loading status.
This is currently the way i can know it is finish or not.
I want to detect with the code.
Asked
Active
Viewed 1.7k times
7
-
probably duplicate.. maybe this link is useful http://stackoverflow.com/a/21842519/4519636 – Zakynthos Feb 18 '16 at 19:09
3 Answers
12
This is a blocking, I/O call, so it finishes when the function call returns. The return value is the number of bytes written (upon success).

AJ.
- 27,586
- 18
- 84
- 94
-
Ok now i see that 'file_put_contents()' returns the file size, at finish. Additionally, can i detect the size 'before' ?? I used: 'file_get_contents()'. If so, i can detect if the process is success writing the all bytes (or) not. – Alvin May 22 '11 at 01:07
-
1
-
It don't work.. I used
$file = file_get_contents($path)
and thenecho "Size to Write: ".filesize($file)
– Alvin May 22 '11 at 01:20 -
2Read the documentation for `filesize()`. It expects the path to a file, not the contents of a file. http://us3.php.net/filesize – AJ. May 22 '11 at 01:22
-
uh, i'm supposed to get from URLs (download) .. But "filesize()" don't work with URL(s).. ;( – Alvin May 22 '11 at 01:28
-
Sorry, your original question didn't mention anything about the file being a **remote** resource. Perhaps you want to revise your original question to make it more accurate what you are trying to do? – AJ. May 22 '11 at 01:32
-
Yes yes, i'm SORRY! Please (everyone!) kindly jump to my new thread http://stackoverflow.com/q/6085524/753726 – Alvin May 22 '11 at 01:45
-
-
1@Alvin: FYI for future reference, you can edit your original question; there's no need for a new one :) For code, use backtick marks (`) around the code. – Jonah May 22 '11 at 02:27
6
It puts everything on hold until it's over
So you could use something like this to determine when it has finished writing the file.
echo "The file's contents are now being written, please wait.";
file_put_contents($filename, $data);
echo "The file's contents have been written.";

Travis
- 137
- 2
- 4
- 9
3
Retrieve the Content-Length
header from the remote location first. You can use get_headers to do that. Like so:
$headers = get_headers($url, 1);
echo "To download: " . $headers['Content-Length'] . " bytes.<br />";

Ry-
- 218,210
- 55
- 464
- 476
-
Will this always work? Or is there cases were the web application or server refuses to give you just the headers? Also does the get_headers method send HEAD or GET? – rzetterberg May 22 '11 at 01:47
-
I don't think that the server can refuse to send headers for something. And from the page: "If anyone is curious, as I was, this function does not send a HEAD verb. Instead it sends a GET. Which in my case is not ideal because I need a quick way to get a HTTP status (200, 404, etc.) The problem with GET is that, for cases such as mine, I do not want all the overhead with the data that comes back." – Ry- May 22 '11 at 01:50
-
Alright, so that means it has to download the file anyway then when using get_headers since it's a GET? – rzetterberg May 22 '11 at 01:51
-
1@Ancide: No, it cancels after it gets the headers. (By "overhead", the person meant "the rest of the headers", I believe.) – Ry- May 22 '11 at 01:53
-
$headers['Content-Length'] is not always returns the "string". Sometime it comes with Array ( [0] => 0 [1] => 19342374 ). So, we have to handle it. I handle like: gettype($headers['Content-Length'])=="array" – Alvin May 22 '11 at 02:06
-
1@minitech I tested this out with a custom socket solution using HEAD instead of GET and it's just like you said, "overhead" == "the rest of the headers". Just wanted to let everyone know for sure :) – rzetterberg May 22 '11 at 02:13