Image upload doesnt work in IE? But it's fine in other browsers
So I'm facing this issue, one I've never had before in over 4 years of development,
HTML code
<fieldset id="step_3" style="display:none;">
<legend>3. Add Photos</legend>
<ol>
<li>
<label for="main_image">Main Image..</label>
<input type="file" name="extra_img0" size="35"/>
</li>
<li>
<label for="extra_img1">Extra Images</label>
<input type="file" name="extra_img1" size="35"/>
</li>
<li>
<label for="extra_img2"> </label>
<input type="file" name="extra_img2" size="35" />
</li>
<li>
<label for="extra_img3"> </label>
<input type="file" name="extra_img3" size="35"/>
</li>
<li>
<input type="submit" name="add" value="Add Listing"/>
</li>
</ol>
</fieldset>
And the PHP code...
$i = 0;开发者_开发知识库
while($i < 4) {
if(!empty($_FILES['extra_img'.$i]['name'])) {
if($_FILES['extra_img'.$i]['type'] == "image/gif" OR $_FILES['extra_img'.$i]['type'] == "image/png" OR $_FILES['extra_img'.$i]['type'] == "image/jpeg") {
$img = md5(microtime()).'.jpg';
$image = New SimpleImage();
$image->load($_FILES['extra_img'.$i][tmp_name]);
if($image->getWidth() < $image->getHeight()) {
$image->resizeToWidth("300");
$image->cutHeight("300");
} else {
$image->resizeToHeight("300");
$image->cutWidth("300");
}
$image->save('uploads/listings/large/'.$img);
$db->query("INSERT INTO `images_to_listing` (`listing_id` ,`name`) VALUES ('{$listing_id}', '{$img}');");
}
}
$i++;
}
Code works just fine in all browsers apart from IE? It doesn't even get inserted into MYSQl, any ideas?
See if this works:
// List of allowable file extensions
$allowedExtensions = array (
'jpg',
'jpeg',
'gif',
'png'
);
for ($i = 0; $i < 4; $i++) {
// Loop 0-3
if (!empty($_FILES["extra_img$i"]['name']) && in_array(strtolower(pathinfo($_FILES["extra_img$i"]['name'],PATHINFO_EXTENSION),$allowedExtensions))) {
// If you get here, the image is set and has a file extension specified as allowable above
$img = md5(microtime()).'.jpg';
$image = New SimpleImage();
$image->load($_FILES["extra_img$i"]['tmp_name']);
if ($image->getWidth() < $image->getHeight()) {
$image->resizeToWidth("300");
$image->cutHeight("300");
} else {
$image->resizeToHeight("300");
$image->cutWidth("300");
}
$image->save("uploads/listings/large/$img");
$db->query("INSERT INTO `images_to_listing` (`listing_id` ,`name`) VALUES ('{$listing_id}', '{$img}');");
} else {
// You may want to add error handling here
}
}
The key difference is that is uses the file's extension, rather than it's MIME type (which relies on the browser being sensible).
精彩评论