doukun8944 2016-04-23 04:38
浏览 152
已采纳

move_uploaded_file无效,但没有错误

I have been having an issue with my code, specifically with the move_uploaded_file. I changed the folder I keep the images in's permissions to 777 to make sure it wasn't a problem with the permissions. I also read a php manual on how to use move_uploaded_file of w3schools.com. I have run out of ideas on how to upload my image to a folder using php. Please help.

Here is the portion of the code with the move_uploeaded_file:

<?php
    if (@$_GET['action'] == "ci"){
        echo "<form action='account.php?action=ci' method='POST' enctype='multipart/form-data'><br />
        Available file extention: <stong>.PNG .JPG .JPEG</stong><br /><br />
        <input type='file' name='image' /><br />
        <input type='submit' name='change_pic' value='Change' /><br />
        </form>";
        if (isset($_POST['change_pic'])) {
            $errors = array();
            $allowed_e = array('png', 'jpg', 'jpeg');

            $file_name = $_FILES['image']['name'];
            $file_e = strtolower(pathinfo($file_name, PATHINFO_EXTENSION));
            $file_s = $_FILES['image']['size'];
            $file_tmp = $_FILES['image']['tmp_name'];

            if(in_array($file_e, $allowed_e) === false) {
                $errors[] = 'This file extension is not allowed.';
            }

            if ($file_s > 2097152) {
                $errors[] = 'File size must be under 2MB';
            }

            if (empty($errors)) {
                move_uploaded_file($file_tmp, '../images/'.$file_name);
                $image_up = '../images/'.$file_name;
                $check = mysqli_query($connect, "SELECT * FROM users WHERE usename='".@$_SESSION['username']."'");
                $rows = mysqli_num_rows($check);

                while($row = mysqli_fetch_assoc($check)) {
                    $db_image = $row['profile_pic'];
                }
                if($query = mysqli_query($connect, "UPDATE users SET profile_pic = '".$image_up."' WHERE username='".$_SESSION['username']."'"))
                    echo "You have successfuly changed your profile picture!";
        } else {
                foreach($errors as $error) {
                    echo $error, '<br />';
                }
            }
        }
    }
?>
  • 写回答

1条回答 默认 最新

  • douxie2029 2016-04-23 05:37
    关注

    Here's the last chunk of the code, slightly rewritten. move_uploaded_file returns a boolean, so we can test if it's true or false by setting up a variable $result:

            if (empty($errors)) {
                $image_up = 'images/'.$file_name;
                $result = move_uploaded_file($file_tmp, $image_up);
    
                if($result){
                    //this line had a typo usename -> username
                    //Also, you should change this over to using parameters and binding values ASAP. This leaves you open to hacking.
                    $check = mysqli_query($connect, "SELECT * FROM users WHERE username='".@$_SESSION['username']."'");
                    $rows = mysqli_num_rows($check);
    
                    while($row = mysqli_fetch_assoc($check)) {
                        $db_image = $row['profile_pic'];
                    }
                    $q = "UPDATE users SET profile_pic = '".$image_up."' WHERE username='".$_SESSION['username']."'";
                    if($query = mysqli_query($connect, $q)){
                        echo "You have successfuly changed your profile picture!";
                        }
                } else {
                    echo "Upload failed.";
                }
        } else {
                foreach($errors as $error) {
                    echo $error, '<br />';
                }
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM