dongwan0574 2013-06-30 04:16
浏览 72
已采纳

PHP未定义索引 - 当变量中包含值时发生

I have the following code that is being used to successfully upload images:

$fileName = $_FILES['file']['name'];
$fileSize =  $_FILES['file']['size'];
$fileType = $_FILES['file']['type'];
$fileTmp = $_FILES['file']['tmp_name'];
list($origWidth, $origHeight) = getimagesize($fileTmp);

The catch is I'm getting an 'Undefined index' on each line.

I have print_r($_FILES) just before this code and each array variable exists and I've echo out varibles like $fileName and can see each has a value.

Also I have put this before this code block and it makes no difference:

$fileName = $fileSize = $fileType = $fileTmp = ''; 

Any suggestions how I can ensure these variables are defined?

thankyou

update.............................

this is the HTML code:

      <form action="http://www.domainname.com/scripts/php/processing.php?page=join&section=precrop&type=profile&token=photoToken" method="post" id="joinPhotoUploadFormProfile" enctype="multipart/form-data">
        <input type="file" name="file" class="fileProfile"><br>
      </form>

update...............................

var_dump($_FILES); output below:

enter image description here

  • 写回答

1条回答 默认 最新

  • dsx58940 2013-06-30 04:52
    关注

    Since you are getting Undefined index and the script completes the upload successfully just add the following lines to make sure that the request method is in fact POST and that the $_FILES array is set.

    if ($_SERVER['REQUEST_METHOD'] == 'POST') {
        $fileName = (isset($_FILES['file']['name']) ? $_FILES['file']['name'] : null);
        $fileSize = (isset($_FILES['file']['size']) ? $_FILES['file']['size'] : null);
        $fileType = (isset($_FILES['file']['type']) ? $_FILES['file']['type'] : null);
        $fileTmp = (isset($_FILES['file']['tmp_name']) ? $_FILES['file']['tmp_name'] : null);
    
        //the rest of your upload code
    }
    

    it also looks like you are passing a lot of variables via url processing.php?page=join&section=precrop&type=profile&token=photoToken whick would suggest that you have other conditionals in the script so make sure that your are editing the code in the right place.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料