Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    move_uploaded_file not working

    I am trying to add a profile page to my website that allows users to add an avatar. Adding the avatar will update the users table. So far I have:

    Code:
    <?php
    include("functions/globals.php"); 
    include("databasefunctions.php");
    include("functions/userfunctions.php"); 
    
    
    
    error_reporting(E_ALL);
    
    ?>
    
    <?php session_start(); ?>
    
    <?php
    if($_SESSION['user'])
    {?>
                          
    Welcome: <?php echo $_SESSION['user']->username; ?>
                    <?php
                          }?>
                          
                          
    <?php
     
    if($_POST['submit'] == 'Upload')
    {
    //get file attributes
    $name = $_FILES['avatar']['name'];
    $tmp_name = $_FILES['avatar']['tmp_name'];
    
     
    if ($name)
    {
    //start upload process
    $location = 'avatars'; 
    foreach ($_FILES["avatar"]["error"] as $key => $error){
    	if ($error == UPLOAD_ERR_OK) {
    	$tmp_name = $_FILES["avatar"]["tmp_name"][$key];
    	$name = $_FILES["avatar"]["name"][$key];
    		 
    move_uploaded_file($tmp_name,"$location/$name");
    	}
    }
    $result = QuickQuery("UPDATE users SET imagelocation='$location' WHERE username='$username'");
    
    die("Your avatar has been uploaded <a href='view.php'>Home</a>");
    }
        else
        if(!$name)
    $error = "Please select an image";
     
    }
     
     
    ?>
    <form action='profile.php' method='post' enctype='multipart/form-data'>
    Upload:<input type='file' name='avatar'><input type='submit' name='submit' value='Upload'>
    </form>
    I am not getting any errors, but files are not being uploaded to the "avatars" folder.

    Would appreciate any help!

    Thanks!

  • #2
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Troubleshooting problems like this ...
    Insert some echoes so you can see what is happening.

    foreach ($_FILES["avatar"]["error"] as $key => $error){
    if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["avatar"]["tmp_name"][$key];
    $name = $_FILES["avatar"]["name"][$key];

    echo "tmp_name: ".$tmp_name." &nbsp;&nbsp; loc/name: ".$location."/".$name."<br />";

    move_uploaded_file($tmp_name,"$location/$name");
    }


    Report back on what you see.


    .

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I added the code and still get the same thing on submit:

    "Your avatar has been uploaded."

  • #4
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Are you saying you never saw the echoed part that you added?
    If so, that means it never got to that part of your script.

    That's a clue ...

    Keep that echo there and add another one ... let's see where the script is going ...

    if ($name)
    {
    echo "we made it through the 'if($name)' statement. <br />";


    You can add echo statements anywhere you want to troubleshoot this problem.



    .

  • #5
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This one echoed correctly. Yes, nothing on the first one you gave me echoed out anything. It only said the file was uploaded.

    What else to do?

    Thanks for helping!

  • #6
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    OK ... that means the problem is within this scripting ...

    foreach ($_FILES["avatar"]["error"] as $key => $error){
    if ($error == UPLOAD_ERR_OK) {
    $tmp_name = $_FILES["avatar"]["tmp_name"][$key];
    $name = $_FILES["avatar"]["name"][$key];

    move_uploaded_file($tmp_name,"$location/$name");
    }



    The "foreach" line is suspicious to me.
    There must not be any 'true' values for the "foreach" line ... so it does nothing.

    You put the word "avatar" in those lines.
    Can you show me the original script that you copied from somewhere.
    Where did you get this script from?


    .

  • #7
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    include("functions/globals.php"); 
    include("databasefunctions.php");
    include("functions/userfunctions.php"); 
    
    
    
    error_reporting(E_ALL);
    
    ?>
    
    <?php session_start(); ?>
    
    <?php
    if($_SESSION['user'])
    {?>
                          
    Welcome: <?php echo $_SESSION['user']->username; ?>
                    <?php
                          }?>
                          
                          
    <?php
    
    $username = $_SESSION['username']; 
    if($_POST['submit'] == 'Upload')
    {
    //get file attributes
    $name = $_FILES['avatar']['name'];
    $tmp_name = $_FILES['avatar']['tmp_name'];
    
    if ($name)
    {
    
    //start upload process
    
    $location = "avatars/$name"; 
    move_uploaded_file($tmp_name, $location);
    
    $result = QuickQuery("UPDATE users SET imagelocation='$location' WHERE username='$username'");
    
    die("Your avatar has been uploaded <a href='view.php'>Home</a>");
    }
    else
    die("Please select a file.");
    
    }
     
     
     
    ?>
    <form action='profile.php' method='post' enctype='multipart/form-data'>
    Upload:<input type='file' name='avatar'><input type='submit' name='submit' value='Upload'>
    </form>
    This is the original script. I think I made a mistake adding foreach...I think that is for adding multiple files. I only need users to add their avatar. This original code does not move the file either.

    The script came from phpacademy tutorial. I was trying!

  • #8
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by savagebeauty View Post
    I think I made a mistake adding foreach...I think that is for adding multiple files. I only need users to add their avatar.
    Then ditch it.

    Code:
    if ($_FILES["avatar"]["error"] == UPLOAD_ERR_OK)
    {
        $tmp_name = $_FILES["avatar"]["tmp_name"];
        $name = $_FILES["avatar"]["name"];
    
        if (move_uploaded_file($tmp_name,"$location/$name"))
        {
            [put your DB insert line here instead of further down the script]
            exit('Your avatar was uploaded successfully');
        }
    }

  • #9
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    <?php
    
    $username = $_SESSION['username']; 
    if($_POST['submit'] == 'Upload')
    {
    //get file attributes
    
    $name = $_FILES['avatar']['name'];
    $tmp_name = $_FILES['avatar']['tmp_name'];
    
    if ($name)
    {
    
    //start upload process
    
    }
    
    $location = "avatars/$name"; 
        if (move_uploaded_file($tmp_name,"$location/$name"))
        {
            $result = QuickQuery("UPDATE users SET imagelocation='$location' WHERE username='$username'");
            exit("Your avatar was uploaded successfully <a href='view.php'>Home</a>");
    
    }
    }
     
     
    ?>
    Tried updating this part of the code to your suggestion-- do not receive "File uploaded" now. Nothing happens at all on submit.

  • #10
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Quote Originally Posted by savagebeauty View Post
    Tried updating this part of the code to your suggestion
    Then post the complete code which you are currently trying. Showing what you've replaced without showing what with is pointless.

  • #11
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    wow dude, aren't you pleasant. Thanks to msleim who is helping me without the attitude. By the way I DID post the entire code:

    Code:
    <?php
    include("functions/globals.php"); 
    include("databasefunctions.php");
    include("functions/userfunctions.php"); 
    
    
    
    error_reporting(E_ALL);
    
    ?>
    
    <?php session_start(); ?>
    
    <?php
    if($_SESSION['user'])
    {?>
                          
    Welcome: <?php echo $_SESSION['user']->username; ?>
                    <?php
                          }?>
                          
                          
    <?php
    
    $username = $_SESSION['username']; 
    if($_POST['submit'] == 'Upload')
    {
    //get file attributes
    
    $name = $_FILES['avatar']['name'];
    $tmp_name = $_FILES['avatar']['tmp_name'];
    
    if ($name)
    {
    
    //start upload process
    
    $location = "avatars/$name"; 
    if (move_uploaded_file($tmp_name, $location));
    {
    $result = QuickQuery("UPDATE users SET imagelocation='$location' WHERE username='$username'");
    }
    exit("Your avatar has been uploaded <a href='view.php'>Home</a>");
    }
    else
    die("Please select a file.");
    
    }
     
     
     
    ?>
    <form action='profile.php' method='post' enctype='multipart/form-data'>
    Upload:<input type='file' name='avatar'><input type='submit' name='submit' value='Upload'>
    </form>

  • #12
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I've made some sort of progress. The avatar is going into the database, but not attaching to the user who is logged in. It is also not in the avatars folder. The code has two parts. 1st part is still in profile.php. Second part is in userfunctions.php

    profile.php

    Code:
    <?php
    include("functions/globals.php"); 
    include("databasefunctions.php");
    include("functions/userfunctions.php"); 
    
    
    
    error_reporting(E_ALL);
    
    ?>
    
    <?php session_start(); ?>
    
    <?php
    if($_SESSION['user'])
    {?>
                          
    Welcome: <?php echo $_SESSION['user']->username; ?>
                    <?php
                          }?>
                          
                          
    <?php
    
    $username = $_SESSION['username']; 
    if($_POST['submit'] == 'Upload')
    {
    //get file attributes
    
    $name = $_FILES['avatar']['name'];
    $tmp_name = $_FILES['avatar']['tmp_name'];
    
    if ($name)
    {
    
    //start upload process
    
    $location = "avatars/$name"; 
    if (move_uploaded_file($tmp_name, $location));
    {
    	 AddAvatar($location);
    }
    exit ("Your Avatar ". $_FILES['avatar']['name'] ." has been uploaded.\n<a href='view.php'>Home</a>");
    }
    else
    die("Please select a file.");
    
    }
     
     
     
    ?>
    <form action='profile.php' method='post' enctype='multipart/form-data'>
    Upload:<input type='file' name='avatar'><input type='submit' name='submit' value='Upload'>
    </form>
    Here is the second part in userfunctions.php

    Code:
    function AddAvatar ($location)
    {
    	global $usertable;
    	openDatabase();
    	$location = mysql_real_escape_string($location);
            $result = QuickQuery("UPDATE users SET imagelocation='{$location}'   WHERE username='{$username}'");
        closeDatabase();
    }
    Mlseim, can you see from this why the avatar is not attaching to username or moving into the avatars folder? In the database it says avatars/me.jpg which is the name of the pic

  • #13
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    I don't think you need to use tmp_name ....

    Change this:

    $tmp_name = $_FILES['avatar']['tmp_name'];

    if ($name)
    {

    //start upload process

    $location = "avatars/$name";
    if (move_uploaded_file($tmp_name, $location));


    to this:

    // comment this out for now ... $tmp_name = $_FILES['avatar']['tmp_name'];

    if ($name)
    {

    //start upload process

    $location = "avatars/$name";
    if (move_uploaded_file($name, $location));




    .

  • #14
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That didn't change anything. But I did figure out something that might help solve this. On the page where I have the avatar form, I also have the registration form. Both forms use if($_POST['submitted'] and then different values-- one is register and the other upload.

    This is why the avatar image is going into the database. If I change the upload to submit instead, it doesn't go into the database at all.

    Not sure what else to do.

  • #15
    Master Coder
    Join Date
    Jun 2003
    Location
    Cottage Grove, Minnesota
    Posts
    9,468
    Thanks
    8
    Thanked 1,085 Times in 1,076 Posts
    Show us that script.


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •