Page 1 of 1

Uploading a file problem

Posted: Sat Feb 09, 2008 11:32 am
by RobertBr
I have a short script which is uploading a file for a database to then use. The file gets to the website but then is unreadable (in fact completely unusable, all access denied). I'm uploading to a geocities site and have tried a chmod on the file but that just produced an error. Has anyone encountered this problem, something in the PHP or something to do with geocities?

Robert

Re: Uploading a file problem

Posted: Sat Feb 09, 2008 12:39 pm
by Christopher
You need to have the upload script change the permissions after it moves the file to allow all access. The webserver runs under a different user that you log-in as. The file belongs to the webserver's user and by default is probably set to only user or user+group access -- your user has neither.

Re: Uploading a file problem

Posted: Fri Feb 29, 2008 5:46 am
by RobertBr
Thanks for the help, that did solve the problem. Unfortunately I seem to be finding the upload of files by far the most frustrating element of the code (when I feel it should be easy).

So I did get it to work once (including the change of permissions) and now it is not working again, but I don't think I actually altered anything, the code looks like this:

Code: Select all

$target = "upload/"; 
$target = $target.basename($_FILES['uploaded']['name']); 
$ok=1; 
 
echo $_FILES['uploaded']['name'];
//This is our size condition 
if ($uploaded_size > 350000) 
{ 
echo "Your file is too large.<br>"; 
$ok=0; 
} 
 
//This is our limit file type condition 
if ($uploaded_type =="text/php") 
{ 
echo "No PHP files<br>"; 
$ok=0; 
} 
 
//Here we check that $ok was not set to 0 by an error 
if ($ok==0) 
{ 
Echo "Sorry your file was not uploaded"; 
} 
 
//If everything is ok we try to upload it 
else 
{ 
if(move_uploaded_file($_FILES['uploaded']['tmp_name'], $target)) 
{ 
echo "The file ".basename( $_FILES['uploadedfile']['name'])." has been uploaded"; 
//Then we try and alter the permissions
if(!chmod($target, 0644))
 {
  echo "Error -- couldn't open file for reading!";
  exit;
 } else {
  echo "Success";
 }
} 
else 
{ 
echo "Sorry, there was a problem uploading your file."; 
} 
}