1

I've followed a small tutorial so that i can upload a file to a server, this file should be moved into img/profile/ and then it reads from there.

My tree is like this (img and profile are the folders):

updatepp.php icon-profile.png img and profile inside of img

I went into my folders and checked the permissions and it says i am able to write to this file.

Inside of updatepp.php is the fcode for the profilepic uploading

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

        if(in_array($file_extn, $allowed) === true){
            $file_path = 'img/profile/' . substr(md5(time()),0,10) . '.' . $file_extn;
            if(move_uploaded_file($file_temp, $file_path)){
                echo "succes " . $file_path;
                $result = $db->prepare("UPDATE users SET profile=:profile WHERE user_id=:user");
                $result->bindParam(':profile', $file_path);
                $result->bindParam(':user', $user);
                $result->execute();
            }else{
                echo "failure";
            }
        }else{
            echo 'incorrect file type. Allowed: ';
            echo implode(', ', $allowed);
        }
    }
}

The file is uploaded to the database and the message succes $file_path is shown on the screen. However when i look into the folder I see that the file has NOT been moved.

I am hosting this on a virtual ubuntu client with LAMP stack installed and i use shared folders between the host and the virtual machine.

I would assume that the folder doesn't exist, but it clearly does.

Mykola
  • 3,343
  • 6
  • 23
  • 39
Waro1234
  • 73
  • 3
  • 2
    $_SERVER['DOCUMENT_ROOT'].'img/profile/' . substr(md5(time()),0,10) . '.' . $file_extn; ... try this.. – devpro Jan 04 '16 at 12:13
  • Have you checked somewhere the image uploaded – Anto S Jan 04 '16 at 12:13
  • Possible duplicate of [move\_uploaded\_file(...): failed to open stream: No such file or directory](http://stackoverflow.com/questions/34541344/move-uploaded-file-failed-to-open-stream-no-such-file-or-directory) – Anto S Jan 04 '16 at 12:14
  • no anto, the file and directory are there – Waro1234 Jan 04 '16 at 12:16
  • May be the directory is there, but problem with defining `$file_path` follow @devpro solution – Anto S Jan 04 '16 at 12:21
  • You dont check `$_FILES['profile']['error']` anywhere, maybe the file is not actually unloading – RiggsFolly Jan 04 '16 at 12:26

1 Answers1

1

From the Docs: $_SERVER['DOCUMENT_ROOT'] is the document root directory under which your project is executing. as defined in the server's configuration file.

If your upload file destination is within the DOCUMENT_ROOT than you can declare your path as:

$file_path = $_SERVER['DOCUMENT_ROOT'].'img/profile/' . substr(md5(time()),0,10) . '.' . $file_extn;

You can also try like this:

$file_path = '../img/profile/' . substr(md5(time()),0,10) . '.' . $file_extn;
devpro
  • 16,184
  • 3
  • 27
  • 38
  • Thanks, this helped! – Waro1234 Jan 04 '16 at 12:24
  • allright, than dont forgot to mark as accepted the answer because it will help to others who also facing this kind of issue. just click on the left green tick. below the rating meter. and happy new year mate. @Waro1234 – devpro Jan 04 '16 at 12:29
  • Why should the OP "try this"? A ***good answer*** will always have an explanation of what was done and why it was done that way, not only for the OP but for future visitors to SO. – Jay Blanchard Jan 04 '16 at 13:11
  • @JayBlanchard: thank you bro.. for mentioning it, i have updated with little explanation. and u r right, for future visitor , we must need to follow put little explanation. – devpro Jan 04 '16 at 13:52