簡體   English   中英

圖片上傳在IE中不起作用? 但是在其他瀏覽器中也可以

[英]Image upload doesnt work in IE? But it's fine in other browsers

因此,我面臨着這個問題,這是我在4年多的發展中從未遇到過的問題,

HTML代碼

<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>

還有PHP代碼...

$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++;
    }

除了IE,代碼在所有瀏覽器中都能正常工作嗎? 它甚至沒有插入MYSQl中,有什么想法嗎?

查看是否可行:

// 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
  }
}

關鍵區別在於使用的是文件的擴展名,而不是MIME類型(這取決於瀏覽器是否明智)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM