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

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 />';
                }
            }
        }
    }
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题