Reputation: 91
Lets say I have a cms set up in a remoter server. Would it be possible to get the actual physical size of the index.php file though an external php script without running the cms?
Can't use file_get_contents() as this will return the whole home page for me. I just need the physical filesize of the index file. Is this even possible?
Upvotes: 0
Views: 315
Reputation: 5910
Fortunately that's not possible. Functions like filesize()
will only handle files on the local filesystem for obvious reasons.
If you could access the physical filesize of a remote file you could also processes other operations (like reading the plain file) to a remote file which would not be very secure, would it?
However, if you have physical access to the remote server, there would be no problem to connect to it with PHP.
PHP provides functionionality to connect to remote servers using SSH. It's also pretty easy (but not necessarily handy) to work with it.
Further reading:
If, for whatever reasons, you can't SSH into your server, but have a FTP-Server installed, PHP provides an interface for working with the file transfer protocol.
Further reading:
I'm sure that if you wanted to, you could find another bunch of possibilities to connect to the remote server. I've chosen SSH and FTP since they are pretty common and even the most shared-hosting packages do provide access through them.
Please note: Connecting to remote servers just to grab the filesize of a file is pretty much an overkill. Make sure that the advantages overcome the downsides of it.
@thedom provided a good example of how to provide an interface to work with files on your remote server without connecting to it via SSH or FTP.
Upvotes: 0
Reputation: 2518
You could place a script on the remote server and call it from your webserver.
The remote file could be like and you call it with http://remote-serv.er/filename.php?f=file-to-get-size-of.php
<?php
if(isset($_GET['f']) && !empty($_GET['f'])) {
if(file_exists($_GET['f'])) {
echo(filesize($_GET['f']));
}
}
?>
Upvotes: 1
Reputation: 943615
In general - no. To get the file size of the PHP file itself (as opposed to the HTML generated when that PHP is executed) you would need access to the filesystem of the computer hosting the file. The information is not exposed through HTTP.
If you had that access, then you could use filesize
to get the information.
Upvotes: 0