Home  >  Article  >  Backend Development  >  写了一个简单的html/php上传的网页,在安卓和电脑上都正常,但ipad上都有问题,即图片上传名称都自动变成image.jpg

写了一个简单的html/php上传的网页,在安卓和电脑上都正常,但ipad上都有问题,即图片上传名称都自动变成image.jpg

WBOY
WBOYOriginal
2016-06-06 20:40:131259browse

ipad的safari或chrome都是这样的问题。

<code>html</code><code><!--表单-->
<div id="upload">
<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file">
    <br>
    <input type="submit" name="submit" value="Submit">
</form>
</div>
</code>
<code>php</code><code>// upload_file.php
<?php error_reporting(E_ERROR| E_PARSE);
session_start();
$name=$_SESSION["free_print_name"];
$phone=$_SESSION["free_print_phone"];
if (!file_exists("/var/www/upload_files/".$phone)) {
    mkdir("/var/www/upload_files/".$phone);
}
$DIR="/var/www/upload_files/".$phone."/";
$temp_array=explode('.', $_FILES["file"]["name"]);
$postfix=$temp_array[count($temp_array)-1];
if (
    ($postfix=="pdf"
        || $postfix == "doc"
        || $postfix == "docx"
        || $postfix == "rtf"
        || $postfix == "ppt"
        || $postfix == "jpg"
        || $postfix == "jpeg"
        || $postfix == "png"
        || $postfix == "psd")
    && ($_FILES["file"]["size"] < 20000000)) {
    if ($_FILES["file"]["error"] > 0) {
        // echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        if (file_exists($DIR . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
            header('Location: http://59.78.7.9/index.html');
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], $DIR. $_FILES["file"]["name"]);
            echo "Stored in: " . $DIR . $_FILES["file"]["name"];
            header('Location: http://59.78.7.9/index.html');
        }
    }
} else {
    echo "Invalid file, you can't upload files of ".$postfix;
}
?>
</code>

回复内容:

ipad的safari或chrome都是这样的问题。

<code>html</code><code><!--表单-->
<div id="upload">
<form action="upload_file.php" method="post" enctype="multipart/form-data">
    <label for="file">Filename:</label>
    <input type="file" name="file" id="file">
    <br>
    <input type="submit" name="submit" value="Submit">
</form>
</div>
</code>
<code>php</code><code>// upload_file.php
<?php error_reporting(E_ERROR| E_PARSE);
session_start();
$name=$_SESSION["free_print_name"];
$phone=$_SESSION["free_print_phone"];
if (!file_exists("/var/www/upload_files/".$phone)) {
    mkdir("/var/www/upload_files/".$phone);
}
$DIR="/var/www/upload_files/".$phone."/";
$temp_array=explode('.', $_FILES["file"]["name"]);
$postfix=$temp_array[count($temp_array)-1];
if (
    ($postfix=="pdf"
        || $postfix == "doc"
        || $postfix == "docx"
        || $postfix == "rtf"
        || $postfix == "ppt"
        || $postfix == "jpg"
        || $postfix == "jpeg"
        || $postfix == "png"
        || $postfix == "psd")
    && ($_FILES["file"]["size"] < 20000000)) {
    if ($_FILES["file"]["error"] > 0) {
        // echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    } else {
        echo "Upload: " . $_FILES["file"]["name"] . "<br>";
        echo "Type: " . $_FILES["file"]["type"] . "<br>";
        echo "Size: " . ($_FILES["file"]["size"] / 1024) . " Kb<br>";
        echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";
        if (file_exists($DIR . $_FILES["file"]["name"])) {
            echo $_FILES["file"]["name"] . " already exists. ";
            header('Location: http://59.78.7.9/index.html');
        } else {
            move_uploaded_file($_FILES["file"]["tmp_name"], $DIR. $_FILES["file"]["name"]);
            echo "Stored in: " . $DIR . $_FILES["file"]["name"];
            header('Location: http://59.78.7.9/index.html');
        }
    }
} else {
    echo "Invalid file, you can't upload files of ".$postfix;
}
?>
</code>

  1. 这就对了。ipad里的照片根本就没有“文件名”这个概念。
  2. 存储文件时也不要文件名一样就丢弃啊……文件存储又不是抢占式的……
  3. 同名文件加后缀,或者干脆整个文件计算SHA-1后用校验值做文件名。
  4. 判断文件类型不能依赖文件的扩展名,而要使用exif_imagetype()函数检查实际文件头。

请问这个问题怎么解决的?我是苹果手机上传图片名称默认就改成了image.jpg. 我们用的是HTML5.安卓和浏览器上传都正常。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn