Jump to content

Add code to my code


emediastudios

Recommended Posts

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\">";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389091
Share on other sites

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\">";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389095
Share on other sites

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\">";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389103
Share on other sites

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\">";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389107
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389112
Share on other sites

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\">";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389114
Share on other sites

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\">";

}
?>

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389118
Share on other sites

	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");

Link to comment
https://forums.phpfreaks.com/topic/76852-add-code-to-my-code/#findComment-389436
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.