PDA

View Full Version : Problem copying a file (moving up a folder)


Czervik
01-30-2004, 01:16 AM
Hey all,

When I try to copy an uploaded file (or an existing file, even), it only lets me copy when the destination folder is either the current or an above folder. In other words, my destination folder cannot start with "../". Here's the bit of code:


$username = str_replace(" ","",strtolower($name));
$source = "$username.jpg";
$destin = "../_new/images/description/$username.jpg"; //this folder does exist
if($_FILES["userfile"]["size"]) {
copy($_FILES['userfile']['tmp_name'], "$username.jpg") or die("nope1");
copy($source,$destin) or die("nope2");

}


In this case, the first copy works (it's uploading the current folder) but the second doesn't (going up a folder). Is this even possible to do, and if so, how do I get around it?

Thanks for any help,
-Czervik

Vladdrac
01-30-2004, 01:24 AM
I'm not sure, maybe if you entered the full path?

Czervik
01-30-2004, 01:40 AM
I tried using the path from the server (/home/[mysite]/public_html/) but that doesn't work either. I get the error:

Warning: copy(/home/[mysite]/public_html/_new/images/description/gdaggs.jpg): failed to open stream: Permission denied in /home/[mysite]/public_html/admin1/edituser.php on line 11
nope2

I don't think an absolute path would work, cause then you'd be able to move any file on any web site. I'm pretty sure it has to stay relative.

-Czervik

Viper007Bond
01-30-2004, 01:41 AM
Erm, nevermind.

Czervik
01-30-2004, 01:42 AM
Originally posted by Viper007Bond
Dunno, but why not just move your script to your root?

That's a brilliant idea! Why didn't I think of that? ;)

Thanks,

-Czervik

Viper007Bond
01-30-2004, 01:47 AM
Was that sarcastic or did you mean it?

Anyway, it works, but somewhat impractical. You should be able to use the full path - I'll look into it.

*EDIT* - No I won't, forgot I killed my server. >_<

Czervik
01-30-2004, 01:52 AM
Yeah, I meant it. It seemed so obvious, even if it's impractical. It's a quick fix for now. :)

-Czervik

Viper007Bond
01-30-2004, 02:33 AM
*nods*

If you don't have it figured out in a few days, I'll look into it.

Czervik
01-30-2004, 02:46 AM
Grrr. Now it's only taking the current folder. I could have sworn I was able to move up folders before.

Thanks viper, I've been trying this for three days now. Would it have something to do with HostRocket's configuration? I have another site on a different host that works fine when I move up folders.

-Czervik

y6y6y6
01-30-2004, 09:03 AM
Your script needs to have permission to write to the destination folder, which it typically won't. The script runs as a special user, usually "nobody". This is the same problem you'll run into if you try to upload files via a web form. The solution is to make sure the script has the right permission, which it will if a script creates the directory.

This is tricky. And from my experience the server is set up to thwart things like this. I've gotten it to work but only by pounding on it for a while. One problem is that if the script user owns a folder, you won't be able to acces it via ftp.

If you ever get a less than cludgy solution I'd very much like to hear about it.

Czervik
01-31-2004, 02:33 PM
By permission, do you mean setting chmod to 777? I've done that, but still no dice.

I'm convinced it's something to do HostRocket's configuration. I have another site on a different host that allows me to move files to any of my folders. The only thing I can think of for this is putting this script in the folder we want to upload to and redirecting it back to where I was. That's what I do now for inserting, updating and deleting mySQL tables, but we have a few different folders we need to upload to at the same time, so that doesn't seem like a good solution.

I submitted a support ticket to HostRocket. I'll let you know what they say when I get the reply.

-Czervik

Viper007Bond
01-31-2004, 03:22 PM
Yeah, this is odd. I've only messed around with writing to a subfolder from a path outside of the parent, never the other way around. It'll be good to know what's up though.

