【问题标题】:Unable to move uploaded image无法移动上传的图片
【发布时间】:2013-07-15 09:12:42
【问题描述】:

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

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

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

下面是调用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>

【问题讨论】:

  • 确保目录存在。此外,请检查您的服务器日志以了解发生了什么
  • 目录存在,我在本地主机上这样做

标签: php


【解决方案1】:

检查一下,

echo getcwd()

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

【讨论】:

    【解决方案2】:

    删除 '/' 登录你的 $file_path

     $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/

    【讨论】:

    • 真的吗?你的图像目录在哪里? '/Applications/XAMPP/xamppfiles/htdocs/core/images'?
    • 你的 'images' 文件夹在 'functions' 文件夹中吗?
    • 没有我的图像文件夹在主目录 /Applications/XAMPP/xamppfiles/htdocs/
    • 如果您写$file_path = '/images/profile' . substr(md5(time()), 0, 15) . '.' . $file_extn;,则表示您将php放入的同一目录中存在“images”文件夹。我更新了答案,请尝试。
    猜你喜欢
    • 2015-01-08
    • 1970-01-01
    • 2019-05-31
    • 2020-11-27
    • 2023-03-16
    • 2020-03-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多