0

我正在尝试从 php 脚本上传文件 (imageupload.php) 的页面中检索文件名,并且我想在 javascript 函数 (QandATable.php) 中的另一个页面中显示它。但我不知道该怎么做

我将向您展示所有相关代码,以便您可以遵循它,以便您能够理解正在发生的事情。

更新:下面我将向您展示如何上传文件的步骤。下面的代码成功上传了文件。

下面是表格(QandATable.php);

var $fileImage = $("<form action='imageupload.php' method='post' enctype='multipart/form-data' target='upload_target' onsubmit='return imageClickHandler(this);' class='imageuploadform' >" + 
<label>Image File: <input name='fileImage' type='file' class='fileImage' /></label><br/><label class='imagelbl'>" + 
"<input type='submit' name='submitImageBtn' class='sbtnimage' value='Upload' /></label>" + 
"</p><ul class='listImage' align='left'></ul>" +
"<iframe class='upload_target' name='upload_target' src='#' style='width:0;height:0;border:0px;solid;#fff;'></iframe></form>"); 

在用户提交表单的同一页面上,它会进入下面的函数,它会检查验证,然后当验证清楚时,它会进入startImageUpload()函数:

 function imageClickHandler(imageuploadform){ 
      if(imageValidation(imageuploadform)){ 
          return startImageUpload(imageuploadform); 
      } 
      return false;
  }

如果没有验证,它将进入隐藏文件输入的下面的 JS 函数 (QandATable.php),并将表单提交到文件上传发生的 imageupload.php。上传文件后,它会回调 stopImageUpload() 函数(QandAtable.php),在该函数中它将显示有关文件是否已上传的消息,这就是我希望来自服务器的文件名的位置附加。

下面是 startImageUpload() 函数:

var sourceImageForm;

function startImageUpload(imageuploadform){

$(imageuploadform).find('.fileImage').css('visibility','hidden');
sourceImageForm = imageuploadform;

return true;
        }

下面是上传文件 (imageupload.php) 的 php 脚本:

     <?php

session_start();

$result = 0;

   if ($_FILES["fileImage"]["error"] > 0) {
      echo "Error: " . $_FILES["fileImage"]["error"] . "<br />";
      } else {
      echo "Upload: " . $_FILES["fileImage"]["name"] . "<br />";
      echo "Type: " . $_FILES["fileImage"]["type"] . "<br />";
      echo "Size: " . ($_FILES["fileImage"]["size"] / 1024) . " Kb<br />";
      echo "Stored in: " . $_FILES["fileImage"]["tmp_name"];
      }

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
    $_SESSION['fileImage']['name'] = $_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"],
    $_SESSION['fileImage']['name'] = $_FILES['fileImage']['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>

最后,当上传完成时,它会返回到 stopUploadImage() 函数 (QandATable.php) 以显示有关文件是否成功上传的消息。这也是我希望从服务器上传的文件名被附加的地方。

   function stopImageUpload(success){

          var result = '';
          if (success == 1){
result = '<span class="msg">The file was uploaded successfully!</span><br/><br/>';
             $('.listImage').append('<br/>');
          }
          else {
result = '<span class="emsg">There was an error during file upload!</span><br/><br/>';
          }

    return true;

    }
4

1 回答 1

1

要调用该stopImageUpload()函数,我将执行以下操作:

从:

<script language="javascript" type="text/javascript">
window.top.windo.stopImageUpload(<?php echo $result;?>);
</script>

至:

<script language="javascript" type="text/javascript">
window.top.stopImageUpload(<?php echo "'$result'";?>);
</script>

要将文件名页面传递到页面,请使用会话变量。我会建议类似于以下内容(因为您已经在使用会话):

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
    $_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];        
    $parts = explode(".",$_FILES['fileImage']['name']);
    $ext = array_pop($parts);
    $base = implode(".",$parts);
    $n = 2;  

只要确保session_start();在您想要将这些变量传递到的每个页面的顶部都有。

通过运行以下命令进行故障排除:

<?php

    if ($_FILES["file"]["error"] > 0) {
      echo "Error: " . $_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 "Stored in: " . $_FILES["file"]["tmp_name"];
      }
    ?>

为了解决您的unexpected ';'错误,我将 $_SESSION 行移到了:

<?php

session_start();

$result = 0;

   if ($_FILES["fileImage"]["error"] > 0) {
      echo "Error: " . $_FILES["fileImage"]["error"] . "<br />";
      } else {
      echo "Upload: " . $_FILES["fileImage"]["name"] . "<br />";
      echo "Type: " . $_FILES["fileImage"]["type"] . "<br />";
      echo "Size: " . ($_FILES["fileImage"]["size"] / 1024) . " Kb<br />";
      echo "Stored in: " . $_FILES["fileImage"]["tmp_name"];
      }

if( file_exists("ImageFiles/".$_FILES['fileImage']['name'])) {
    $_SESSION['fileImage']['name'] = $_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"]);
    //The error was thrown because you had $_SESSION inside the move_upload_file()
    //Can't have a ; inside a function
    //However, you need to remove this $_SESSION anyway, it is already declared above, 
    //it wouldn't work here anyway since your code breaks apart the $_FILES array.
    //$_SESSION['fileImage']['name'] = $_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>
于 2012-04-19T21:06:26.673 回答