Jump to content

Recommended Posts

i have an image upload system for a travel agency.

 

the form has 4 file fields where u can browse and add your images for upload.

 

when you press upload, the script automatically uploads each file, creates a thumbnail and then add the path to mysql.

 

the problem is that i can't add all the files to mysql, just the first one.

 

i believe i need a simillar loop or something..

 

here are parts of the code:

 

for($i=0; $i<10; $i++)
  if(!empty($_FILES["upload$i"]['name']))
  {
     $pic[$i] = $_FILES["upload$i"]['name'];
  }

$totalPics = count($pic);

for($i = 0; $i<$totalPics; $i++)

if(is_uploaded_file($_FILES["upload$i"]['tmp_name']) and
    copy($_FILES["upload$i"]['tmp_name'], "$fileName$i$ext"))

 

then it comes the INSERT sql command:

 

 

$sql2 = "INSERT INTO images SET
  			numestatiune='$numestatiune',
                filename='$fileName$i$ext',
                width='$newwidth',
                height='$newheight'
                ";

      if(mysql_query($sql2))
      {
        echo "images uploaded and resized";
      }

 

so on the webserver i have 0.jpg , 1.jpg, 2.jpg but in sql i have only 0.jpg

 

any ideas anyone?

 

cheers

Link to comment
https://forums.phpfreaks.com/topic/119969-array-loop/
Share on other sites

i was thinking somehting like this:

 

$xxdata = array();
foreach(array_keys($_FILES['upload$i']) as $i)
$xxdata[] = $_FILES['upload$i'][$i];
$xxvalue = mysql_real_escape_string(implode("\n",$xxdata)); 

 

 

and then add the $xxvalue into sql

 

but it doesn't work

Link to comment
https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618008
Share on other sites

If u have 4 file uploads, lets say name "file1", "file2", "file3", "file4", u can use a similar code:

 

<?php
for($i=1;$i<=4;$i++){
     $filename = $_FILES['file' . $i]['name'];
     $results = @mysql_query("INSERT INTO table (image) VALUES ('$filename')") or die();
}
?>

 

EDIT: Noted that i had an extra loop in the snippet so i edited the code.

Link to comment
https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618009
Share on other sites

Could use this:

$_pics = "";
$_pics_temp = "";
$_done = 0;

foreach ($_FILES as $_key => $_value)
{
	if(!empty($_FILES[$_key]['name']))
	{
		$_pics .= $_FILES[$_key]['name'].",";
		$_pics_temp .= $_FILES[$_key]['tmp_name'].",";
	}
}
$_pics = explode(",", substr($_pics, 0, strlen($_pics)-1));
$_pics_temp = explode(",", substr($_pics_temp, 0, strlen($_pics_temp)-1));
$totalPics = count($_pics);

foreach($_pics_temp as $_key => $_value)
{
	$_rand = rand(0, 1000);
	if (is_uploaded_file($_value) && copy($_value, "$fileName$_rand$ext"))
	{
		$_sql = sprintf("INSERT INTO images (namestatiune, filename, width, height) VALUES ('%s', '%s', '%d', '%d')", $numestatiune, $fileName.$_rand.$ext, $newwidth, $newheight);
		if (mysql_query($_sql))
		{
			$_done++;
		}
	}
}

echo("You have uploaded -".$_done."- image(s).");

 

I haven't really tested it though...

Link to comment
https://forums.phpfreaks.com/topic/119969-array-loop/#findComment-618099
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.