Forum Moderators: coopster

Message Too Old, No Replies

problem moving images once uploaded - do I not have permision?

move_uploaded_file not working

         

ScottD

9:46 am on Feb 5, 2006 (gmt 0)

10+ Year Member



I'm trying to se PHP to upload images from a form. The image seems to go up okay to the temp directory, but then it doesn't get moved across.

I'm new to PHP and just learning, but I had help from someone with a lot of experience yesterday and we still couldn't solve it. It seems it may be that I just don't have permision to put files on the site in this way.

Is there some way I can check this? Is there a way to change this?

The hosting is with bravenet, and I was very happy with them until now. When I tried to find detailed information on their site it was impossible. I've opened a ticket but they have not replied so far.

The PHP code I was using was basically like this:

<?php

$uploaddir = '/var/www/uploads/';
$uploadfile = $uploaddir . basename($_FILES['userfile']['name']);

echo '<pre>';
if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {
echo "File is valid, and was successfully uploaded.\n";
} else {
echo "Possible file upload attack!\n";
}

echo 'Here is some more debugging info:';
print_r($_FILES);

print "</pre>";

?>

As a result I get a screen with the $_FILES array, and the error code is 0, so the file was uploaded.

I really appreciate any thoughts you may have. Thanks! Scott

dreamcatcher

11:51 am on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Scott,

The first question is, do you have write permissions on the directory you are moving the image to?

Set your error reporting to E^ALL as well at the top of your script:

error_reporting(E^ALL);

See what happens.

dc

ScottD

4:28 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



Hi DC

That is the question I suppose - do I have permisions? I don't know.

I tried adding the line of code you suggested but nothing came of it. Then I removed the whole "if" statement and just used the "move" command directly, leaving your line of code in, but it just showed a blank screen. And of course, no file appeared in my directory...

I suspect I just don't have the permision, but that is really annoying. Do you know a good host that definately does allow this? Or do you know if there is another way around this?

dreamcatcher

6:18 pm on Feb 5, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can you not chmod the upload dir to 777? This can usually be done via your CP or an FTP program.

dc

ScottD

6:27 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



ermmm.. chmod?

JollyK

6:59 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



ScottD, "chmod" is a Unix/Linux-type command that "CHanges the MODe" of a file such that different users and/or groups of users can read, write, or execute ("run," as in a program) a file.

If your server is Unix-based in some way (Unix, Linux, Solaris, BSD, etc, etc) then files are "owned" by different users. If a file is owned by your user ID, then my user ID cannot modify that file unless the mode is changed to give me permission to do so.

Here's the problem: If you're uploading files via PHP, then the files are most likely "owned" by the user ID that the web server software runs under. This is almost certainly not your user ID, but rather, is something like, "apache" or "nobody" or "www."

Thus, when you upload files, they're owned by, say "www". When you then try to copy them into a directory on your site, that directory is most likely owned by YOUR user ID, (I'll call it "scottd," but it's whatever it is on that server), so that "www" cannot modify it by putting files in there.

To "chmod" a directory 777 means read/write/execute for the owner (in this case scottd), read/write/execute for the group (I won't get into this, but certain users are grouped into user groups), and read/write/execute for the "world" or everyone. Generally, your directories would be "chmod" 755, which is to say, only the owner can write to it (put files in, take files out, etc) but everyone else can read files in there. You have to let everyone read files so that the web server user can retrieve and display those files to people browsing your site.

Okay. So.

Most FTP or file upload programs, or heck, even website control panels have something where you can change permissions on a file or directory. You might want to run a google search on "how do I chmod" or similar or "how do I chmod with [your upload program]" to find out how to do it. It's often a matter of making sure that "read/write/execute" are checked for owner, group, and world/other/everyone (sometimes the "world" setting is presented as "other" or "everyone" or similar).

If you chmod the directory 777, then the "www" user will be able to put files in there, even if the directory is owned by scottd.

And yes, there are some security issues inherent in this, but for the most part, you have no choice if you are using PHP to upload files and you do not have full administrative control over your server, I'm afraid.

JK

JollyK

7:17 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



P.S. If you go to Bravenet's "help" link on their main website, and then go to "Website Hosting Forum" and then search for "chmod", there are a couple of posts there from people on chmod (except they spelled it "chmode"). Basically, the answer seems to be that you need to change it with your FTP program, and they have a link on how to do chmod with Cute FTP and WS_FTP. I searched their other forums, and didn't find any way built into Bravenet to do it.

JK

ScottD

7:32 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



JollyK you are awesome. And dreamcatcher I really appreciate your help too. Excuse my ignorance, and thanks!

JollyK

8:22 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



No excuse needed, ScottD: everyone has to start somewhere!

:-)

