Jump to content

Simple AJAX and PHP - just checking if a file exists (doesn't work in IE6)


davil

Recommended Posts

I found a great client side image resize solution:

http://www.resize-before-upload.com/

 

but the free version doesn't have form integration, so I copied some ajax code off the internet and integrated it into my PHP - so it just keeps checking for a specific file to be uploaded and then the PHP just echos 'success' and if the ajax sees that it redirects to another page to continue the process

 

Here's the JS (Jquery obviously)

$(document).ready(function()
{
   //filecheck.php is called every 2 seconds to ask server if file has been uploaded yet
   var refreshId = setInterval(function()
   {
   $('#Davfilecheck').load('scripts/filecheck.php');
   
   var fc1 = new String();
   fc1 = document.getElementById('Davfilecheck').innerHTML;

if (fc1 == "Success!"){
alert ("The photo has been successfully uploaded to the site. Click OK to enter details for photo");
window.location="index.php?locate=upload&filedone=yes";
}


   }, 2000);

});

 

And here's the 'filecheck.php' code

<?php
if (file_exists('../secure/uploads/uploadfile')){echo "Success!";}
?>

 

This system works great with most newer browsers but when the file is uploaded in IE6 the JS doesn't seem to notice.

 

I will upload my main PHP script if necessary but I'm pretty sure this is the part that won't work in IE6.

 

Is the only option to ask the users to upgrade browser or is there a hack for getting this to work in IE6 ?

 

My PHP coding is a lot better than my JS coding. :-D

Link to comment
Share on other sites

  • 2 weeks later...

Try it this way.  The random number is needed because IE likes to cache pages it "thinks" are the same.  Also I didnt set the inner html of the div. But that can be done by adding $('#Davfilecheck').html(data); to the function.

$(document).ready(function()
{

filecheck();

});

function filecheck(){
  var randomnumber=Math.floor(Math.random()*11)
  $.get('scripts/filecheck.php', {rand: randomnumber}, function(data) {
    if (data == "Success!"){
      alert ("The photo has been successfully uploaded to the site. Click OK to enter details for photo");
      window.location="index.php?locate=upload&filedone=yes";
    }
  };
  setTimeout('filecheck()', 2000);
}

Link to comment
Share on other sites

Thanks Icarus. I'll give that a go. I did almost get it working today by enabling XMLHTTPRequest properly before my code, but I'm aware that the way I was doing things is bad bad bad.... I'm not that hot at JS, PHP's my language... but I'll get there.

Link to comment
Share on other sites

sorry I just tested that new code and now it doesn't work in newer browsers either (tested in Google Chrome) - so I'll be reverting to original for the moment. However, it's probably something I neglected to do, I didn't fully understand what you meant by "I didnt set the inner html of the div , that can be done by adding $('#Davfilecheck').html(data); to the function. " perhaps you could explain a bit further ? sorry....

Link to comment
Share on other sites

Woops left out a closing parenthesis

 

$(document).ready(function()
{

filecheck();

});

function filecheck(){
  var randomnumber=Math.floor(Math.random()*11)
  $.get('scripts/filecheck.php', {rand: randomnumber}, function(data) {
    if (data == "Success!"){
      alert ("The photo has been successfully uploaded to the site. Click OK to enter details for photo");
      window.location="index.php?locate=upload&filedone=yes";
    }
  });
  setTimeout('filecheck()', 2000);
}

 

What i meant is that in your original code you we setting the contents of #Davfilecheck to what the php file check was returning.  In this version I didnt do that.

Link to comment
Share on other sites

Ok it seems to be working now (nearly finished). Previously I had to add this code to get IE6 to play ball:

if (!XMLHttpRequest) {
  window.XMLHttpRequest = function() {
    return new ActiveXObject('Microsoft.XMLHTTP');
  }
}

 

but since your code changes, I no longer need that piece of code (weird!) - anyhow, it's working all but for one small issue:

depending on the timing of when you click upload, the window.location redirect might happen a second time, which plays havoc with my PHP code, which expects to be run once (it copies the original upload file elsewhere and deletes it at the very start, so when looped it can't find the file)

 

I could do some sort of $_SESSION hack with a variable like 'stayput' or something, but there must be a better way to do this??

 

I'm so close now I can almost taste it. :-D

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.