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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • douwei3863 2012-04-22 18:17

    Consider using a function to handle the upload:

    function handleImageUpload()
    {
        // was a file uploaded?
        if (!isset($_FILES['fileImage'])) {
            return;
        }
        if (!isImage($_FILES['fileImage'])) {
            // consider throwing an exception here
            return;
        }
        $savepath = getFileSavePath($_FILES['fileImage'], 'ImageFiles/')
        // handle upload
        // ...
    }
    
    function isImage($imagepath)
    {
        // return boolean 
    }
    
    function getFileSavePath($uploadpath, $destinationdirectory)
    {
        // return path to save upload without overwriting any existing file
    }
    

    Consider using pathinfo() to get information about the file. http://www.php.net/manual/en/function.pathinfo.php

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题