Jump to content

Multiple images help


haris244808

Recommended Posts

hi there

how can i modify this code to let me upload multiple images in database?

thanks

 

/ Start a session for error reporting
session_start();

//Connect to database
require_once('../Connections/connect_to_mysql.php');

//check if the button is pressed
if(isset($_POST['submit'])){
// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");

	if (in_array($file['type'], $valid_types))
		return 1;
	    return 0;
}
// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "adminform_images/";

// Get our POSTed variables
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$comments = $_POST['comments'];
$image = $_FILES['image'];
$price = $_POST['price'];

// Sanitize our inputs
$title = mysql_real_escape_string($title);
$name = mysql_real_escape_string($name);
$surname = mysql_real_escape_string($surname);
$email = mysql_real_escape_string($email);
$phone = mysql_real_escape_string($phone);
$city = mysql_real_escape_string($city);
$comments = mysql_real_escape_string($comments);
$image['name'] = mysql_real_escape_string($image['name']);
$price = mysql_real_escape_string($price);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( $title == "" || $name == "" || $email == "" || $city == ""  || $comments == "" || $price == ""|| $image['name'] == "" )
{
	echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>";
	exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
	{
	echo "Fotografite duhet te jene te formateve jpg, jpeg, bmp, gif, ose png. <a href=login.php>Kthehu prapa</a>";
	exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
	echo "Ju lutem nderroni emrin e fotos dhe provoni perseri. <a href=login.php>Kthehu prapa</a>";
	exit;
}

	// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
	// NOTE: This is where a lot of people make mistakes.
	// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
	$sql = "INSERT INTO admins_form (title, name, surname, email, phone, city, comments, price, date_added, filename) values ('$title', '$name', '$surname', '$email', '$phone', '$city', '$comments', '$price', now(), '" . $image['name'] . "')";
	$result = mysql_query($sql) or die ("Futja e te dhenave ne databaze DESHTOI. <a href=login.php>Provo perseri</a> ");
	echo "Shtimi i te dhenave u krye me SUKSES. Klikoni per te shkuar tek <a href=../index.php>faqja kryesore</a>";
	exit;
}
else
{
	// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
	// Make sure you chmod the directory to be writeable
	echo "Shtimi i te dhenave NUK u krye me Sukses. Ju lutem provoni <a href=login.php>perseri</a>";
	exit;
}
}

Link to comment
Share on other sites

hi there

how can i modify this code to let me upload multiple images in database?

thanks

 

/ Start a session for error reporting
session_start();

//Connect to database
require_once('../Connections/connect_to_mysql.php');

//check if the button is pressed
if(isset($_POST['submit'])){
// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");

	if (in_array($file['type'], $valid_types))
		return 1;
	    return 0;
}
// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "adminform_images/";

// Get our POSTed variables
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$comments = $_POST['comments'];
$image = $_FILES['image'];
$price = $_POST['price'];

// Sanitize our inputs
$title = mysql_real_escape_string($title);
$name = mysql_real_escape_string($name);
$surname = mysql_real_escape_string($surname);
$email = mysql_real_escape_string($email);
$phone = mysql_real_escape_string($phone);
$city = mysql_real_escape_string($city);
$comments = mysql_real_escape_string($comments);
$image['name'] = mysql_real_escape_string($image['name']);
$price = mysql_real_escape_string($price);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( $title == "" || $name == "" || $email == "" || $city == ""  || $comments == "" || $price == ""|| $image['name'] == "" )
{
	echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>";
	exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
	{
	echo "Fotografite duhet te jene te formateve jpg, jpeg, bmp, gif, ose png. <a href=login.php>Kthehu prapa</a>";
	exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
	echo "Ju lutem nderroni emrin e fotos dhe provoni perseri. <a href=login.php>Kthehu prapa</a>";
	exit;
}

	// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{
	// NOTE: This is where a lot of people make mistakes.
	// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
	$sql = "INSERT INTO admins_form (title, name, surname, email, phone, city, comments, price, date_added, filename) values ('$title', '$name', '$surname', '$email', '$phone', '$city', '$comments', '$price', now(), '" . $image['name'] . "')";
	$result = mysql_query($sql) or die ("Futja e te dhenave ne databaze DESHTOI. <a href=login.php>Provo perseri</a> ");
	echo "Shtimi i te dhenave u krye me SUKSES. Klikoni per te shkuar tek <a href=../index.php>faqja kryesore</a>";
	exit;
}
else
{
	// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
	// Make sure you chmod the directory to be writeable
	echo "Shtimi i te dhenave NUK u krye me Sukses. Ju lutem provoni <a href=login.php>perseri</a>";
	exit;
}
}

 

