弹出带有来自php文件的消息的框

时间:2019-04-25 10:14:59

标签: javascript php

我一直在研究php文件,并希望显示文件是否已上传

我尝试过:

if (move_uploaded_file($file_tmp, $file)) {
    echo "<script type='text/javascript'>alert('File sucessfully uploaded');</script>";
} else {
    echo "<script type='text/javascript'>alert('Upload failed');</script>";
}

但是它不会产生弹出窗口。但是我可以在开发人员选项的响应下看到它。知道我该如何解决吗?

1 个答案:

答案 0 :(得分:0)

您必须在alert();内打印valid html。参见以下示例:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Upload</title>
</head>
<body>
<form name="form" method="post" enctype="multipart/form-data" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">
Upload File: <input type="file" size="30" id="userfile" name="userfile">
<?php
$upload_dir = $_SERVER['DOCUMENT_ROOT'] . "/upload/";
if (!is_dir($upload_dir)) { 
  @mkdir($upload_dir, 0755, true);
}
if (isset($_FILES['userfile'])) {
  $temp_name = $_FILES['userfile']['tmp_name'];
  $file_name = $_FILES['userfile']['name'];
  $file_path = $upload_dir.$file_name;
}
if ((isset($_FILES['userfile'])) && (is_uploaded_file($_FILES['userfile']['tmp_name']))) {
  if (@move_uploaded_file($temp_name, $file_path)) {
    @chmod($file_path,0755);
    echo "<script type='text/javascript'>alert('File sucessfully uploaded');</script>";
  } else {
    echo "<script type='text/javascript'>alert('Upload failed');</script>";
  }
}
?>
<input type="submit" name="submit" value="Upload">
</form>
</body>
</html>

这个例子对我来说很好。我希望这会有所帮助。