Hine Hine - 5 months ago 10
PHP Question

Check a file on a different server

I want my user to download a file which my script generates and put it on their server (this part has been built successfully). The goal is to verify that the user has the ability to upload files to the website they claim they own. I will be checking the root of the website so an example would be http://www.google.com/file

I then want my script to check if the file is present on their server. I figured, I could use some javascript to check if the domain of the user combined with a file path would return any different HTTPresponse than 404.

SO I looked around on the internet and tried a few things. Now here is the resulting function :

/* DUMMY */
url = 'http://www.google.com/';

xhr = new XMLHttpRequest();
xhr.open("HEAD", url,true);
xhr.onreadystatechange=function() {
alert("HTTP Status Code:"+xhr.status)

The url I used should exist. This should result in a 200 (or something along the lines of it exists). However, for most URL's I'll get an error 0 and following error: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost' is therefore not allowed access.

Could anyone help me out with my script?


I would suggest use php to check the same. If you are wondering about the issue you are getting read about CORS.

This is a simple example to do it

$file = 'http://www.domain.com/somefile.jpg';
$file_headers = @get_headers($file);
if($file_headers[0] == 'HTTP/1.1 404 Not Found') {
$exists = false;
else {
$exists = true;

From here : http://www.php.net/manual/en/function.file-exists.php#75064