PHP uploader uploads 1 file only - php

I have a uploader in my page that is design to work from iPad and have 8 upload boxes.
When i upload pictures in the uploader, only one files is saved.
How can i get it to save all the pictures that gets uploaded?
folder is set to 777.
The Page
PHP:
<?
if ( isset( $_POST['submit'] ) ){
foreach ($_FILES['file']['name'] as $_key => $_value){
if($_FILES['file']['size'][$_key] !==0){
$randomizer = rand(0000000, 9999999);
$f=$randomizer.$_FILES['file']['name'][$_key];
echo $f;
echo "<br>";
$t=$_FILES['file']['tmp_name'][$_key];
echo $t;
echo "<br>";
//echo $_FILES['file']['size'][$_key]."<br />";
$file_mb = round(($_FILES["file"]["size"][$_key] / 1048576), 2);
echo "Size: " . $file_mb . " Mb<br />";
echo $_FILES['file']['type'][$_key]."<br />";
if( file_exists($t) ){ move_uploaded_file($t, 'upload/' . $f); }
//move_uploaded_file($_FILES['file']['tmp_name'][$_key], "upload"/$_FILES['file']['name'][$_key]);
echo "ok file";
echo "<br>";
}
}
}
?>
<form id="main_form" action="" method="POST" enctype="multipart/form-data">
<? for($i = 0; $i < 9; $i++) {
echo "<input type=\"file\" name=\"file[]\" /> <br>";
}
?>
<input type="submit" name="submit" value="send">
</form>
HTML:
<input type="file" name="image1" /><br /> <br /> <br />
<input type="file" name="image2" /><br /> <br /> <br />
<input type="file" name="image3" /><br /> <br /> <br />
<input type="file" name="image4" /><br /> <br /> <br />
<input type="file" name="image5" /><br /> <br /> <br />
<input type="file" name="image6" /><br /> <br /> <br />
<input type="file" name="image7" /><br /> <br /> <br />
<input type="file" name="image8" /><br /> <br /> <br />

Related

Image not uploading to phpmyadmin

Im having some trouble uploading an image with php to phpmyadmin.
Here is my markup so far.
<form method="post">
<p>Event Date<span> - eg "23" for the 23rd</span><br /> <input type="text" name="eventDate" maxlength="2" required /> </p>
<p>Event Month<span> - eg "Mar" for March</span> <br /><input type="text" name="eventMonth" maxlength="3" required /> </p>
<p>Artist Image <br /><input type="file" name="artistImage" required /> </p>
<p>Artist Name <br /><input type="text" name="artistName" maxlength="30" required /> </p>
<p>Location <br /><input type="text" name="location" maxlength="30" required /> </p>
<p>Price <br /><input type="text" name="price" maxlength="10" required /> </p>
<p>Time <br /><input type="text" name="time" maxlength="10" required /> </p>
<input id="eventSubmit" type="submit" name="submitEvent" />
</form>
and here is my php code
`
if(isset($_POST['submitEvent'])){
$addEventDate = $_POST['eventDate'];
$addEventMonth = $_POST['eventMonth'];
$addEventName = $_POST['artistName'];
$addEventLocation = $_POST['location'];
$addEventPrice = $_POST['price'];
$addEventTime = $_POST['time'];
$addEventImg = $_FILES['artistImage']['name'];
$temp_name1 = $_FILES['artistImage']['tmp_name'];
move_uploaded_file($temp_name1, "img/$addEventImg");
$ticket= 1;
$insert_product = "INSERT INTO events (eventDate,eventMonth,artistImg,artistName,location,price,time,buyTicket) VALUES ('$addEventDate','$addEventMonth','$addEventImg','$addEventName','$addEventLocation','$addEventPrice','$addEventTime','$ticket')";
$run_product = mysqli_query($con,$insert_product);
echo mysqli_error($con);
if($run_product){
echo "<script>alert('Product has been inserted.');</script>";
echo "<script>window.open('admin.php','_self');</script>";
}
}
?>`
I have set the database up to take the image as a blob here is a picture of my database
and this is the code that is rendering the products onto the events page.
<?php
$get_events = "select * from events";
$run_events = mysqli_query($con,$get_events);
while($row_events = mysqli_fetch_array($run_events)){
$eventDate = $row_events['eventDate'];
$eventMonth = $row_events['eventMonth'];
$artistImg = $row_events['artistImg'];
$artistName = $row_events['artistName'];
$location = $row_events['location'];
$price = $row_events['price'];
$time = $row_events['time'];
$buyTicket = $row_events['buyTicket'];
echo "
<div class='eventBox'>
<div class='dateBanner'>
<p><span>$eventDate</span><br />$eventMonth</p>
</div>
<img src='img/$artistImg' alt='event image' />
<div class='eventDetails'>
<h2>$artistName</h2>
<p><span>Location</span> <br /> $location<br /><br /><span>Ticket Price</span> <br /> £$price<br /><br /><span>Time</span> <br />$time<br /><br /></p>
</div>
<div class='PurchaseTicket'>
<h3>Buy Ticket</h3>
</div>
</div>
";
}
?>
This works for everything apart from the image. The image just displays the alt tag not the actual image. It also doesn't save the image in the folder either.
If anyone could help I'd really appreciate it!!
Thanks

