Forum Moderators: coopster

Message Too Old, No Replies

php image upload

trouble with permissions of files uploaded through php

         

eliddell

12:53 pm on May 11, 2006 (gmt 0)

10+ Year Member



hi ya'll

i am a neo phite here so bare with me...

i created an html form and php script that allows me to upload images to my server... seems to be working accept when the file gets to the server, the permissions keep getting changed so that the image is an "un authorized url" meaning even a simple <img src=""> tag can not be used to display the image.. i checked the chmod of the folder and the php script and they are all set to read, write, and execute for all three groups..(ie. user, owner, group all have full read, write, and execute.. well that is until the image is uploaded through my php script..)

anyway.. here is the php script i am using.. idealy i want the script to take the user selected file.. upload it to the same directory as the php script and name it bio1.jpg...

<?php
// Where the file is going to be placed
$target_path = "uploads/";
/* Add the original filename to our target path. Result is "uploads/filename.extension" */
$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

// This is how we will get the temporary file...
$_FILES['uploadedfile']['tmp_name'];
$target_path = "bio1.jpg";

//$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded and saved as the image for bio 1";
} else{
echo "There was an error uploading the file, please make sure that the file is a jpg, 384 pixels wide and 286 pixels high, then try again!";
}
?>
<img src="bio1.jpg">

ahmedtheking

2:18 pm on May 11, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



At the beginning of your PHP script, add this:

unlink("filename.jpg");

This deletes the file before you write a new one! PS make sure the dir you're uploading to is 777

[edited by: ahmedtheking at 2:27 pm (utc) on May 11, 2006]

mgm_03

2:18 pm on May 11, 2006 (gmt 0)

10+ Year Member



I think if you google on "file upload tutorial" you will find plenty of well-used code that you can learn from. The code will have additional tips to involve in your fil upload script such as checking the type of file, size, etc. Or, check your favorite PHP script repository.

As you get more into PHP, learn from others' code so you do not have to waste time re-inventing a wheel each time.

eliddell

3:50 pm on May 11, 2006 (gmt 0)

10+ Year Member



like i said ... i set all the chmods to 777.. the folder is 777 the html is 777 the php is 777 the jpg is 777... heck i even tried erasing the image from the server so it doesn't already exist..

the weird thing is after i check all this and they are all 777.. i goto the html form... upload a file and bang. its there but the chmod to the html changes to 666 chmod the thimage changes to 600 and the only file that remains 777 is the php script!

please help!

jatar_k

3:55 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld eliddell,

can you use chmod [php.net] on the image right after you upload it? the script should be the owner and can set the proper permissions right at that time.

eliddell

4:13 pm on May 11, 2006 (gmt 0)

10+ Year Member



thanks j thats what i was looking for. let me test and get back to you

"yo! you down with php!"
"yeah! you know me!"

sorry progrmaing ruins humor!

jatar_k

4:23 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



I could hear the song in my head while reading that

hehe ;)

eliddell

4:26 pm on May 11, 2006 (gmt 0)

10+ Year Member



awe man :(

its not working

here is how my script looks currently

<?php

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$_FILES['uploadedfile']['tmp_name'];
$target_path = "bio1.jpg";

chmod ("bio1.jpg", 0777);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded and saved as the image for bio 1";
} else{
echo "There was an error uploading the file, please make sure that the file is a jpg, 384 pixels wide and 286 pixels high, then try again!";
}
?>
<img src="bio1.jpg">

unfortunately its still changing the chmod of the bio1.jpg to 600..

what am i doing wrong
?

jatar_k

4:31 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



are you getting the success message? or the error? for the move_uploaded_file

eliddell

4:35 pm on May 11, 2006 (gmt 0)

10+ Year Member



i am getting a success.. the file is being writen exactly where i want it.. but the chmod keeps getting set to 600

erik

jatar_k

4:38 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



then you need to move your chmod down in the script

you need to chmod it after it is moved

eliddell

5:22 pm on May 11, 2006 (gmt 0)

10+ Year Member



i am.. aren't i? please look at the script again

<?php
$mode ="777";

$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

$_FILES['uploadedfile']['tmp_name'];

$target_path = "bio1.jpg";
echo $mode;

chmod("bio1.jpg", $mode);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded and saved as the image for bio 1";
} else{
echo "There was an error uploading the file, please make sure that the file is a jpg, 384 pixels wide and 286 pixels high, then try again!";
}
?>
<img src="bio1.jpg">

jatar_k

5:29 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



try adding it again after the if else for move_uploaded

before you try the img tag

eliddell

5:45 pm on May 11, 2006 (gmt 0)

10+ Year Member



you rock! it works! thanks for being the one person who actually read my post and realized i was checking my folder permissions ect.. damn that was killing my brain cells.. so let me ask this.. it works now but how come it mattered that it was before the "if" statements and not after?

erik

jatar_k

5:47 pm on May 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



move_uploaded_file actually moves the file from the place it was uploaded to the target

this is a new file and sometimes the permissions get changed in that new location

so you need to chmod the final destination because that is the file you are trying to call with the image tag

your error is permissions on that file/dir/user whatever

that make sense?