here is the form:

 

   	  <p>Te gjithe fushat qe jane te shoqeruara me nje yll (*) duhen plotesuar!</p>
      <form action="adminform_submit.php" method="post" enctype="multipart/form-data" name="frm" id="frm">
        <table width="980" border="0" cellpadding="6" cellspacing="0" class="inputfield">
          <tr>
            <th scope="row">Title:</th>
            <th colspan="2" scope="row"><span id="sprytextfield1">
            <input type="text" name="title" id="title" tabindex="5" />
            <span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shkronjave.</span><span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span></span>
              *</th>
          </tr>
          <tr>
            <th scope="row"><label>Emri:
              
            </label></th>
            <th colspan="2" scope="row"><span id="sprytextfield2">
            <input type="text" name="name" id="name" tabindex="10" />
            <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal shkronjave.</span></span>
              *</th>
          </tr>
          <tr>
            <th scope="row"><label>Mbiemri:
              
            </label></th>
            <th colspan="2" scope="row"><input type="text" name="surname" id="surname" tabindex="20" /></th>
          </tr>
          <tr>
            <th scope="row"><label>Email:
              
              </label></th>
            <th colspan="2" scope="row"><span id="sprytextfield3">
            <input type="text" name="email" id="email" tabindex="30" />
            <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldInvalidFormatMsg">Shkruani emailin ne formen e duhur.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shkronjave.</span></span>
              *</th>
          </tr>
          <tr>
            <th scope="row"><label>Nr. Kontaktues:
              
            </label></th>
            <th colspan="2" scope="row"><input type="text" name="phone" id="phone" tabindex="40" /></th>
          </tr>
          <tr>
            <th scope="row"><label>Qyteti:
              
            </label></th>
            <th colspan="2" scope="row"><span id="sprytextfield4">
            <input type="text" name="city" id="city" tabindex="50" />
            <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shkronjave.</span></span>
              *</th>
          </tr>
          <tr>
            <th scope="row"><label>Informatat
              e<br />
hollesishme: </label></th>
            <th colspan="2" scope="row"><span id="sprytextarea1">
              <textarea name="comments" id="comments" cols="45" rows="5" tabindex="60"></textarea>
              <span class="textareaRequiredMsg">Kjo zbrazetire duhet plotesuar.</span></span>
              *</th>
          </tr>
          <tr>
            <th scope="row"><label>Fotografite:
              
            </label></th>
            <th colspan="2" scope="row"><input type="file" name="image" id="image" tabindex="70" />
              *</th>
          </tr>
          <tr>
            <th scope="row"><label>Cmimi:
              
            </label></th>
            <th colspan="2" scope="row"><span id="sprytextfield5">
            <input type="text" name="price" id="price" tabindex="80" size="10"/>
            <span class="textfieldRequiredMsg">Kjo zbrazetire duhet plotesuar.</span><span class="textfieldMaxCharsMsg">Keni kaluar numrin maksimal te shifrave te lejuara.</span><span class="textfieldInvalidFormatMsg">Ju lutem shkruani cmimin ne formen e duhur.</span></span>
              *</th>
          </tr>
          <tr>
            <th scope="row"><input type="submit" name="submit" id="submit" value="Shtoj te dhenat" tabindex="90" /></th>
            <th scope="row"> </th>
            <th scope="row" align="right"><a href="logout.php">Dalja</a></th>
          </tr>
        </table>
      </form> 

Link to comment
Share on other sites

You're not really asking for help are you?

 

This isn't your code you're trying to modify. Understand the code above, and you should understand how to make it work for multiple files.

 

r u kidding?

of course its not all mine. I seen a lot of codes than i modified one. I understand all the code and i know i should use a loop to make multiple file upload, but whatever i tried it wont work so i asked here for help.

and isnt ur job to help us?

Link to comment
Share on other sites

exactly when u r volunteer u help otherwise u dont need to reply, because its php forum not for chatting :)

dont misunderstand. this is the truth

 

LMAO Ok. Lets see how much help you get telling people they have to help you.

 

BTW..you could use an array to store the multiple files

Link to comment
Share on other sites

exactly when u r volunteer u help otherwise u dont need to reply, because its php forum not for chatting :)

dont misunderstand. this is the truth

 

LMAO Ok. Lets see how much help you get telling people they have to help you.

 

BTW..you could use an array to store the multiple files

 

haha thanks.

btw i just got one :P

Link to comment
Share on other sites

a help man :)

 

Let's just say I'm feeling generous  :shrug:

 

Just telling you. Not everyone would help with that approach. Notice he hasn't posted again?

 

Anyway... make your form element an array, and add a loop around the processing for each file in the input array

Link to comment
Share on other sites

No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help

 