Multiple MYSQL update with PHP

can someone please tell me how to modify this code so that instead of it it adding a new field to the observation table i would like it to update the table instead where the username is equal to :user and the child_ID is equal to :child_id
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
//initial query
$query = "INSERT INTO observation ( username, elg1, elg2, elg3, elg4, elg5,elg6, elg7, elg8, elg9, elg10, elg11, elg12, elg13, elg14, elg15, elg16, elg17, col1, col2, col3, child_id ) VALUES ( :user, :elg1, :elg2, :elg3, :elg4, :elg5, :elg6, :elg7, :elg8, :elg9, :elg10, :elg11, :elg12, :elg13, :elg14, :elg15, :elg16, :elg17, :col1, :col2, :col3, :child_id )";
//Update query
$query_params = array(
':user' => $_POST['username'],
':elg1' => $_POST['elg1'],
':elg2' => $_POST['elg2'],
':elg3' => $_POST['elg3'],
':elg4' => $_POST['elg4'],
':elg5' => $_POST['elg5'],
':elg6' => $_POST['elg6'],
':elg7' => $_POST['elg7'],
':elg8' => $_POST['elg8'],
':elg9' => $_POST['elg9'],
':elg10' => $_POST['elg10'],
':elg11' => $_POST['elg11'],
':elg12' => $_POST['elg12'],
':elg13' => $_POST['elg13'],
':elg14' => $_POST['elg14'],
':elg15' => $_POST['elg15'],
':elg16' => $_POST['elg16'],
':elg17' => $_POST['elg17'],
':col1' => $_POST['col1'],
':col2' => $_POST['col2'],
':col3' => $_POST['col3'],
':child_id' => $_POST['child_id']
);
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add post!" . $ex->getMessage();
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Post Successfully Added!";
echo json_encode($response);
} else {
?>
<h1>Add Comment</h1>
<form action="elg.php" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />
elg1:<br />
<input type="text" name="elg1" placeholder="elg1" />
<br /><br />
elg2:<br />
<input type="text" name="elg2" placeholder="elg2" />
<br /><br />
elg3:<br />
<input type="text" name="elg3" placeholder="elg3" />
<br /><br />
elg4:<br />
<input type="text" name="elg4" placeholder="elg4" />
<br /><br />
elg5:<br />
<input type="text" name="elg5" placeholder="elg5" />
<br /><br />
elg6:<br />
<input type="text" name="elg6" placeholder="elg6" />
<br /><br />
elg7:<br />
<input type="text" name="elg7" placeholder="elg7" />
<br /><br />
elg8:<br />
<input type="text" name="elg8" placeholder="elg8" />
<br /><br />
elg9:<br />
<input type="text" name="elg9" placeholder="elg9" />
<br /><br />
elg10:<br />
<input type="text" name="elg10" placeholder="elg10" />
<br /><br />
elg11:<br />
<input type="text" name="elg11" placeholder="elg11" />
<br /><br />
elg12:<br />
<input type="text" name="elg12" placeholder="elg12" />
<br /><br />
elg13:<br />
<input type="text" name="elg13" placeholder="elg13" />
<br /><br />
elg14:<br />
<input type="text" name="elg14" placeholder="elg14" />
<br /><br />
elg15:<br />
<input type="text" name="elg15" placeholder="elg15" />
<br /><br />
elg16:<br />
<input type="text" name="elg16" placeholder="elg16" />
<br /><br />
elg17:<br />
<input type="text" name="elg17" placeholder="elg17" />
<br /><br />
col1:<br />
<input type="text" name="col1" placeholder="col1" />
<br /><br />
col2:<br />
<input type="text" name="col2" placeholder="col2" />
<br /><br />
col3:<br />
<input type="text" name="col3" placeholder="col3" />
<br /><br />
child_id:<br />
<input type="text" name="child_id" placeholder="child_id" />
<br /><br />
<input type="submit" value="Add Comment" />
</form>
<?php
}
?>
You can essentially use the same statement. But update the syntax to an UPDATE statement.
$query = "UPDATE observation SET elg1=:elg1,elg2=:elg2,[etc]
where child_id=:child_id and user=:user";
Try this:
<?php
//load and connect to MySQL database stuff
require("config.inc.php");
if (!empty($_POST)) {
//initial query
$query = "UPDATE observation SET elg1 = :elg1, elg2 = :elg2, elg3 = :elg3, elg4 = :elg4, elg5 = :elg5, elg6 = :elg6, elg7 = :elg7, elg8 = :elg8, elg9 = :elg9, elg10 = :elg10, elg11 = :elg11, elg12 = :elg12, elg13 = :elg13, elg14 = :elg14, elg15 = :elg15, elg16 = :elg16, elg17 = :elg17, col1 = :col1, col2 = :col2, col3 = :col3 WHERE username = :user AND child_id = :child_id";
//Update query
$query_params = array(
':user' => $_POST['username'],
':elg1' => $_POST['elg1'],
':elg2' => $_POST['elg2'],
':elg3' => $_POST['elg3'],
':elg4' => $_POST['elg4'],
':elg5' => $_POST['elg5'],
':elg6' => $_POST['elg6'],
':elg7' => $_POST['elg7'],
':elg8' => $_POST['elg8'],
':elg9' => $_POST['elg9'],
':elg10' => $_POST['elg10'],
':elg11' => $_POST['elg11'],
':elg12' => $_POST['elg12'],
':elg13' => $_POST['elg13'],
':elg14' => $_POST['elg14'],
':elg15' => $_POST['elg15'],
':elg16' => $_POST['elg16'],
':elg17' => $_POST['elg17'],
':col1' => $_POST['col1'],
':col2' => $_POST['col2'],
':col3' => $_POST['col3'],
':child_id' => $_POST['child_id']
);
//execute query
try {
$stmt = $db->prepare($query);
$result = $stmt->execute($query_params);
}
catch (PDOException $ex) {
// For testing, you could use a die and message.
//die("Failed to run query: " . $ex->getMessage());
//or just use this use this one:
$response["success"] = 0;
$response["message"] = "Database Error. Couldn't add post!" . $ex->getMessage();
die(json_encode($response));
}
$response["success"] = 1;
$response["message"] = "Post Successfully Added!";
echo json_encode($response);
} else {
?>
<h1>Add Comment</h1>
<form action="elg.php" method="post">
Username:<br />
<input type="text" name="username" placeholder="username" />
<br /><br />
elg1:<br />
<input type="text" name="elg1" placeholder="elg1" />
<br /><br />
elg2:<br />
<input type="text" name="elg2" placeholder="elg2" />
<br /><br />
elg3:<br />
<input type="text" name="elg3" placeholder="elg3" />
<br /><br />
elg4:<br />
<input type="text" name="elg4" placeholder="elg4" />
<br /><br />
elg5:<br />
<input type="text" name="elg5" placeholder="elg5" />
<br /><br />
elg6:<br />
<input type="text" name="elg6" placeholder="elg6" />
<br /><br />
elg7:<br />
<input type="text" name="elg7" placeholder="elg7" />
<br /><br />
elg8:<br />
<input type="text" name="elg8" placeholder="elg8" />
<br /><br />
elg9:<br />
<input type="text" name="elg9" placeholder="elg9" />
<br /><br />
elg10:<br />
<input type="text" name="elg10" placeholder="elg10" />
<br /><br />
elg11:<br />
<input type="text" name="elg11" placeholder="elg11" />
<br /><br />
elg12:<br />
<input type="text" name="elg12" placeholder="elg12" />
<br /><br />
elg13:<br />
<input type="text" name="elg13" placeholder="elg13" />
<br /><br />
elg14:<br />
<input type="text" name="elg14" placeholder="elg14" />
<br /><br />
elg15:<br />
<input type="text" name="elg15" placeholder="elg15" />
<br /><br />
elg16:<br />
<input type="text" name="elg16" placeholder="elg16" />
<br /><br />
elg17:<br />
<input type="text" name="elg17" placeholder="elg17" />
<br /><br />
col1:<br />
<input type="text" name="col1" placeholder="col1" />
<br /><br />
col2:<br />
<input type="text" name="col2" placeholder="col2" />
<br /><br />
col3:<br />
<input type="text" name="col3" placeholder="col3" />
<br /><br />
child_id:<br />
<input type="text" name="child_id" placeholder="child_id" />
<br /><br />
<input type="submit" value="Add Comment" />
</form>
<?php
}
?>

