duanfu7994 2012-04-22 18:00
浏览 66
已采纳

下面的isset()函数是否正确放置?

I have this code below and the problem is that it is not uploading the file and the call back javascript function at the bottom isn't displaying the result. Now before I entered in the if isset ($_POST[fileImage'])... code, and the $_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];, then the files uploaded perfectly.But with those 2 pieces of code, it has stopped the uploading from happening.

So I believe the problem is that the code above I have used have been placed in the wrong places. How can the code below be placed correctly in the right places for the uploading to start working again?

Below is the code which is suppose to upload the files but fails:

  <?php

    $result = 0;

       if (isset($_POST['fileImage']) && ((($_FILES["fileImage"]["type"] == "image/gif")
    || ($_FILES["fileImage"]["type"] == "image/jpeg")
    || ($_FILES["fileImage"]["type"] == "image/pjpeg")
    || ($_FILES["fileImage"]["type"] == "image/jpg")
    || ($_FILES["fileImage"]["type"] == "image/png")
    || ($_FILES["fileImage"]["type"] == "image/tif"))){

    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.stopImageUpload(<?php echo $result;?>);
</script>   

          <?php

    }

      $_SESSION['fileImage']['name'] = $_FILES['fileImage']['name'];

    ?>
  • 写回答

2条回答 默认 最新

  • dongnai1876 2012-04-22 18:43
    关注

    I see that you are using using both $_FILES["fileImage"] and $_POST['fileImage'] and am not sure that is correct .

    The way you have also written does not make it clean enough to easily identify errors .. you can use this

    Try

    <?php
    
    $result = 0;
    $errors = array ();
    $dirImage = "ImageFiles/";
    
    $allowedTypes = array (
            "image/jpeg",
            "image/gif",
            "image/pjpeg",
            "image/jpg",
            "image/png",
            "image/tif" 
    );
    
    if (isset ( $_FILES ['fileImage'] ) && $_FILES ["fileImage"] ["error"] == UPLOAD_ERR_OK) {
    
        $fileName = $_FILES ['fileImage'] ['name'];
        $fileSize = $_FILES ['fileImage'] ['size'];
        $fileTemp = $_FILES ['fileImage'] ['tmp_name'];
        $fileType = $_FILES ['fileImage'] ['type'];
    
        $fileExt = pathinfo ( $fileName, PATHINFO_EXTENSION );
        $fileExt = strtolower ( $fileExt );
    
        if (empty ( $fileName ))
            continue;
    
        if (! in_array ( $fileType, $allowedTypes )) {
            $errors [] = "File Type is not accepted";
    
        }
    
        if(!is_writable($dirImage ))
        {
            $errors [] = "File Destination not writeable";
        }
    
    
        $fileDst = $dirImage . DIRECTORY_SEPARATOR . $fileName;
        $filePrifix = basename ( $fileName, "." . $fileExt );
        $i = 0;
        while ( file_exists ( $fileDst ) ) {
            $i ++;
            $fileDst = $dirImage . DIRECTORY_SEPARATOR . $filePrifix . "_" . $i . "." . $fileExt;
    
        }
        // Move the file
    
        if (count ( $errors ) == 0) {
            if (move_uploaded_file ( $fileTemp, $fileDst )) {
                $result = 1;
            }
        }
    
    }
    
    $_SESSION ['fileImage'] ['name'] = $_FILES ['fileImage'] ['name'];
    ?>
    

    Please note that the errors are stored in $errors variable and what i advice is for you ro convert it to json can call it via your JavaScript code

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable