...

View Full Version : Upload script fails on IE



Jon W
02-02-2010, 09:06 PM
I'm not to sure if server side code would be effected or by different browsers but this script doesn't work on internet explorer. It fails every time which I can't see why it is failing for. If I could some help on this then that would be great.




<?php
session_start();
class imageHandler {

public $submit; // The submit button name
public $location; // The upload location to be uploaded at
public $name; // The name of the file
public $tmp_name;
public $type; // The file type
public $size; // The file size

public function conn($link) {
if($link) {
require($link);
}
}

public function validate() { //make sure that this file is a image
$valid = array(1 => 'png', 2 => 'jpg', 3 => 'jpeg', 4 => 'gif');

if(!in_array($this->type, $valid)) {
return false;
}

return true;
}

public function newname($rename) {

if($this->validate() == true && isset($_POST[$this->submit])) {

//first thing is to remove the file extension
$remove = array(1 => '.png', 2 => '.jpeg', 3 => '.jpg', 4 => '.gif');

$noExt = str_replace($remove, '', $this->name);
//we will also check to make sure that there isn't any white spaces
$noExt = str_replace(' ', '_', $noExt);

//now we are going to check if we are wanting to rename
//this photo with a rand digit or use the previous name
//now so we can refer it again in the database in a different
//directory

if($rename == true || !isset($_SESSION['newname']) || $_SESSION['noExt'] !== $noExt) {

if(isset($_SESSION['newname'])) {
unset($_SESSION['newname']);
}

while(file_exists($this->location.$newname)) {
$rand = rand(0, 999999);
$newname = $noExt.'-'.$rand.'.'.$this->type;
$_SESSION['newname'] = $newname;
$_SESSION['noExt'] = $noExt;
}

}
else
{
$newname = $_SESSION['newname'];
}

return $newname;

}
else
{
return false;
}

}

public function upload($new_width, $new_height, $db) {
if(!file_exists($this->location.$this->newname(false))) { //Dont allow any file to be uploaded that already exists

if($new_width == false && $new_height == false) { // If there is going to be no image resize}
copy($this->tmp_name, $this->location.$this->newname(false));
}

if($db == true) {
//Also remove the users old image to save space
$rSql = "SELECT photo FROM users WHERE username = '".$_SESSION['username']."'";
$rQuery = mysql_query($rSql);
$last = mysql_fetch_assoc($rQuery);
$_SESSION['last'] = $last['photo'];

if(file_exists($this->location.$_SESSION['last']) && !empty($_SESSION['last'])) { //run a check to make sure the file is there
unlink($this->location.$_SESSION['last']);
}

}
else {
if(file_exists($this->location.$_SESSION['last']) && !empty($_SESSION['last'])) { //run a check to make sure the file is there
unlink($this->location.$_SESSION['last']);
}
}


if($db == true) { // Save this image as the user profile image
$sql = "UPDATE users SET photo = '".$this->newname(false)."' WHERE username = '".$_SESSION['username']."'";
$query = @mysql_query($sql);

if(!$query) {
die('Server Error: ' . mysql_error());
}
}

if($new_width == true || $new_height == true) { // Image is set to be resized.

list($width, $height) = getimagesize($this->tmp_name);
$image_p = imagecreatetruecolor($new_width, $new_height);

switch($this->type) { //switch between different file types
case 'jpeg':
$image = imagecreatefromjpeg($this->tmp_name);
break;
case 'jpg':
$image = imagecreatefromjpeg($this->tmp_name);
break;
case 'gif':
$image = imagecreatefromgif($this->tmp_name);
break;
case 'png':
$image = imagecreatefrompng($this->tmp_name);
break;
}

imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

switch($this->type) {
case 'jpeg':
imagejpeg($image_p, $this->location.$this->newname(false), 100);
break;
case 'jpg':
imagejpeg($image_p, $this->location.$this->newname(false), 100);
break;
case 'gif':
imagegif($image_p, $this->location.$this->newname(false), 100);
break;
case 'png':
imagepng($image_p, $this->location.$this->newname(false), null);
break;
}
}

}
}
}
?>


