-1

Whenever I use move_uploaded_file to my an uploaded file, the file always ends up in my web root. What am I doing wrong? Should the path be relative to my web root, or should it be an absolute path on my file system?

Ultimately what I'm trying to do, it have a folder for php to upload/dowload files. I don't want web bots and anyone else just to be able to access the files, i want only authenticated people using my website to be able to download the files. So this is how I have my file structure laid out:

/var/www/website/public_html

and

/var/www/website/files

and my move_uploaded_file command is like this:

move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "/var/www/website/files/".$_FILES['txtFileSelector']['name']);

but no matter what i've tried, the file always ends up in /var/www/website/public_html I've even tried sending the file in other sub folders of public_html but still no luck.

1
  • 1
    use move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], getcwd() . DIRECTORY_SEPARATOR . 'files/'.$_FILES['txtFileSelector']['name']); Oct 30, 2014 at 6:23

1 Answer 1

0

ah-ha! Destination path is relative!

So the solution for me is:

echo move_uploaded_file($_FILES['txtFileSelector']['tmp_name'], "../files/".$_FILES['txtFileSelector']['name']

Because of the relative pathing, use .../ to go up from the web root, and then move it to the desired storage folder.

CORRECTION

Absolute path or relative path either will work. It was a combination of folder permissions (www-data needs to either be owner or group member with read/write permissions) and me being an idiot and discovering a programming bug. My code was in a php class and the uploading was function. In my constructor I had a bug in my code. When doing OO there's a big difference between

$upload_dir = "/path/to/upload";

and

$this->upload_dir = "/path/to/upload";
1
  • The path is relative if you use a relative path. Since in your original post you're using an absolute path (starting with /), the path should be absolute. Something's weird here!
    – deceze
    Oct 31, 2014 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.