我正在尝试从一个php脚本中接收一个文件的名称,该脚本位于一个页面中并将其附加到单独页面上的javascript函数中。问题是我在错误控制台中收到一个无效的正则表达式标志w错误,该错误控制器指出了未定义的变量:'fileimagename for the line:
var nameimagefile = <?php echo $fileimagename; ?>;
如何解决此错误?
以下是javascript函数(QandATable.php):
function stopImageUpload(success){
var nameimagefile = <?php echo $fileimagename; ?>;
var result = '';
if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
$('.listImage').append(nameimagefile + '<br/>');
}
else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
}
return true;
}
以下是php代码,该代码位于单独的页面(imageupload.php),其中文件名为:
<?php
session_start();
if (isset($_POST['fileimagename'])) {
$_SESSION['fileimagename'] = $_SESSION[' $_FILES['fileImage']['name']'];
}
if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
$parts = explode(".",$_FILES['fileImage']['name']);
$ext = array_pop($parts);
$base = implode(".",$parts);
$n = 2;
while( file_exists("ImageFiles/".$base."_".$n.".".$ext)) $n++;
$_FILES['fileImage']['name'] = $base."_".$n.".".$ext;
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
else
{
move_uploaded_file($_FILES["fileImage"]["tmp_name"],
"ImageFiles/" . $_FILES["fileImage"]["name"]);
$result = 1;
}
?>
<script language="javascript" type="text/javascript">window.top.window.stopImageUpload(<?php echo $result;?>);</script>
答案 0 :(得分:0)
看起来错误是说$fileimagename
未设置。
var nameimagefile = <?php echo $fileimagename; ?>;
您需要定义$fileimagename
答案 1 :(得分:0)
QandATable.php
中的某处,您应该有一个$fileimagename = $_SESSION['fileimagename']
,或者设置该变量的东西。