Czervik
02-04-2004, 08:16 PM
The tech support guy only suggested using the complete path (starting with /home/mysite...) but that doesn't work. I guess the only way around it is to isolate that portion of the code in its own php file in the folder where you want it uploaded, then use a header() to redirect it back to where you want to be.

-Czervik

Czervik
02-22-2004, 10:49 PM
Has anyone figured out a solution for this? This is rediculous. We have dozens of different folders to upload to, and this would require sticking the script in each folder. Granted, we could just use an include script for each, but we'd still need a file for this in each folder. There has to be a more efficient way.

-Czervik

y6y6y6
02-23-2004, 12:14 AM
My php scripts that upload files are having no problem putting the files in several folders. The folders need to be owned by the script user - probably "nobody". If you have the scripts create the folders they'll be writing to then they shouldn't have any trouble writing to them.

Czervik
02-23-2004, 03:21 PM
Thanks Jon. :D Last night I was trying to upload to a folder that wasn't there (my mistake).

What do you have your permissions set to? It looks like mine are defaulted at 755, but I have to set each individual subfolder to 777 for it to work. When I tried this a few weeks ago, I set root folder to 777. I didn't realize all the subfolders were still 755. That must've been the problem.

y6y6y6
02-23-2004, 04:57 PM
Well, I do it in a three step process. First I set the folder where the new folders will be created to 777. Then I have the script create the folders and set them to 755. Then go back and set the original folder back to 755. I think if you could just ssh in and set the owner of the folders to "nobody" that would work, but I haven't tried that.

Czervik
02-23-2004, 07:19 PM
Ok, I manually changed everything back to 755, now I'm trying to change to 777 with the script. Should this work? It's not doing it for me:

chmod("/home/dmbwall/public_html/wallpaper/title/boyd", 0777);

I still get the error:

Warning: chmod(): Operation not permitted in /home/dmbwall/public_html/_new/admin1/editwp.php on line 10

In the php manual it also has a filename, but I just want to do it for the folder that I'm uploading to (I don't need to make new folders). I would then change it back to 0755 after the upload.

Sorry for all the questions. I do research and try everything before asking for help. :)

y6y6y6
02-23-2004, 09:10 PM
The script won't have permissions to change a 755 folder that is owned by some other user.

Czervik
02-23-2004, 11:01 PM
Could you elaborate a little more, please? I'm not familiar with the "owner" stuff. The posix_getpwuid() array shows the owner as dmbwall (the ftp login name). I've tried using the chown and exec functions. What else do I need to do with the script?

Czervik
02-24-2004, 03:25 AM
Having the owner set to 'nobody' works, but I'm not able to change ownership via SSH (even though the folder it's in is 777):

bash-2.05a$ chown nobody zzz
chown: changing ownership of `zzz': Operation not permitted


The only way I could set the ownership to 'nobody' is if I use mkdir() to create a folder in a 777 folder (but I still can't use chown() to change ownership via PHP), which I think is what you do. In your three step process, do you manually do the first and third steps (with ftp or the cp) as opposed to using a script?


Once it's set to nobody, I can upload to that folder fine. So I guess my problem now is setting all the current folders to 'nobody' (which are now set to my ftp login name).

I notice now that I can't delete what I just uploaded via FTP (in the 'nobody' folder). Is this normal? I hope I can do it via PHP, but I'll work on that tomorrow.


This stuff is really confusing. :- \

y6y6y6
02-24-2004, 09:49 AM
Your scripts have to run as a user. In this case that user is "nobody". If a script is 755 and it's owned by the ftp user, then "nobody" won't be able to change it. If it's 755 and owned by "nobody" then the ftp user won't be able to change it.

You can't mix and match. Either the script can use a folder for writing, or the ftp user. Not both. Welcome to share hosting.

It seems like it shouldn't have to be this way, but I haven't found a way around it.

Czervik
02-24-2004, 12:44 PM
Is that only specific to this host? On my other host I can use both PHP and FTP to transfer files. The owners are my FTP login name and my groups are 'ftpusers'.