Storing customisable quiz questions and answers in a .txt file using PHP

I have a customisable quiz site, where the user can input questions and answers. My first PHP file takes the inputted information and generates a quiz page. The answers are in hidden text box. A second PHP then compares the inputted radio button to the hidden text box to see if the user is correct and gives a score in relation to that. I would like to store the questions and answers in a .txt file which could then be accessed by other users, unfortunately as part of the project we are not able to use MySQL. Hopefully I am being clear, I have tried to go about this many ways but all have broken my code. Any ideas would be much appreciated.
My code is below:
INITIAL HTML:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<form action="actions.php" method="post">
<strong>Question 1:</strong> <input name="q1" type="text" /> <br />
Answer 1: <br />
<input name="a11" type="text" /><input name="1" type="radio" value="1.1" /><br />
Answer 2: <br />
<input name="a12" type="text" /><input name="1" type="radio" value="1.2" /><br />
Answer 3: <br />
<input name="a13" type="text" /><input name="1"type="radio" value="1.3" /><br />
Answer 4: <br />
<input name="a14" type="text" /><input name="1" type="radio" value="1.4" />
<br /><br />
<strong>Question 2:</strong> <input name="q2" type="text" /> <br />
Answer 1: <br />
<input name="a21" type="text" /><input name="2" type="radio" value="2.1" /><br />
Answer 2: <br />
<input name="a22" type="text" /><input name="2" type="radio" value="2.2" /><br />
Answer 3: <br />
<input name="a23" type="text" /><input name="2"type="radio" value="2.3" /><br />
Answer 4: <br />
<input name="a24" type="text" /><input name="2" type="radio" value="2.4" />
<br /><br />
<strong>Question 3:</strong> <input name="q3" type="text" /> <br />
Answer 1: <br />
<input name="a31" type="text" /><input name="3" type="radio" value="3.1" /><br />
Answer 2: <br />
<input name="a32" type="text" /><input name="3" type="radio" value="3.2" /><br />
Answer 3: <br />
<input name="a33" type="text" /><input name="3"type="radio" value="3.3" /><br />
Answer 4: <br />
<input name="a34" type="text" /><input name="3" type="radio" value="3.4" />
<input name="" type="submit" />
</form>
</body>
FIRST PHP:
<html>
<body>
<form action="actions2.php" method="post">
<strong><?php echo $_POST["q1"];?></strong>
<br /><br />
<?php
echo $_POST["a11"]."<input name='newa1' type='radio' value='1.1' />"."<br />".$_POST["a12"]."<input name='newa1' type='radio' value='1.2' />"."<br />".$_POST["a13"]."<input name='newa1' type='radio' value='1.3' />"."<br />".$_POST["a14"]."<input name='newa1' type='radio' value='1.4' />"."<br />";
echo "<br /><br />"
?>
<strong><?php echo $_POST["q2"];?></strong>
<br><br>
<?php
echo $_POST["a21"]."<input name='newa2' type='radio' value='2.1' />"."<br />".$_POST["a22"]."<input name='newa2' type='radio' value='2.2' />"."<br />".$_POST["a23"]."<input name='newa2' type='radio' value='2.3' />"."<br />".$_POST["a24"]."<input name='newa2' type='radio' value='2.4' />"."<br />";
echo "<br /><br />"
?>
<strong><?php echo $_POST["q3"];?></strong>
<br><br>
<?php
echo $_POST["a31"]."<input name='newa3' type='radio' value='3.1' />"."<br />".$_POST["a32"]."<input name='newa3' type='radio' value='3.2' />"."<br />".$_POST["a33"]."<input name='newa3' type='radio' value='3.3' />"."<br />".$_POST["a34"]."<input name='newa3' type='radio' value='3.4' />"."<br />";
echo "<br /><br />"
?>
<input name="result1" type="hidden" value="<?php echo $_POST['1']; ?>">
<input name="result2" type="hidden" value="<?php echo $_POST['2']; ?>">
<input name="result3" type="hidden" value="<?php echo $_POST['3']; ?>">
<input name="" type="submit" />
</form>
<br /><br />
</body>
</html>
SECOND PHP:
<?php
$correctAnswer1 = $_POST['result1'];
$newAnswer1 = $_POST['newa1'];
$correctAnswer2 = $_POST['result2'];
$newAnswer2 = $_POST['newa2'];
$correctAnswer3 = $_POST['result3'];
$newAnswer3 = $_POST['newa3'];
$score = 0;
if($newAnswer1 == $correctAnswer1) {
$score++;
}
if($newAnswer2 == $correctAnswer2) {
$score++;
}
if($newAnswer3 == $correctAnswer3) {
$score++;
}
if ($score >= 2)
{
echo "Congratulations, you did well. You got ".$score;
}
else if ($score == 1)
{
echo "Only one, poor show";
}
else
{
echo "YOU GOT ZERO YOU SHITE";
}
?>
I'm not totally sure what you wanted, but if you need a way to store the questions and answers you could look into XML or JSON.
PHP should have libraries to encode/decode both of them
I am not sure if I have understood the question pretty well, but this is how you store in a .txt file using php.
http://bd1.php.net/file_put_contents also take a look at this http://www.w3schools.com/php/func_filesystem_fwrite.asp

