Flash image upload with PHP - images loading from cahce!

  • DrGonzo
  • Beginner
  • Beginner
  • DrGonzo
  • Posts: 36

Post 3+ Months Ago

Hello all,
I'm using the Flash fileReference class with a PHP serverside script to allow users to upload images to my application. It works just fine; the images are being uploaded to the sever and downloaded to Flash without any problems, plus my PHP script makes a time stamped copy in a seperate folder in case another user uploads an image of the same name and overwrites the prevoius one.

The problem is this: if a user uploads and image and then the same user uploads a different image of the same name, the previous image is overwritten (as it should be) but then Flash is loading the first image stil - from the cache - therefore displaying the wrong one to the user...

It only effects some browsers (namely IE) with particular settings (Automatically check for new versions of stored pages in IE) but obviously I need it to work universally. Also, as i've got an XML gallery on the site too, i'd ideally like to stop Flash from caching completely (if possible).

The fileReference code is:

Code: [ Select ]
System.security.allowDomain("www.tshirtsetc.co.uk");
import flash.net.FileReference;

// The listener object listens for FileReference events.
var listener:Object = new Object();

listener.onSelect = function(selectedFile:FileReference):Void {

 upWin._x = 200;
 selectedFile.upload("./upload.php");
};

// the file is starting to upload.
listener.onOpen = function(selectedFile:FileReference):Void {
 _root.upWin.results_txt.text = String("Uploading " + selectedFile.name + "\n");
};

listener.onHTTPError = function(file:FileReference, httpError:Number):Void {
  _root.upWin.results_txt.text = String("HTTPError number: "+httpError +"\nFile: "+ file.name);
}

listener.onIOError = function(file:FileReference):Void {
    _root.upWin.results_txt.text = String("IOError: "+ file.name);
}

listener.onSecurityError = function(file:FileReference, errorString:String):Void {
  _root.upWin.results_txt.text = String("SecurityError: "+SecurityError+"\nFile: "+ file.name);  
}

listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
    upWin.loadBar._width = Number(bytesLoaded)/Number(bytesTotal)*300;
}

// the file has uploaded
listener.onComplete = function(selectedFile:FileReference):Void {
 upWin.results_txt.text = String("Upload finished.\nNow downloading " + selectedFile.name + " to player\n");
 
 if(position_txt.text == String("Front")){
 attachMovie("trans", "transHolder", -16161, {_x:150, _y:120});
     downloadImage1(selectedFile.name);
 }else{
 if(position_txt.text == String("Back")){    
 attachMovie("trans2", "transHolder2", -16162, {_x:450, _y:120});
      downloadImage2(selectedFile.name);
 }else{
 if(position_txt.text == String("LSleeve")){    
 attachMovie("trans3", "transHolder3", -16163, {_x:150, _y:120});
      downloadImage3(selectedFile.name);
 }else{
 if(position_txt.text == String("RSleeve")){    
 attachMovie("trans4", "transHolder4", -16164, {_x:450, _y:120});
      downloadImage4(selectedFile.name);
 }
 }
 }
 }
 Itotal_txt.text = Number(3.00);
 _root.upWin._x = 2000;
};

var imageFile:FileReference = new FileReference();
imageFile.addListener(listener);

imageMovie.uploadBtn.onPress = uploadImage;
imageMovie.uploadBtn2.onPress = uploadImage;
imageMovie2.uploadBtn3.onPress = uploadImage;
imageMovie2.uploadBtn4.onPress = uploadImage;

// Call the uploadImage() function, opens a file browser dialog.
function uploadImage(event:Object):Void {
 imageFile.browse([{description: "Image Files", extension: "*.jpg;*.gif;*.png"}]);
}

// If the image does not download, the event object's total property
// will equal -1. In that case, display am error message
function imageDownloaded(event:Object):Void {
 if(event.total == -1) {
  _root.upWin.results_txt.text = String("error");  
 }
}

// show uploaded image in scrollPane
function downloadImage1(file:Object):Void {
    transHolder.umbongo.loadMovie("./uploaded/" + file);
}

// show uploaded image in scrollPane
function downloadImage2(file:Object):Void {
    transHolder2.umbongo2.loadMovie("./uploaded/" + file);
}

// show uploaded image in scrollPane
function downloadImage3(file:Object):Void {
    transHolder3.umbongo3.loadMovie("./uploaded/" + file);
}

// show uploaded image in scrollPane
function downloadImage4(file:Object):Void {
    var randomNum:Number = Math.round(Math.random()*(10000-0))+0;
    transHolder4.umbongo4.loadMovie("./uploaded/" + file);
}
  1. System.security.allowDomain("www.tshirtsetc.co.uk");
  2. import flash.net.FileReference;
  3. // The listener object listens for FileReference events.
  4. var listener:Object = new Object();
  5. listener.onSelect = function(selectedFile:FileReference):Void {
  6.  upWin._x = 200;
  7.  selectedFile.upload("./upload.php");
  8. };
  9. // the file is starting to upload.
  10. listener.onOpen = function(selectedFile:FileReference):Void {
  11.  _root.upWin.results_txt.text = String("Uploading " + selectedFile.name + "\n");
  12. };
  13. listener.onHTTPError = function(file:FileReference, httpError:Number):Void {
  14.   _root.upWin.results_txt.text = String("HTTPError number: "+httpError +"\nFile: "+ file.name);
  15. }
  16. listener.onIOError = function(file:FileReference):Void {
  17.     _root.upWin.results_txt.text = String("IOError: "+ file.name);
  18. }
  19. listener.onSecurityError = function(file:FileReference, errorString:String):Void {
  20.   _root.upWin.results_txt.text = String("SecurityError: "+SecurityError+"\nFile: "+ file.name);  
  21. }
  22. listener.onProgress = function(file:FileReference, bytesLoaded:Number, bytesTotal:Number):Void {
  23.     upWin.loadBar._width = Number(bytesLoaded)/Number(bytesTotal)*300;
  24. }
  25. // the file has uploaded
  26. listener.onComplete = function(selectedFile:FileReference):Void {
  27.  upWin.results_txt.text = String("Upload finished.\nNow downloading " + selectedFile.name + " to player\n");
  28.  
  29.  if(position_txt.text == String("Front")){
  30.  attachMovie("trans", "transHolder", -16161, {_x:150, _y:120});
  31.      downloadImage1(selectedFile.name);
  32.  }else{
  33.  if(position_txt.text == String("Back")){    
  34.  attachMovie("trans2", "transHolder2", -16162, {_x:450, _y:120});
  35.       downloadImage2(selectedFile.name);
  36.  }else{
  37.  if(position_txt.text == String("LSleeve")){    
  38.  attachMovie("trans3", "transHolder3", -16163, {_x:150, _y:120});
  39.       downloadImage3(selectedFile.name);
  40.  }else{
  41.  if(position_txt.text == String("RSleeve")){    
  42.  attachMovie("trans4", "transHolder4", -16164, {_x:450, _y:120});
  43.       downloadImage4(selectedFile.name);
  44.  }
  45.  }
  46.  }
  47.  }
  48.  Itotal_txt.text = Number(3.00);
  49.  _root.upWin._x = 2000;
  50. };
  51. var imageFile:FileReference = new FileReference();
  52. imageFile.addListener(listener);
  53. imageMovie.uploadBtn.onPress = uploadImage;
  54. imageMovie.uploadBtn2.onPress = uploadImage;
  55. imageMovie2.uploadBtn3.onPress = uploadImage;
  56. imageMovie2.uploadBtn4.onPress = uploadImage;
  57. // Call the uploadImage() function, opens a file browser dialog.
  58. function uploadImage(event:Object):Void {
  59.  imageFile.browse([{description: "Image Files", extension: "*.jpg;*.gif;*.png"}]);
  60. }
  61. // If the image does not download, the event object's total property
  62. // will equal -1. In that case, display am error message
  63. function imageDownloaded(event:Object):Void {
  64.  if(event.total == -1) {
  65.   _root.upWin.results_txt.text = String("error");  
  66.  }
  67. }
  68. // show uploaded image in scrollPane
  69. function downloadImage1(file:Object):Void {
  70.     transHolder.umbongo.loadMovie("./uploaded/" + file);
  71. }
  72. // show uploaded image in scrollPane
  73. function downloadImage2(file:Object):Void {
  74.     transHolder2.umbongo2.loadMovie("./uploaded/" + file);
  75. }
  76. // show uploaded image in scrollPane
  77. function downloadImage3(file:Object):Void {
  78.     transHolder3.umbongo3.loadMovie("./uploaded/" + file);
  79. }
  80. // show uploaded image in scrollPane
  81. function downloadImage4(file:Object):Void {
  82.     var randomNum:Number = Math.round(Math.random()*(10000-0))+0;
  83.     transHolder4.umbongo4.loadMovie("./uploaded/" + file);
  84. }


And the PHP is:

Code: [ Select ]
<?php

move_uploaded_file($_FILES['Filedata']['tmp_name'], './uploaded/'.$_FILES['Filedata']['name']);
copy('./uploaded/'.$_FILES['Filedata']['name'], './timeStamped/'.time().$_FILES['Filedata']['name']);

?>
  1. <?php
  2. move_uploaded_file($_FILES['Filedata']['tmp_name'], './uploaded/'.$_FILES['Filedata']['name']);
  3. copy('./uploaded/'.$_FILES['Filedata']['name'], './timeStamped/'.time().$_FILES['Filedata']['name']);
  4. ?>


If anyone can think of a way to sort this out - it'd be most appreciated - i'm tearing my hair out over this!

Thanks,

DrGonzo.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • graphixboy
  • Control + Z
  • Mastermind
  • User avatar
  • Posts: 1828
  • Loc: In the Great White North

Post 3+ Months Ago

generate a random number and append it to the end of the image url in the display code. That way the browser always thinks the image being displayed is different.

IE image.jpg?=1234323212323

Your other option is to force a "no cache" on the page displaying your flash file

http://lab.artlung.com/anti-cache/

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 8 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
cron
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.