And then I called it like this...



<?php
if(isset($_POST['submit'])) {
include('../scripts/class.php');

$image = new imageHandler();
$image->submit = 'submit';
$image->tmp_name = $_FILES['photo']['tmp_name'];
$image->name = $_FILES['photo']['name'];
$image->type = basename($_FILES['photo']['type']);
$image->conn('../conn.php');

$image->location = '../../uploads/profile/';
$image->newname(false);
$image->upload(220,166,true);

}

?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<body>
<form action="upload.php" method="post" enctype="multipart/form-data">
<input name="photo" type="file" />
<input name="submit" type="submit" value="Upload" />
</form>
</body>
</html>


Thanks for the help.

angst
02-02-2010, 09:53 PM
nothing particular stands out to me while looking at your code.

though a few things seem a bit strange, for example, I don't see where your getting the $type from. I see that you pass it the basename, then in the class try to compare it to file extentions?

also this part seems a bit odd to me:


while(file_exists($this->location.$newname)) {
$rand = rand(0, 999999);
$newname = $noExt.'-'.$rand.'.'.$this->type;
$_SESSION['newname'] = $newname;
$_SESSION['noExt'] = $noExt;
}


why would you use session for this area, when the rest of the script is passing data back to the class?


have you tried debugging?
ie: go down the script one step at a time and see where it's stopping.

bdl
02-02-2010, 09:59 PM
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">


Why are you using an XML namespace with HTML4? Did you mean to use an XHTML DTD? This could be why IE is having a hard time.

Otherwise, please explain what is / how it is failing.

Jon W
02-02-2010, 10:29 PM
I doubt its really the script is the problem but I'm open to options too. I've took a look to what you guys said I haven't tried debugging yet because the script runs fine on other browsers.

How its failing is you select a image to upload and you click upload it acts like it is uploading but then it seems like it times out and stops uploading. When I go and check the photo has not been updated. I'm really confused by this whole thing because I know IE has problems but I thought the server side script run in the background on the server and not really in the browser like JavaScript and other markup languages. I'll give debugging a try and see what I come up with.

_Aerospace_Eng_
02-03-2010, 02:04 AM
What versions of IE does it fail in or what versions of IE have you tried?

Jon W
02-03-2010, 02:19 AM
Internet explorer 8.

Jon W
02-03-2010, 01:05 PM
Does anyone have any idea what the problem might be with this? I still haven't got this one resolved.

MattF
02-03-2010, 01:41 PM
Does anyone have any idea what the problem might be with this? I still haven't got this one resolved.


I haven't tried debugging yet because the script runs fine on other browsers.


Neither will you unless you follow the correct steps for isolating a problem. Step number one usually being actually trying to isolate the problem, funnily enough.

Jon W
02-03-2010, 02:40 PM
Okay so for some reason IE is reading jpeg files a pjpeg.... Apparently it stand for protected jpeg... I wonder who was the genius at microsoft decided to rename the mime type for jpegs... So what was happening was the script was failing every time it got the point where it would check to see if it was a valid file type... so I added a line in the code to allow this file types... But now I get a new error saying:

Warning: imagecopyresampled(): supplied argument is not a valid Image resource in /var/tech/scripts/class.php on line 131

Hrmm...

Jon W
02-03-2010, 03:28 PM
Okay, I got this problem resolved. Thanks guys. :)

angst
02-03-2010, 03:29 PM
what was the issue?

Jon W
02-03-2010, 04:00 PM
I had a few switch that wasn't allowing the pjpeg. So I just added the pjpeg and it is now correctly working. After fixing this problem shortly after thinking I didn't have any more problem I tried uploading a png. png are being read as x-png in IE, so again I had to allow it to be used. And that was pretty much it. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum