Uploading a file problem

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
RobertBr
Forum Newbie
Posts: 11
Joined: Fri Oct 20, 2006 1:28 pm

Uploading a file problem

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Uploading a file problem

Post 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.
(#10850)
RobertBr
Forum Newbie
Posts: 11
Joined: Fri Oct 20, 2006 1:28 pm

Re: Uploading a file problem

Post 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."; 
} 
}
Post Reply