Jump to content

[SOLVED] chmod question


gwood_25

Recommended Posts

hello,

 

I am uploading an image to my web host and am storing a reference to the file in a mysql database. The file upload works and the reference to the image is stored in the db. However viewing the image generates an "unauthorized" error. I was told by the host that this was a problem with my php script and not setting permissions on the uploaded file via script. I have reasearched this and to the best of my knowledge I need to use the chmod command. Following is my code, at what point to I call chmod?

 

Any help is greatly appreciated.

 

$pic=($_FILES['photo']['name']);

 

if($pic != "")

{

//Writes the information to the database

mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', '$pic')")

or die(mysql_error());

 

//Writes the photo to the server

if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))

{

chmod($_FILES['photo']['tmp_name'],0755);

//Tells you if its all ok

echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the database";

}

else {

 

//Gives and error if its not

echo "Sorry, there was a problem uploading your file.";

}

}

else

{

//Writes the information to the database

mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', 'noimage.png')")

or die(mysql_error());

}

mysql_close($dbh);

?>

 

Link to comment
Share on other sites

$pic=($_FILES['photo']['name']); 

if($pic != "")
{
   //Writes the information to the database 
   mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', '$pic')")
   or die(mysql_error());
   
   //Writes the photo to the server 
   chmod($target, 0777); // chmods folder to read, write and execute for owner, groups and other
   if(move_uploaded_file($_FILES['photo']['tmp_name'], $target)) 
   { 
   chmod($_FILES['photo']['tmp_name'],0755);
   //Tells you if its all ok 
   echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the database"; 
   } 
   else { 
   
   //Gives and error if its not 
   echo "Sorry, there was a problem uploading your file."; 
   } 
chmod($target, 0755); // chmods folder to read and execute for groups and other
}
else
{
   //Writes the information to the database 
   mysql_query("INSERT INTO `brands` (Name, Weblink, Image) VALUES ('$name', '$weblink', 'noimage.png')")
   or die(mysql_error());
}
mysql_close($dbh);   
?> 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.