user2589447
user2589447

Reputation:

Unable to move uploaded image

I am getting the following errors when I upload an image:

Warning: move_uploaded_file(/images/profile767abbfeae82141.jpg) [function.move-uploaded-file]: failed to open stream: No such file or directory in /Applications/XAMPP/xamppfiles/htdocs/core/functions/users.php on line 5

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/Applications/XAMPP/xamppfiles/temp/phpATXrDx' to '/images/profile767abbfeae82141.jpg' in /Applications/XAMPP/xamppfiles/htdocs/core/functions/users.php on line 5

Here is the code of the function where move_uploaded_file is called:

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);
}

And here is where the image can be uploaded:

<?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>

Upvotes: 0

Views: 2242

Answers (2)

kayla
kayla

Reputation: 156

remove '/' sign in yor $file_path

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

Edit

change your path to

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

or move your images folder to /Applications/XAMPP/xamppfiles/htdocs/core/functions/

Upvotes: 0

Chitowns24
Chitowns24

Reputation: 960

Check this,

echo getcwd()

Should tell you where you are and can help fix that problem

Upvotes: 0

Related Questions