emediastudios Posted November 11, 2007 Share Posted November 11, 2007 I have a good file exists code and want to add it to a different file, i tried several times but i get coninual errors. Could someone please help me and paste my code into my full file, i promise to learn from your work. i know it should be simple but im a bigginer to php. thanks for any help given <?php // strip file_name of slashes $file_name = $_FILES['photo'. $x]['name']; $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($target. $file_name) ) { echo " {$file_name} Exists"; exit(); ?> I want that in my file below, just dont know how to combine them'' <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //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; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="*********"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 11, 2007 Author Share Posted November 11, 2007 I added it to my code <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //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; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { // strip file_name of slashes $file_name = $_FILES['photo'. $x]['name']; $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($target. $file_name) ) { echo " {$file_name} Exists"; exit(); } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="5050888202"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> and i get this error Parse error: syntax error, unexpected $end in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 73 Quote Link to comment Share on other sites More sharing options...
nuxy Posted November 11, 2007 Share Posted November 11, 2007 Well, you are missing an closing bracket, I don't know if you missed that or just mistyped. <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; // strip file_name of slashes $file_name = $_FILES['photo'. $x]['name']; $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($target. $file_name) ) { echo " {$file_name} Exists"; exit(); } //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; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="*********"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 11, 2007 Author Share Posted November 11, 2007 i cant see it Quote Link to comment Share on other sites More sharing options...
nuxy Posted November 11, 2007 Share Posted November 11, 2007 i cant see it Please explain a bit more. What cant you see, the script? If so, try putting the error_reporting function in the script, it should display all the error produced. <?php error_reporting(E_ALL); require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; // strip file_name of slashes $file_name = $_FILES['photo'. $x]['name']; $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($target. $file_name) ) { echo " {$file_name} Exists"; exit(); } //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; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="*********"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 11, 2007 Author Share Posted November 11, 2007 Sorry, pasted your code, Just echos Exists when it doesnt. need to fix it up somehow, thanks for your help man Quote Link to comment Share on other sites More sharing options...
nuxy Posted November 11, 2007 Share Posted November 11, 2007 Well, if you want your script to continue executing when the file does exist, it should not exit. Try the following script. <?php error_reporting(E_ALL); require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; // strip file_name of slashes $file_name = $_FILES['photo'. $x]['name']; $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($target. $file_name) ) echo " $file_name Exists"; else { echo 'The file you requested does not exist'; exit; } //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; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="*********"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 11, 2007 Author Share Posted November 11, 2007 Get this error with your new code Notice: Undefined variable: x in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 10 Notice: Undefined index: photo in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 10 ExistsSuccess.Success.Success. Notice: Undefined variable: count in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 55 Warning: mysql_connect() [function.mysql-connect]: Access denied for user 'root'@'localhost' (using password: YES) in C:\Program Files\Apache Group\Apache2\htdocs\Prepare2win\admin\upload_news.php on line 68 cannot connect Quote Link to comment Share on other sites More sharing options...
nuxy Posted November 11, 2007 Share Posted November 11, 2007 Change the error reporting to something like E_ERROR. Then it will only display fatal errors produced by the script. <?php error_reporting(E_ERROR); require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; // strip file_name of slashes $file_name = $_FILES['photo'. $x]['name']; $file_name = stripslashes($file_name); $file_name = str_replace("'","",$file_name); if(file_exists($target. $file_name) ) echo " $file_name Exists"; else { echo 'The file you requested does not exist'; exit; } //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; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="*********"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 11, 2007 Author Share Posted November 11, 2007 ExistsSuccess.Success.Success.cannot connect ??? Quote Link to comment Share on other sites More sharing options...
emediastudios Posted November 11, 2007 Author Share Posted November 11, 2007 maybe a better idea would be to add different method of checking for file exists and puting it in this code here that works. <?php require_once('../Connections/p2w.php'); //This is the directory where images will be saved $target = "../news/"; //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; $target = "../news/" . basename( $_FILES[$photo]['name']); if (($_FILES[$photo]["type"] == "image/gif") || ($_FILES[$photo]["type"] == "image/png") || ($_FILES[$photo]["type"] == "image/jpeg") || ($_FILES[$photo]["type"] == "image/pjpeg") && ($_FILES[$photo]["size"] < 1000000)){ if(move_uploaded_file($_FILES[$photo]['tmp_name'], $target)){ //Tells you if its all ok echo "Success."; $photo=""; } } else { //Gives and error if its not ++$count; } } if ($count > 0){ echo $count .'failed to upload '; } else { //Writes the information to the database $host="localhost"; // Host name $username="root"; // Mysql username $password="5050888202"; // Mysql password $db_name="preparetowin"; // Database name $tbl_name="news"; // Table name mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); mysql_query("INSERT INTO `news` VALUES (NULL,'$headline','$contents','$photo1','$photo2','$photo3','$title1','$title2','$title3','$link')") ; print "<meta http-equiv=\"refresh\" content=\"0;URL=news_added_successfully.php\">"; } ?> Quote Link to comment Share on other sites More sharing options...
nuxy Posted November 11, 2007 Share Posted November 11, 2007 Are you using this script within a loop of some sort? Because it seems it is executing multiple times, and not once. Quote Link to comment Share on other sites More sharing options...
AndyB Posted November 11, 2007 Share Posted November 11, 2007 mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); Should be: mysql_connect($host, $username, $password) or die("cannot connect"); mysql_select_db($db_name) or die("cannot select DB"); 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.