Access to client's localhost from server - php

Here is the context : I need to load heavy files in the Google Earth plugin on a website. I have a php script that build up a path to those files and send it to javascript through ajax. Then, the plugin download the file and build it on earth. This take a long time. I need to speed that up for a public event. The computer can run a web server and have these files on his hard drive.
So here is my question : Is there a way for php to check if the client is running a local server and check if it contains a specific file ? Or at least, to execute a php script from this local server ?
Something like :
if(is_file('localhost/files/heavy.kmz'))
$path = 'localhost/files/heavy.kmz';
else
$path = 'www.randomsite.com/files/heavy.kmz';
return $path
Of course, this localhost refers to the wrong server :(.
I guess that if it was possible, there would be security issues. But i ask anyway.
I'm not very used to stackoverflow habits, I hope a didn't do anything wrong.
Kororo.
Edit : I will try to clarify a bit.
I know the path of the files on localhost. I need to check if it exist or not in order to send a path or another. If i can find it, i don't have to download it from the server.
The local webserver is only needed on machine to allow php check of the file.

If the data file is on the same machine, and you can tell where your data file should be relative to the script you are using to check it's existence, just use the __DIR__ (or dirname(__FILE__) if your php is ancient) to create the path.
So for example, you have have the directory structure like this:
files/
heavy.kmz
check.php
...
Then inside the check.php the following work as expected:
if (is_file(__DIR__.'/files/heavy.kmz')) {
$path = __DIR__.'/files/heavy.kmz';
} else {
$path = 'www.randomsite.com/files/heavy.kmz';
}

Related

How to get client side absolute path of selected file in php

I want to know whether there is anyway to get the client side absolute path of selected file!
$_FILES['xml_file']['tmp_name']; This also provides path of selected file but its server side path... Its result is something like this C:\xampp\tmp\php2679.tmp
I want C:\Users\Sami\Desktop\data\myfile.xml where my actual file is placed.
Because I want this actual path to use again for fetching and saving etc automatically (by code).
you may try echo realpath($_FILES['xml_file']['name']); on your php.
then the output will be:
C:\Users\Sami\Desktop\data\myfile.xml
When you click the browse button it chooses the file. When you click the submit button, the entire file is being sent from the browser to the web server, and PHP is indeed putting the entire file in the php servers temp directory. Then, your script gets called.
So when you try to get absolute path from $_FILES['xml_file']['name'] or $_FILES['xml_file']['tmp_name'] or realpath($_FILES['xml_file']['name']) you will get the path on server.
The solution to this problem would be to simply copy the file somewhere, and not ask for the file upload again. You'll need to be sure that where ever you copy it to on your web server, that the server has permissions to do so. Usually that involves giving write permissions for the user which the web server runs as (usually user 'apache' or 'nobody' for apache based servers) to a special directory you've created for the purpose.
Copy is available in PHP. http://us3.php.net/manual/en/function.copy.php
Your only trick will be what to name it so you can find it again for the second request, and deciding what directory to put it into.

PHP- delete/unlink a file using an domain path

how can I delete a file using an url path ?
I have
$file_with_path = "http://www.myweb.com/uploads/audio.mp3";
if (file_exists($file_with_path)) {
unlink($file_with_path);
}
I don't use "/uploads/audio.mp3" or similar directory paths due some reasons.
thanks in advance !!
unlink tells the operating system to delete a given file. The OS identifies files by file system path - it does not interact with URLs in any way. URLs are translated to file system paths by the web server, which is an entirely different piece of software. While theoretically there is a way to tell a web server to delete the file (by sending a HTTP DELETE request), no web server is going to honor that - it would be way too insecure. It is relatively easy to control who can access the file system; it is very hard to control who can send requests to the web server.
In short, you will have to figure out what the file system path for the file is, and use unlink (and file_exists) with that path.

Get remote path of current location

I am making a reseller hosting, for my own clients, but now I want to store my user-backups on a different host. So use a cronjob to do this.
But now my cronjob asks me for a Remote path to store the backups on.
I just want them in the /backups folder, but I guess I can't just fill in /backups in the remote path?
So how can I see what the full pathname is?
My idea was to create a .php file, and echo the remote path with some code.
I can't find the code on the internet so I asked it here.
If you need a path of a other machine with PHP, then thats do the trick:
echo __FILE__; // Full Filename
// OR
echo basedir( __FILE__ ); // Only the Path

Is PHP copy() using two client PCs possible?

I'm using two computers (both connected to one network) and one of them has XAMPP. I'm trying to upload files to the one with XAMPP in it (the files are from the other computer). But I always end up having the 'No such file or directory' error even though I have the correct path. But when I use the path from the computer with XAMPP, even when I'm using the other computer, the system works just fine. Can anyone help me?
P.S. I'm using PHP copy() function because the file path is coming from an excel file.
Here's the part of my PHP code:
$original_file_name = objWorksheet->getCellByColumnAndRow(5,$i)->getValue();
// Example of the cell value: C:\Users\ComputerWithoutXAMPP\Desktop\scanned documents\SO 2010\#1.jpeg
$ext = pathinfo($original_file_name, PATHINFO_EXTENSION);
$file = time().substr(md5(microtime()),rand(0,26),5);
// UPLOAD THE FILE DECLARED IN EXCEL
copy($original_file_name, 'uploads/docs/'.$file.'.'.$ext);
You can use copy() to upload a file to another machine, or from another machine, but to access the remote machine, the appropriate argument to copy (source or dest) has to be a URL. The code you've posted is trying to copy the file to a local "uploads/docs/" directory, it's doesn't even appear to aware of another machine.
While what you're looking to do may be technically possible, I haven't the foggiest idea how you'd go about it: it seems rather Rube Goldberg to me. The ftp:// wrapper would probably work, if FTP is set up properly on the XAMPP server.
How big is the file you're trying to send? If it's small enough, you may have better luck either encoding and sending the content itself or uploading the file with curl to an upload-catching script on the XAMPP side

Opening a Word document using the PHP COM object

I've got some code that generates a word document, as follows:
$word->Documents [1]->SaveAs ( $localDir . $filename );
Now, I was kinda hoping that I could now open the file once it's saved by doing the following:
$word->Documents->Open($remoteDir . $filename)
// remotedir = 'word/', so for example the above would be '/word/document1.doc'
But it seems to open it on the host machine, and not the users! Is there anyway to open it on the user's machine and not on the server?
edit: Just for clarity it will be used exclusively on an intranet by a single user that will be on a Windows machine at all times, with Word etc installed... just want to try and make her life a little easier!
Thanks
I think you are fundamentally mistaken about what runs where. PHP is a purely server side language. You can not use it to open a file on the client's PC so that the user has an opened instance of Word in front of them.
You can maybe achieve that through client side Scripting, namely in VBScript or some other Microsoft scripting flavour. Be prepared for massive obstacles and incompatibilities, though, because such things are blocked for security reasons by default in all browsers, and sometimes those blocks cannot be circumvented even with special settings ("Trusted sites") in the client browser.
You may be able to display the document in the user's browser as an embedded HTML object.
The most simple thing really may be generating the file, and offering it to the user as a download. The user can then save it, and open it. Job done.
/word/document1.doc is the path for a file in the server, not in the client. On Windows, supposing that the file sharing is enabled for the client PC, then you can use a path such as \\IP\word\document1.doc, where IP is the IP of the client PC.
You can get the IP of the PC connecting to the server with $_SERVER['REMOTE_ADDR']; $_SERVER['REMOTE_HOST'] is the result of a DNS reverse lookup, which could return the same value of $_SERVER['REMOTE_ADDR'], in your case.
Probably PHP will not open remote files if it has not been set to do so (there is a directive or that).
If directly accessing the shared file from the COM object doesn't work, then you can copy the file from the client PC to the server in a temporary file, and then give that file to the COM object. In this way, if there are any errors while accessing the networked file, you should be able to get them.
I find strange, anyway, that passing a network file path you get a local file. Are you sure the COM object is not copying of the server the file it finds at the remove file path passed? Did you try with a different file? If that happens with different files too, then we are missing something; I would find strange that for all the network files you try to open, there is already a local file with the same name. Try also renaming the network files.

Categories