How to detect the finish with file_put_contents() in php?

15,740

Solution 1

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).

Solution 2

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.";

Solution 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 />";
Share:
15,740
Alvin
Author by

Alvin

Updated on June 19, 2022

Comments

  • Alvin
    Alvin almost 2 years

    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.

  • Alvin
    Alvin almost 13 years
    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.
  • AJ.
    AJ. almost 13 years
    Size of file (before or after) can be determined by calling filesize()
  • Alvin
    Alvin almost 13 years
    It don't work.. I used <code>$file = file_get_contents($path)</code> and then <code>echo "Size to Write: ".filesize($file)</code>
  • AJ.
    AJ. almost 13 years
    Read the documentation for filesize(). It expects the path to a file, not the contents of a file. us3.php.net/filesize
  • Alvin
    Alvin almost 13 years
    uh, i'm supposed to get from URLs (download) .. But "filesize()" don't work with URL(s).. ;(
  • AJ.
    AJ. almost 13 years
    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?
  • Alvin
    Alvin almost 13 years
    Yes yes, i'm SORRY! Please (everyone!) kindly jump to my new thread stackoverflow.com/q/6085524/753726
  • rzetterberg
    rzetterberg almost 13 years
    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?
  • Alvin
    Alvin almost 13 years
    Woooooooooooow! Yes!! Thanks so much minitech! ;)
  • Ry-
    Ry- almost 13 years
    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."
  • rzetterberg
    rzetterberg almost 13 years
    Alright, so that means it has to download the file anyway then when using get_headers since it's a GET?
  • Ry-
    Ry- almost 13 years
    @Ancide: No, it cancels after it gets the headers. (By "overhead", the person meant "the rest of the headers", I believe.)
  • Alvin
    Alvin almost 13 years
    $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"
  • rzetterberg
    rzetterberg almost 13 years
    @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 :)
  • Alvin
    Alvin almost 13 years
    @AJ: btw, how did you typed the comment text to show formatted <code></code> blocks?
  • Jonah
    Jonah almost 13 years
    @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.