【问题标题】:PHP Image upload with iOS Safari not providing TMP_NAME使用 iOS Safari 上传 PHP 图像不提供 TMP_NAME
【发布时间】:2015-12-23 03:12:04
【问题描述】:

我最近在我的网站上注意到,用户无法在移动设备上上传个人资料图片。所以我从根目录复制了所有需要的 HTML 和 PHP 代码。个人资料图片的移动位置也进行了修改,以便将图片上传到正确的位置。现在,当我对其进行测试并尝试使用 Safari for iOS 上传个人资料图片时,我收到此错误:

Warning: getimagesize() [function.getimagesize]: Filename cannot be empty in /home5/bobcatss/public_html/mobile/profile.propic.php on line 18

这是用户提交的表单:

    <div class="propic"><form method="post" action="profile.propic.php" enctype="multipart/form-data">
        <input type="hidden" name="MAX_FILE_SIZE" value="100000" />
        Upload a profile picture <br><input name="file" type="file" id="file"/><br>
        <input type="submit" value="Upload">
    </form></div>

这是处理图像的脚本:

<?php 

error_reporting(E_ALL);
ini_set('display_errors', 1);

include("../config.php");
session_start();

// Where the file is going to be placed 
$target_path = "../propics/";

$filename = $_FILES["file"]["name"];

$limit_size=100000;
$temp = explode(".", $filename);
$extension = end($temp);

$info = getimagesize($_FILES["file"]["tmp_name"]); //Line 18

$allowed_types = array(IMG_GIF, IMG_JPEG, IMG_PNG, IMG_JPG);
if (in_array($info[2], $allowed_types))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    exit;
    }
  else
    {
    if (file_exists("../propics/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../propics/" . $_FILES["file"]["name"]);

      try {

    //insert into database
    $stmt = $db->prepare('UPDATE users SET propic = :propic WHERE username = :username') ;
    $stmt->execute(array(
        ':propic' => $filename,
        ':username' => $_SESSION["USER"]
    ));

    //redirect to profile page
     header("Location: profile.php?msg=Profile picture upload complete");
    } catch(PDOException $e) {
        echo $e->getMessage();
    }


      }
    }
  }
else
  {
  echo "Invalid file";
  }
?> 

我将图像通过电子邮件发送给自己并尝试从笔记本电脑上传,但它给了我同样的错误。

【问题讨论】:

  • 这些预定义的常量(IMG_GIF, IMG_JPEG, IMG_PNG, IMG_JPG)是在别处配置的吗?
  • 否,但在计算机上使用相同的代码不会给出与允许的类型相关的任何错误或警告。
  • 你试过$allowed_types=array('jpg','jpeg','gif','png');吗?你现在使用的是基于常量的。
  • 在 php.ini 中 file_uploads 标志是否设置为 true?
  • 我能让您上传代码正常工作的唯一方法是将if(in_array($info[2], $allowed_types)) 更改为if(in_array($extension, $allowed_types))

标签: php html ios safari


【解决方案1】:

我知道这很老了,但这也发生在我身上,我遇到了这件事,试图弄清楚发生了什么。如果您使用的是 3G 连接,请求所用时间可能比 MAX_REQUEST_TIMEOUT 长,因此它可能会失败 ( duplicate here )

【讨论】:

    猜你喜欢
    • 2018-11-30
    • 2017-09-30
    • 2015-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-02
    相关资源
    最近更新 更多