I'm really starting to hate files!
I've spent all night trying to figure whats wrong with this code below but I cant figure it out.
It uploads a file, renames it, then displays it.
It works becuase after uploading the file, I check my server. The correct image is uploaded and renamed correctly....
but...
When the image displays.. It displays the wrong image... Even if the image has been deleted.
It seems the problem is with the cache. If I press CTRL+F5, I get the correct image. This is the only way to get the correct image to display.
Anyway, here's the whole script. Can somebody please tell me if I'm missing something?
http://www.argyllnewmedia.co.uk/auction/image_test.php
<?
$sent = $_POST['sent'];
$pic_remove = $_POST['pic_remove'];
$pic_set = $_POST['pic_set'];
$p_name = $_POST['p_name'];
$item_no = 2;
$archive_dir = "user_images/444444";
if ($sent==true)
{
foreach ($_FILES["pic"]["error"] as $key => $error)
{
if ($error == UPLOAD_ERR_OK)
{
// copy the file
$tmp_name = $_FILES["pic"]["tmp_name"][$key];
$name = $_FILES["pic"]["name"][$key];
move_uploaded_file($tmp_name, "$archive_dir/$name");
// create a new file name and get extension
$fn = $item_no."-".$key;
$ext = substr_replace($_FILES["pic"]["name"][$key],"",0,strpos($_FILES["pic"]["name"][$key],"."));
// delete all other files of the same name but with a different extension
if ((file_exists("$archive_dir/$fn.jpg")) && ($ext!=".jpg"))
unlink("$archive_dir/$fn.jpg");
if ((file_exists("$archive_dir/$fn.jpeg")) && ($ext!=".jpeg"))
unlink("$archive_dir/$fn.jpeg");
if ((file_exists("$archive_dir/$fn.gif")) && ($ext!=".gif"))
unlink("$archive_dir/$fn.gid");
if ((file_exists("$archive_dir/$fn.bmp")) && ($ext!=".bmp"))
unlink("$archive_dir/$fn.bmp");
if ((file_exists("$archive_dir/$fn.png")) && ($ext!=".png"))
unlink("$archive_dir/$fn.png");
// rename the uploaded file
$new_fn[$key] = $item_no."-".$key.$ext;
rename("$archive_dir/$name", "$archive_dir/$new_fn[$key]");
}
if ($error == UPLOAD_ERR_INI_SIZE)
$err1[$key]=true;
if ($error == UPLOAD_ERR_PARTIAL)
$err2[$key]=true;
if (($_FILES["pic"]["type"][$key]!="image/pjpeg") && ($_FILES["pic"]["type"][$key]!="image/gif") &&
($_FILES["pic"]["type"][$key]!="image/bmp") && ($_FILES["pic"]["type"][$key]!="image/jpeg") &&
($_FILES["pic"]["type"][$key]!="image/png") && ($_FILES["pic"]["name"][$key]!=""))
$err3[$key]=true;
}
}
?>
<form action="image_test.php" method="post" enctype="multipart/form-data">
<input type="hidden" name="sent" value="true" />
<?
$i = 0;
while($i <= 5)
{
$y=$i+1;
if ($pic_remove[$i]=="Remove Picture")
{
echo "<p>Picture $y: <input type=\"file\" name=\"pic[$i]\" /></p>";
unlink("$archive_dir/$p_name[$i]");
}
elseif ($err1[$i]==true)
echo "<p style=\"color:#FF0000;\">Picture $y: <input type=\"file\" name=\"pic[$i]\" /> File is too big. Limit is 800 kb.</p>";
elseif ($err2[$i]==true)
echo "<p style=\"color:#FF0000;\">Picture $y: <input type=\"file\" name=\"pic[$i]\" /> File did not upload.</p>";
elseif ($err3[$i]==true)
echo "<p style=\"color:#FF0000;\">Picture $y: <input type=\"file\" name=\"pic[$i]\" /> Incorrect file type.</p>";
elseif (($pic_set[$i]==true) || ($_FILES["pic"]["name"][$i]))
{
if ($_FILES["pic"]["name"][$i])
$source="$archive_dir/$new_fn[$i]";
elseif ($p_name[$i])
$source="$archive_dir/$p_name[$i]";
echo "<div style=\"width:300px;background-color:#FFFFAE;border:#999999 solid 1px;margin-bottom:12px;\">";
echo "<table cellpadding=\"5\" cellspacing=\"0\"><tr><td rowspan=\"2\"><img src=\"$source\" width=\"58\" height=\"58\"></td>";
echo "<input type=\"hidden\" name=\"pic_set[$i]\" value=\"true\" />";
if ($_FILES["pic"]["name"][$i])
{ ?> <input type="hidden" name="<? echo "p_name[$i]" ?>" value="<? echo $new_fn[$i] ?>" /> <? }
else
{ ?> <input type="hidden" name="<? echo "p_name[$i]" ?>" value="<? echo $_POST["p_name"][$i] ?>" /> <? }
echo "<td>Picture $y</td></tr><tr>";
echo "<td valign=\"bottom\"><input type=\"submit\" name=\"pic_remove[$i]\" value=\"Remove Picture\"></td></tr></table></div>";
}
else
echo "<p>Picture $y: <input type=\"file\" name=\"pic[$i]\" /></p>";
$i++;
}
?>
<div id="msg" style="display:none;height:50pox;border:double #FF0000 6px;margin:10px;padding:20px;text-align:center;">
<div style="font-size:20px;font-style:italic;font-weight:bold;">Loading Pictures</div>
<b>Your pictures are being sent to our system. Depending on your connection speed and the size of your image files, this might take
several minutes. Please wait.</b></div>
<input type="submit" name="direction" value="Upload Pictures" onClick="if (hasPic(this.form))
document.getElementById('msg').style.display='';" />
</form>
<script language="javascript">
function hasPic(form) {
var pic="";
for (var i=0; i<=5; i++) {
if (form["pic["+i+"]"]) {
pic+=form["pic["+i+"]"].value;
}
}
return pic!="";
}
</script>