Jump to content

[SOLVED] Upload More images


emediastudios

Recommended Posts

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."; 
} 
?>

Link to comment
https://forums.phpfreaks.com/topic/76126-solved-upload-more-images/
Share on other sites

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?

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."; 
} 
?>

 

<?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 }

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

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.

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.

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']); 

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."; 
} 
}

 

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 :);):D;D

Thanks heaps, Im so gratefull

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.