无法移动上传的图片

时间:2013-07-17 00:38:49

标签: php

上传图片时出现以下错误:

  

警告:move_uploaded_file(/images/profile767abbfeae82141.jpg)[function.move-uploaded-file]:无法打开流:/ Applications / XAMPP / xamppfiles / htdocs / core / functions / users中没有此类文件或目录。第5行的PHP

     

警告:move_uploaded_file()[function.move-uploaded-file]:无法将'/ Applications / XAMPP / xamppfiles / temp / phpATXrDx'移动到/ Applications / XAMPP / xamppfiles /中的'/images/profile767abbfeae82141.jpg'第5行的htdocs / core / functions / users.php

以下是调用move_uploaded_file的函数的代码:

function change_profile_image($user_id, $file_temp, $file_extn) {
    $file_path = '/images/profile' . substr(md5(time()), 0, 15) . 
                 '.' . $file_extn;
    move_uploaded_file($file_temp, $file_path);
    mysql_query("UPDATE `users` SET `profile` = '" . 
                mysql_real_escape_string($file_path) . 
                "' WHERE `user_id` = " . (int)$user_id);
}

此处可以上传图片:

<?php
    require 'core/init.php';
    if (!(isset($_SESSION['user_id']))) {
        header ("Location: index.php");
    }

    if (isset($_FILES['profile']) === true) {
        if (empty($_FILES['profile']['name']) === true) {
            echo 'please choose a file';
        } else {
            $allowed = array('jpg', 'jpeg', 'gif', 'png');
            $file_name = $_FILES['profile']['name'];
            $file_extn = explode('.', $file_name);
            $file_extn = end($file_extn);
            $file_temp = $_FILES['profile']['tmp_name'];

            if (in_array($file_extn, $allowed) === true) {
                change_profile_image($session_user_id, $file_temp, $file_extn);
                header('Location: index.php');
                exit();
            } else {
                echo 'That file type is not allowed';
            }
        }
    }
?>
<!DOCTYPE html>
<head>
    <meta charset="utf-8" />
</head>

<div class="profile">
<?php
    if (empty($user_data['profile']) === false) {
        echo '<img src="', $user_data['profile'], '" alt="',
              $user_data['first_name'],'\'s Profile Image">'; 
    }
?>    
    <form action="" method="post" enctype="multipart/form-data">
        <input type="file" name="profile"><br>
        <input type="submit">
    </form>
</div>
Hello <?php echo $user_data['first_name']; ?><br>
Your email is : <?php echo $user_data['email']; ?><br>
Your last name is: <?php echo $user_data['last_name']; ?><br>
And your username is: <?php echo $user_data['username']; ?><br>

We currently have <?php echo user_count(); ?> users.<br>

<a href="/<?php echo $user_data['username']; ?>"> Profile </a><br>
<a href="../changepassword.php"> Change Password </a><br>
<a href="../settings.php"> Settings </a><br>
<a href="logout.php"> Logout </a>
</html>

2 个答案:

答案 0 :(得分:0)

删除$ file路径中的'/'符号

 $file_path = 'images/profile' . substr(md5(time()), 0, 15) . '.' . $file_extn;

修改

将您的路径改为

$file_path = '../../images/profile' . substr(md5(time()), 0, 15) . '.' . $file_extn;

或将图片文件夹移至/Applications/XAMPP/xamppfiles/htdocs/core/functions/

答案 1 :(得分:0)

检查一下,

echo getcwd()

应该告诉你你在哪里,可以帮助解决这个问题