emediastudios Posted November 5, 2007 Share Posted November 5, 2007 I have this code that works good, just have to implement some filters. How would i change this code if i wanted to add 3 images instead of just the 1. I have input fields and fields in my database called photo1, phot02 and photo3. <?php require_once('../Connections/gcproperty.php'); ?> <?php //This is the directory where images will be saved $target = "../images/news/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $headline=$_POST['headline']; $link=$_POST['link']; $content=$_POST['content']; $photo1=($_FILES['photo'1]['name']); //Writes the photo to the server if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) { //Tells you if its all ok print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES ('$headline','$photo1','$content','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 i changed my code to this. <?php require_once('../Connections/p2w.php'); ?> <?php //This is the directory where images will be saved $target = "../news/"; $target = $target . basename( $_FILES['photo1']['name']); //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server if(move_uploaded_file($_FILES['photo1']['tmp_name'], $target)) { //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Only uploads the one file, i know why but how i get it to do the 3? Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 loop your move upload function Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Im a biginner in php could you please explain how i do that. sorry Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ ect....... } } Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Thanks for your help. I did as you said and get this error Parse error: syntax error, unexpected $end in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 39 My code looks like this now <?php require_once('../Connections/p2w.php'); ?> <?php //This is the directory where images will be saved $target = "../news/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } ?> Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; $target = $target . basename( $_FILES['photo']['name']); //This gets all the other information from the form $headline=$_POST['headline']; $contents=$_POST['contents']; $photo1=($_FILES['photo1']['name']); $photo2=($_FILES['photo2']['name']); $photo3=($_FILES['photo3']['name']); $title1=$_POST['title1']; $title2=$_POST['title2']; $title3=$_POST['title3']; $link=$_POST['link']; //Writes the photo to the server for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } ?> you missed } Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 I pasted your code over mine and now i get this. Warning: move_uploaded_file(../news/) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 18 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Windows\TEMP\php1CB6.tmp' to '../news/' in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 18 Sorry, there was a problem uploading your file. Warning: move_uploaded_file(../news/) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 18 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Windows\TEMP\php1CC7.tmp' to '../news/' in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 18 Sorry, there was a problem uploading your file. Warning: move_uploaded_file(../news/) [function.move-uploaded-file]: failed to open stream: Permission denied in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 18 Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move 'C:\Windows\TEMP\php1CC8.tmp' to '../news/' in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 18 Sorry, there was a problem uploading your file. ??? Thanks for your help Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 check your folder or dir mod bu sure its writable (777) Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 6, 2007 Share Posted November 6, 2007 check your folder or dir mod bu sure its writable (777) lol you don't need to chmod files on windows.. Your server might now allow that function, or its disabled. Never seen that happen on a windows server to be honest. Check if the directory your moving it to exists. Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 The directory exists. and i have other upload files that work. this is strange. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 check your folder or dir mod bu sure its writable (777) lol you don't need to chmod files on windows.. Your server might now allow that function, or its disabled. Never seen that happen on a windows server to be honest. Check if the directory your moving it to exists. I have this code that works good, just have to implement some filters. How would i change this code if i wanted to add 3 images instead of just the 1. I have input fields and fields in my database called photo1, phot02 and photo3. Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Changed this a little as i have this code in another site <?php for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; if(move_uploaded_file($_FILES[$photo . $x]['tmp_name'], $target)){ ?> Gave me this error Sorry, there was a problem uploading your file.Sorry, there was a problem uploading your file.Sorry, there was a problem uploading your file. could there b a problem here $target = $target . basename( $_FILES['photo']['name']); Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 i should have seen that lol put that inside the loop for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 i should have seen that lol put that inside the loop for ($x=1;$x<=3;$x++){ $photo ='photo'.$x; $target = $target . basename( $_FILES[$photo]['name']); if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; //Writes the information to the database mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; } else { //Gives and error if its not echo "Sorry, there was a problem uploading your file."; } } Works Thanks heaps, Im so gratefull Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 6, 2007 Author Share Posted November 6, 2007 Found a problem. It inserts the record into sql 3 times. Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 6, 2007 Share Posted November 6, 2007 what do you expect its inside the loop? just move it outside if you want it to insert once Quote Link to comment 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.