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
https://forums.phpfreaks.com/topic/73526-solved-chmod-question/
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
https://forums.phpfreaks.com/topic/73526-solved-chmod-question/#findComment-370948
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.