尝试检索文件名时,文件未上载

时间:2012-04-19 20:33:17

标签: php javascript jquery ajax

我正在尝试从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;
        }

下面是上传文件的php脚本(imageupload.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;

    }

1 个答案:

答案 0 :(得分: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>