html-php form code not executing

I am trying to execute this html form code, the php script is not executing properly. Can you point out the problems?
<form action="form.php" method="post">
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
php script
<?php
if (isset($_POST['submit']))
{
$k=0;
foreach($_POST['languages'] as $language) {
echo $language = htmlentities($language);
echo $title = $_POST['titles'][$k];
$k++;
}
}
?>
HTML
<form action="form.php" method="post">
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
<p>
<input type="text" name="titles[]" /><br />
<textarea name="languages[]" ></textarea><br />
</p>
<input type="submit" name="submit" id="submit" value="Submit">
</form>
PHP
<?php
if (isset($_POST['submit']))
{
foreach($_POST['languages'] as $key => $language) {
echo $language = htmlentities($language);
echo " -- ";
echo $title = $_POST['titles'][$key];
echo "<br/>";
}
}
?>

warning in mkdir and how to display images from the directory created

My program is supposed to create a folder for the uploaded images on the directory but gives this warning:
mkdir() [function.mkdir]: File exists in C:\XAMP\xampp\htdocs\gallery\uploader3.php on line 26
Here is the code:
<html>
<head>
<title> Sample1 - File Upload on Directory </title>
</head>
<body>
<div align="center">
<form action="uploader3.php" method="post" enctype="multipart/form-data" >
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Create an Album (limited to 10 images): <br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<input type="file" name="uploadedfile[]" /><br />
<br />
<input type="submit" value="Upload File" />
</form>
</div>
<?php
$target_path = "uploads1/";
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
else
{
for($count = 0; $count < count($_FILES['uploadedfile']); $count++)
{
$target_path = $target_path . basename( $_FILES['uploadedfile']['name'][$count]);
if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'][$count], $target_path))
{
echo "The file ". basename( $_FILES['uploadedfile']['name'][$count]).
" has been uploaded";
}
else{
echo "There was an error uploading the file, please try again!";
}
}
}
?>
</body>
</html>
Modify your codes below:
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
to:
if(!file_exist($target_path)) {
if(!mkdir($target_path))
{
die('Failed to create folders...');
}
}
That will check the folder first, if it's already exist, no need to create it again.
for your 2nd question, you need to store the uploaded image names to somewhere ( i guess DB is a good choice), Then, you can show them anywhere you want.
Or you can use below codes to search in folder and display them:
$image_files = glob("uploads1/*.jpg");
foreach($image_files as $img) {
echo "<img src='".$img."' /><br/>";
}
You should check first that the directory does not already exist before attempting to create it
if (!file_exists($target_path))
mkdir($target_path);
if (file_exists($target_path))
{
// Further processing here
}
else
{
// Could not create directory
}

Categories