doushen1026 2014-03-11 21:46 采纳率: 100%
浏览 21
已采纳

使用php无法正常上传图片

I was just trying to create a simple image uploading script.

This is what I used, but it seems there is some problem and shows an error

<?php
    define("FILEREPOSITORY", "./");
    if(isset($_POST['submit'])){
        $user =$_POST['user'];
        $ext_boo = FALSE;
        $size_boo = FALSE;
        $user_boo = FALSE;
        if(strlen($user)<=0){
            echo "No Username";
            $user_boo = FALSE;
        }
        if($_FILES['picture']['size'] <=1024000){
            $size_boo = TRUE;
        }
        else{
            echo "Too large";
            $size_boo = FALSE;
        }
        if(is_uploaded_file($_FILES['picture']['tmp_name'])){
            //mime type
            switch($_FILES['picture']['type']){
                case "image/jpeg":
                    $extension = ".jpeg";
                    $ext_boo = TRUE;
                    break;
                case "image/gif":
                    $extension = ".gif";
                    $ext_boo = TRUE;
                    break;
                case "image/png":
                    $extension = ".png";
                    $ext_boo = TRUE;
                    break;
            }
            if($ext_boo && $size_boo && $user_boo){
                $result = move_uploaded_file($_FILES['picture']['tmp_name'], FILEREPOSITORY."/images/".$user."".$extension."");
                if($result)
                    echo "Uploaded";
                else
                    echo "Some problems";
            }
        echo "Wrong file type";
        }
    }
    else{
        echo "<table>
        <form enctype=\"multipart/form-data\" action=\"\" method=\"post\">
        <tr>
        <td>User:</td>
        <td><input type=\"test\" name=\"user\" /></td>
        </tr>
        <tr>
        <td>File:</td>
        <td><input type=\"file\" name=\"picture\" /></td>
        </tr>
        <input type=\"submit\" name=\"submit\" value=\"upload\" />
        </form>
        </table>";
    }
?>

Can anybody help me to find out the problem?

Error after trying var_dump($_FILES);:

array (size=1)
  'picture' => 
    array (size=5)
      'name' => string 'sample.png' (length=10)
      'type' => string 'image/png' (length=9)
      'tmp_name' => string 'C:\wamp\tmp\php74BB.tmp' (length=23)
      'error' => int 0
      'size' => int 7575
Wrong file type
  • 写回答

1条回答 默认 最新

  • douya1061 2014-03-12 03:14
    关注

    You never set $user_boo to TRUE anywhere. I switched it to start off as TRUE (assume it is true until you later check to see if it's false on line 10).

    Also on line 37 I changed the file path. You had an extra / in your upload location. Code below worked on my machine. Make sure you have the www user set to owner on whatever location you are trying to upload to and that it has permissions to write.

    <?php
        define("FILEREPOSITORY", "./");
        if(isset($_POST['submit'])){
            $user =$_POST['user'];
            $ext_boo = FALSE;
            $size_boo = FALSE;
            $user_boo = TRUE;
            if(strlen($user)<=0){
                echo "No Username";
                $user_boo = FALSE;
            }
            if($_FILES['picture']['size'] <=1024000){
                $size_boo = TRUE;
            }
            else{
                echo "Too large";
                $size_boo = FALSE;
            }
            if(is_uploaded_file($_FILES['picture']['tmp_name'])){
                //mime type
                switch($_FILES['picture']['type']){
                    case "image/jpeg":
                        $extension = ".jpeg";
                        $ext_boo = TRUE;
                        break;
                    case "image/gif":
                        $extension = ".gif";
                        $ext_boo = TRUE;
                        break;
                    case "image/png":
                        $extension = ".png";
                        $ext_boo = TRUE;
                        break;
                }
                var_dump($ext_boo,$size_boo,$user_boo);
                if($ext_boo && $size_boo && $user_boo){
                    $result = move_uploaded_file($_FILES['picture']['tmp_name'], FILEREPOSITORY."images/".$user."".$extension."");
                    if($result)
                        echo "Uploaded";
                    else
                        echo "Some problems";
                }
            echo "Wrong file type";
            }
        }
        else{
            echo "<table>
            <form enctype=\"multipart/form-data\" action=\"\" method=\"post\">
            <tr>
            <td>User:</td>
            <td><input type=\"test\" name=\"user\" /></td>
            </tr>
            <tr>
            <td>File:</td>
            <td><input type=\"file\" name=\"picture\" /></td>
            </tr>
            <input type=\"submit\" name=\"submit\" value=\"upload\" />
            </form>
            </table>";
        }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程