JK

ScottD

9:01 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



hmmm...I still seem to be having problems. I'm using dreamweaver so I looked up how to chmod with that and found this:

go to window/results/ftplog and type in the command bar

site chmod 777 /path/to/your/file

I got that to work, or so it seemed judging by the message that appeared in the window below. So should that mean that the permissions are permanently changed? Or is it a momentary thing?

Not knowing the above answer I saw I could use chmod in PHP so I wrote this:

chmod("/path/to/file/", 777);

and included it before the PHP script to move images.

Needless to say it didn't work...so I put this in to see if the chmod was working:

if (chmod("/bla bla directory/", 777)) {
echo "chmod ok\n";
} else {
echo "chmod not ok\n";
}

and it showed me "chmod not ok"

When trying to use the dreamweaver thing I quite often got a message saying the directory didn't exist, but occaisionally I got a positive message saying the mode had been changed.

Well...sorry for blabbing on, but I've been trying to resolve this for 3 days now!

Things I don't really understand:

1 - in dreammweaver, am I relating this ftp command to a specific file, or is it a general command not related to or stored in any file?
2 - is the change permanent once set? (unless changed by another chmod of course)

Thanks again for your enduring patience. Scott

ScottD

9:48 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



I have just written this to see what permissions the directory has:

$file = posix_getcwd();
echo substr(sprintf('%o', fileperms($file)), -4);

you see I am learning!

Anyway, the amazing thing is it returns a value....0777

so now I really don't know whats going on.

hmmm...I'm off to bed.

Scott

JollyK

11:26 pm on Feb 5, 2006 (gmt 0)

10+ Year Member



There should be a way to get PHP to tell you *why* it can't move the file. I don't really do file upload with PHP because of the user ID issues, or I'd tell you what it was. (I use PHP for database stuff, and Perl for file-based stuff since with Perl I can make sure that the script runs as ME and not the web server user, while with php, I cannot.)

As for whether "chmod" is permanent, it is supposed to stay the way you chmod it until you, or the superuser/administrator ("root" user) of the machine chmods it some other way. Some webhosts un-chmod stuff from 777 to 755 periodically automatically as a "security" feature. I have "security" in quotes because honestly, if everyone's files are owned by the web server user, it's rather like everything being 777 anyway.

But I digress.

Have you set error_reporting(E^ALL); in your script as suggested by dc above? That will generally give you more information about what may be going wrong.

Another rather slickity thing to do in this case is, rather than attempting to use move_uploaded_file(), instead, make an actual FTP connection to "127.0.0.1" (localhost), and use ftp_chdir to get into the directory you want to put the script, then use ftp_put to "upload" the file into that directory. That somewhat gets around the whole permissions thing if, in fact, it IS a permissions thing. I saw this idea in the comments on the manual page for move_uploaded_file(), so no guarantees.

On the same manual page, someone commented that Fedora Core 3 Linux may have a security function built in such that you may not be able to do image upload. I have no idea how your server is configured, though.

Definitely ask Bravenet what is going on, I think. :-)

JK

dreamcatcher

7:50 am on Feb 6, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you are getting somewhere. Usually a permissions error is generated by PHP if a file can`t be uploaded. I thought adjusting the error reporting might show it.

dc

adb64

8:45 am on Feb 6, 2006 (gmt 0)

10+ Year Member



I also had about the same problem two weeks ago and it turned out that I exceeded the upload limit. The default upload limit for PHP is 2Mb. I asked my host to increase this limit and now it is set to 6Mb and I'm able to upload larger files via a form. Maybe you encounter the same problem.

Arjan

ScottD

4:53 pm on Feb 6, 2006 (gmt 0)

10+ Year Member



Thanks again to all of you, especialy JollyK who has exceded the bounds of generosity in giving so much time and thought and for explaining things so completely that even a numskull like me can understand. I really appreciate it.

I have finally got a reply from Bravenet too (it was the weekend I suppose) and they say it should be okay, but they run on safe mode, so I think I know where to go now. If I get seriously stuck, I know where there are some great people to turn to. But I hope I can work it out on my own now.

Thanks again

Scott