Search code examples
urlluafilesize

How to get file size from url in Lua?


I want to get file's size from a url that it is uploaded into it.

I have a url of a picture. I want to get it's size using the url in Lua but I don't know how

Thanks.


Solution

  • function get_resource_size(url)
       local pipe = io.popen('wget -S --spider --no-check-certificate "'..url..'" 2>&1')
       local size = pipe:read"*a":match"Content%-Length: (%d+)"
       pipe:close()
       return size and tonumber(size)
    end
    
    print(get_resource_size(
       "https://www.gravatar.com/avatar/282ad8d2c96e9b753bde22ac6ca0918b?s=32&d=identicon&r=PG"
    ))
    

    Your avatar has size of 2074 bytes