【问题标题】:why isnt this php working, i get undefined index...?为什么这个 php 不工作,我得到未定义的索引...?
【发布时间】:2009-11-07 12:48:17
【问题描述】:

我正在尝试将图像上传到服务器。

我有一个带有几个输入的表单... 在这个表单中,我有一个 iframe,其中包含另一个用于上传的表单,这是因为我不希望原始表单提交()。

原来的形式基本上是:

      <form> ...... bla bla bla alot of inputs....<iframe src="pic_upload.html"></iframe></form>

这里是 pic_upload.html:

      </head>
      <body><form name="pic_form" id="pic_form" action="bincgi/imageUpload.php" method="post" target="pic_target"><input name="pic_file" type="file" id="pic_file" size="35" onchange="this.form.submit();"></form><div id="pic_target" name="pic_target" style="width:80px; height:80px;"></div></body>
      </html>

这里是 imageUpload.php:

      <?php
      $destination_path = getcwd().DIRECTORY_SEPARATOR;
      //echo $destination_path;
      $result = 0;
      $target_path = $destination_path . basename($_FILES['pic_file']['name']);
      if(@move_uploaded_file($_FILES['pic_file']['tmp_name'], $target_path)) {
      $result = 1;
      }
      ?>

我收到此错误:未定义索引:imageUpload.php 中第 5 行的 pic_file

另外,如果我让它工作,有没有一种好方法可以在表单的目标内显示上传的图像?

非常感谢! PS:我会将javascript添加到标签中,因为也许这就是我需要的!

【问题讨论】:

    标签: php javascript html


    【解决方案1】:

    不要忘记表单属性

    enctype="multipart/form-data"
    

    【讨论】:

      【解决方案2】:

      表格需要是这样的

      <form enctype="multipart/form-data" action="__URL__" method="POST">
      

      查看this PHP.net documentation了解更多信息。

      【讨论】:

        【解决方案3】:

        您首先需要测试$_FILES 是否有带有键pic_file 的项目。所以使用isset 函数来做到这一点:

        $destination_path = getcwd().DIRECTORY_SEPARATOR;
        //echo $destination_path;
        $result = 0;
        if (isset($_FILES['pic_file'])) {
            $target_path = $destination_path . basename($_FILES['pic_file']['name']);
            if (@move_uploaded_file($_FILES['pic_file']['tmp_name'], $target_path)) {
                $result = 1;
            }
        }
        

        【讨论】:

        • 好的,所以现在它没有给出任何错误,但是会打开一个空白屏幕......并且图像没有上传??
        猜你喜欢
        • 2015-03-23
        • 2012-02-05
        • 1970-01-01
        • 2023-03-03
        • 1970-01-01
        • 2017-03-29
        • 1970-01-01
        • 1970-01-01
        • 2016-07-31
        相关资源
        最近更新 更多