To echo, just loop through the directory where they're stored and display them

Link to comment
Share on other sites

No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help

 

To echo, just loop through the directory where they're stored and display them

 

aha ok. however when i put the code to this loop: foreach ($_FILES["image"]["error"] as $key => $error) {} and make the name of the box with [] still doesnt work..

The table i use its normal one that contains 4 columns for the picture .

 

Link to comment
Share on other sites

No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help

 

To echo, just loop through the directory where they're stored and display them

 

aha ok. however when i put the code to this loop: foreach ($_FILES["image"]["error"] as $key => $error) {} and make the name of the box with [] still doesnt work..

The table i use its normal one that contains 4 columns for the picture .

 

Can you show all your relevant code?

Link to comment
Share on other sites

No extra fields would be needed if I understand you correctly. Each image would be stored as a new record. Depends on your table structure though. If you can post that it might help

 

To echo, just loop through the directory where they're stored and display them

 

aha ok. however when i put the code to this loop: foreach ($_FILES["image"]["error"] as $key => $error) {} and make the name of the box with [] still doesnt work..

The table i use its normal one that contains 4 columns for the picture .

 

Can you show all your relevant code?

 

 

here is the php:

[m]<?php
//check if the button is pressed
if(isset($_POST['submit'])){

//Connect to database
require_once('../Connections/connect_to_mysql.php');




foreach ($_FILES["image"]["error"] as $key => $error) {

// Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
	// This is an array that holds all the valid image MIME types
	$valid_types = array("image/jpg", "image/jpeg", "image/bmp", "image/gif", "image/png");

	if (in_array($file['type'], $valid_types))
		return 1;
	    return 0;
}
// This variable is the path to the image folder where all the images are going to be stored
// Note that there is a trailing forward slash
$TARGET_PATH = "adminform_images/";


$image = $_FILES['image'];

$image['name'] = mysql_real_escape_string($image['name']);

// Build our target path full string.  This is where the file will be moved do
// i.e.  images/picture.jpg
$TARGET_PATH .= $image['name'];

// Make sure all the fields from the form have inputs
if ( $image['name'] == "" )
{
	echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>";
	exit;
}

// Check to make sure that our file is actually an image
// You check the file type instead of the extension because the extension can easily be faked
if (!is_valid_type($image))
	{
	echo "Fotografite duhet te jene te formateve jpg, jpeg, bmp, gif, ose png. <a href=login.php>Kthehu prapa</a>";
	exit;
}

// Here we check to see if a file with that name already exists
// You could get past filename problems by appending a timestamp to the filename and then continuing
if (file_exists($TARGET_PATH))
{
	echo "Ju lutem nderroni emrin e fotos dhe provoni perseri. <a href=login.php>Kthehu prapa</a>";
	exit;
}

	// Lets attempt to move the file from its temporary directory to its new home
if (move_uploaded_file($image['tmp_name'], $TARGET_PATH))
{


		// Get our POSTed variables
$title = $_POST['title'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$city = $_POST['city'];
$comments = $_POST['comments'];
$price = $_POST['price'];

// Sanitize our inputs
$title = mysql_real_escape_string($title);
$name = mysql_real_escape_string($name);
$surname = mysql_real_escape_string($surname);
$email = mysql_real_escape_string($email);
$phone = mysql_real_escape_string($phone);
$city = mysql_real_escape_string($city);
$comments = mysql_real_escape_string($comments);
$price = mysql_real_escape_string($price);

	if ( $title == "" || $name == "" || $email == "" || $city == ""  || $comments == "" || $price == "")
{
	echo "Te gjithe fushat duhet plotesuar. <a href=login.php>Provo perseri</a>";
	exit;
}
	// NOTE: This is where a lot of people make mistakes.
	// We are *not* putting the image into the database; we are putting a reference to the file's location on the server
	$sql = "INSERT INTO admins_form (title, name, surname, email, phone, city, comments, price, date_added, filename) values ('$title', '$name', '$surname', '$email', '$phone', '$city', '$comments', '$price', now(), '" . $image['name'] . "')";
	$result = mysql_query($sql) or die ("Futja e te dhenave ne databaze DESHTOI. <a href=login.php>Provo perseri</a> ");
	echo "Shtimi i te dhenave u krye me SUKSES. Klikoni per te shkuar tek <a href=../index.php>faqja kryesore</a>";
	exit;
}
else
{
	// A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
	// Make sure you chmod the directory to be writeable
	echo "Shtimi i te dhenave NUK u krye me Sukses. Ju lutem provoni <a href=login.php>perseri</a>";
	exit;
}







}
}

?>[/m]

 

 

and here is the input file for the image:

 

<input type="file" name="image[]" id="image" tabindex="70" />

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.