PDA

View Full Version : testing new php script - can't delete uploaded/copied files!




brianellisrules
Mar 10, 2004, 10:38 PM
Hi everyone, it's me again.

OK, I'm working a script that'll allow users to upload a picture to my site. The idea is that the script will create a directory and then upload the picture to that directory.

It works!

However, since this is just temporary, I'd like to be able to delete the files and directories but it's just not working. I can't even rename the files that I uploaded. I don't know what's going on.

Here's my upload code:
$piccount = $_POST['piccount'];
$user = $_POST['user'];

mkdir("/home2/brian/public_html/temp/upload/$user");

// start for loop
for($x=1;$x<=$piccount;$x++){
$file_name = $_FILES['uploadfile'. $x]['name'];

// strip file_name of slashes
$file_name = stripslashes($file_name);
$file_name = str_replace("'","",$file_name);
$location = "/home2/brian/public_html/temp/upload/$user/$file_name";
$copy = copy($_FILES['uploadfile'. $x]['tmp_name'],$location);

// check if successfully copied
if($copy) {
echo "$file_name uploaded sucessfully!<br>";
}
else {
echo "<b>$file_name could not be uploaded! <a href=\"http://www.brianellisrules.com/contact.php\">Contact the admin</a>!</b><br>";
}
}
Like I said, it creates the folder and uploads the file just fine, but it's undeletable. Ugh.

It was working great until I added the functionality of uploading to a specific folder rather than the current directory....

//I changed this:
$copy = copy($_FILES['uploadfile'. $x]['tmp_name'],$file_name);
//to this:
$location = "/home2/brian/public_html/temp/upload/$user/$file_name";
$copy = copy($_FILES['uploadfile'. $x]['tmp_name'],$location);


Yeah, I'm clueless.



mnkeybsness
Mar 10, 2004, 11:14 PM
I admit that I barely know anything about PHP, but is there a way that the permissions are getting set so that no one has rights to them after they are uploaded? Is there a way to chmod the files?

brianellisrules
Mar 10, 2004, 11:22 PM
I admit that I barely know anything about PHP, but is there a way that the permissions are getting set so that no one has rights to them after they are uploaded? Is there a way to chmod the files?
I thought about that and used my ftp program to try and chmod the directories and files to see if that had anything to do with it, but it said the permission was denied and was unable to chmod them.



Now that I think about it, I wonder if I have to issue a command to close the directory after I upload to it? Cripes, I have no clue.

jeremy.king
Mar 11, 2004, 12:56 AM
You have to remember that when you upload files through PHP, they will belong to the "user" that the web server runs as (typically the apache user). If you are using Apache as your web server, you can see the user that the service runs as by looking at the httpd.conf file.

So most likely your files are uploaded with the owner being the user "apache" or something similar. Typical default permissions are 644 which means you (using your own login) would not be able to delete the files since you only have read access.

I am guessing that your account and apache don't belong to the same group, so you would need a minimum of 666 if you want to be able to delete files with a login other than apache outside of the web application. To accomplish this add the following function call BEFORE you create files in PHP.

umask(0000);

To be more proper you could

umask (0111) - which should yield permissions 666 or rw-rw-rw

For more information see
http://us3.php.net/umask

Good luck.

brianellisrules
Mar 11, 2004, 07:22 AM
You have to remember that when you upload files through PHP, they will belong to the "user" that the web server runs as (typically the apache user). If you are using Apache as your web server, you can see the user that the service runs as by looking at the httpd.conf file.

So most likely your files are uploaded with the owner being the user "apache" or something similar. Typical default permissions are 644 which means you (using your own login) would not be able to delete the files since you only have read access.

I am guessing that your account and apache don't belong to the same group, so you would need a minimum of 666 if you want to be able to delete files with a login other than apache outside of the web application. To accomplish this add the following function call BEFORE you create files in PHP.

umask(0000);

To be more proper you could

umask (0111) - which should yield permissions 666 or rw-rw-rw

For more information see
http://us3.php.net/umask

Good luck.
Ah, cool... I had no idea. Thanks, I'll give it a try!

And I'm guessing since I don't have permission to delete the files that's something my provider will have to do, eh?

jeremy.king
Mar 11, 2004, 10:24 AM
Ah, cool... I had no idea. Thanks, I'll give it a try!

And I'm guessing since I don't have permission to delete the files that's something my provider will have to do, eh?

Technically, you could write a PHP page to delete them using the unlink function, since this would execute as the owner of the file (since a PHP page created them).

Did my umask suggestion work?

brianellisrules
Mar 11, 2004, 12:40 PM
Technically, you could write a PHP page to delete them using the unlink function, since this would execute as the owner of the file (since a PHP page created them).

Did my umask suggestion work?
It did work. Although I tried 0111 and it wouldn't let me change to the directory... so I went with 0000 and it worked like a dream. Thanks again.