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 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题