我有一个简单的文件上传表单,它发布到 iframe,我从 iframe 检索值并重定向到另一个页面。
问题是表单没有发布到 iframe(我不认为),所以没有任何 php 正在执行,并且 null 正在返回到 javascript。
HTML 代码
<form id="upload" name="upload" enctype="multipart/form-data" method="post" action="index.php" target="upload_target">
<input name="folder" type="hidden" value="<?php echo $folder ?>" />
<input name="filename" type="hidden" value="<?php echo $filename ?>" />
<input name="uploaded_file" type="file" size="5120" id="uploaded_file" accept="image/jpeg" />
<input id="sent" name="sent" type="submit" value="Upload" />
</form>
<iframe id="upload_target" name="upload_target" style="width:10px; height:10px; display:none"></iframe>
Javascript 从 iframe 中获取值
$('form#upload').submit(function(){
$('#upload_wrapper').hide();
$('#loading').show();
// get the uploaded file name from the iframe
$('#upload_target').unbind().load( function()
{
var img = $('#upload_target').contents().find('#filename').html();
alert(img); //returning null
$('#loading').hide();
});
});
和页面顶部的PHP上传文件
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$targetFolder = $_POST['folder'] . "/";
$filename2 = $_POST['filename'];
if (!is_dir($targetFolder))
{
mkdir($targetFolder, 0777);
}
$tempFile = $_FILES['uploaded_file']['tmp_name'];
$targetPath = dirname(__FILE__) . '/' . $targetFolder;
$targetFile = rtrim($targetPath,'/') . '/' . $_FILES['uploaded_file']['name'];
$uploadfile = $targetFolder. basename($filename2 .".jpg");
move_uploaded_file($tempFile,$uploadfile);
$fileName = $uploadfile;
echo "<div id='filename'>$fileName</div>";
}
这段代码之前一直在工作,所以我不确定为什么它现在停止了,请帮忙。