gpt4 book ai didi

php - 通过 iframe 上传文件在 IE 中不起作用

转载 作者:行者123 更新时间:2023-11-29 22:14:08 28 4
gpt4 key购买 nike

我有两个文件iframe.phpiframe.htmliframe.php 包含我想在 iframe.html 中显示为 iframe 的文件上传表单(仅适用于 IE < 10,否则我可以使用 ajax 毫无问题地上传) .

它在 IE.10 中工作正常,但在 IE.9 中我需要在提交按钮上单击两次才能上传,而在 IE.8 中它根本不起作用并且 $output 总是返回Error: invalid file 并且还需要点击两次,我不确定是什么问题!任何帮助都会很棒。

iframe.php

<div id="uploadImage">

<form method='POST' enctype='multipart/form-data' action='<?php echo $_SERVER['PHP_SELF'] ?>'>
<input type="text" disabled placeholder="Choose an Image to upload" id="file_input" name="file_input" />
<input type="button" id="file_btn" value="" onClick="document.getElementById('file').click()" />
<input type="submit" id="upload" value="" />
<div style="width: 0; height: 0; overflow: hidden;">
<input type="file" id="file" name="file" />
</div>
</form>

<div id="properties" class="texxt">
<p>Name: <br/>
Size: max 2 MB or 2000 KB<br/>
Type: (jpg, jpeg, png, gif)
</p>
</div>

<div id="results">
<?php
$status = "Close";
$source = "";
if( isset($_FILES['file']))
{
$allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
$size = (int) $_SERVER['CONTENT_LENGTH'];

if ( (($_FILES["file"]["type"] == "image/gif") ||
($_FILES["file"]["type"] == "image/jpeg") ||
($_FILES["file"]["type"] == "image/jpg") ||
($_FILES["file"]["type"] == "image/png")) &&
in_array($extension, $allowedExts) &&
($size < (2 * 1024 * 1024)) )
{
if ($_FILES["file"]["error"] > 0)
{
$output = "Return Code: " . $_FILES["file"]["error"];
}
else
{
if (file_exists("upload/" . $_FILES["file"]["name"]))
{
$output = "Error: File already exists. Please rename your file or chose a different one";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"upload/" . $_FILES["file"]["name"]);
$status = "Add Image";
$output = "Uploaded Successfully";
$source = sprintf(" !img! %s !*img! ", $_FILES["file"]["name"]);
}
}
}
else
{
if (!in_array($extension, $allowedExts))
{
$output = "Error: Please select an image.";
}
elseif ( ($size > (2 * 1024 * 1024)) && in_array($extension, $allowedExts) )
{
$output = "Error: File size(" . $size . ") exceeds the limit of 2 mb.";
}
else
{
$output = "Error: Invalid file";
}
}
echo $output;
}

?>
</div>
<p align="center">
<input type="hidden" name="source" id="source" class="source" value="<?php echo $source ?>" />
<input type="button" id="close" class="close" value="<?php echo $status ?>" />
</p>
</div>

<script>
document.getElementById('file').onchange = function(){
var path = this.value.replace(/C:\\fakepath\\/, ''),
props = document.getElementById('properties');
props.innerHTML = props.innerHTML.replace('Name: ', 'Name: ' + path);
document.getElementById('file_input').value = path;
document.getElementById('results').innerHTML = "";
};
</script>

iframe.html

<div id="iframe" style="height: 296px; width: 481px">
</div>

<script>
var iframe = document.createElement('iframe');
iframe.src = "iframe.php";
iframe.frameBorder = "no";
iframe.allowTransparency = "true";
document.getElementById('iframe').appendChild(iframe);
</script>

最佳答案

出于安全原因,IE 不允许从 javascript 操作 type="file" 输入元素。设置文件名或调用单击事件以显示浏览器对话框将导致表单提交时出现“访问被拒绝”错误,用户必须手动单击文件输入。

关于 IE.8 中的 invalid file 错误,问题出在 $_FILES['file']['type'] 上,我遇到了这个问题我自己之前。使用 var_dump($_FILES['file']['type']) 时,您可以看到它显示的文件类型如下:

jpeg -> pjpeg
jpg -> pjpeg
png -> x-png
gif -> gif

要解决此问题,请将 x-pngpjpeg 添加到允许的文件类型中:

if ( (($_FILES["file"]["type"] == "image/gif")    ||
($_FILES["file"]["type"] == "image/jpeg") ||
($_FILES["file"]["type"] == "image/jpg") ||
($_FILES["file"]["type"] == "image/pjpeg") ||
($_FILES["file"]["type"] == "image/png") ||
($_FILES["file"]["type"] == "image/x-png")) &&
in_array($extension, $allowedExts) &&
($size < (2 * 1024 * 1024)) )
{

关于php - 通过 iframe 上传文件在 IE 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16227016/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com