gwood_25 Posted October 16, 2007 Share Posted October 16, 2007 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73526-solved-chmod-question/ Share on other sites More sharing options...
marcus Posted October 16, 2007 Share Posted October 16, 2007 Try CHMODing the folder the files are uploaded to, make it 0777 Quote Link to comment https://forums.phpfreaks.com/topic/73526-solved-chmod-question/#findComment-370934 Share on other sites More sharing options...
gwood_25 Posted October 16, 2007 Author Share Posted October 16, 2007 ok, I am very new to php so how do I do that and do I have to do it each time I upload a file? Where in the code should the chmod command go? Quote Link to comment https://forums.phpfreaks.com/topic/73526-solved-chmod-question/#findComment-370939 Share on other sites More sharing options...
mattal999 Posted October 16, 2007 Share Posted October 16, 2007 $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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/73526-solved-chmod-question/#findComment-370948 Share on other sites More sharing options...
gwood_25 Posted October 16, 2007 Author Share Posted October 16, 2007 Thank you so much. That worked perfectly. Quote Link to comment https://forums.phpfreaks.com/topic/73526-solved-chmod-question/#findComment-370952 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.