Jump to content

[SOLVED] upload script only uploads one image


jesushax

Recommended Posts

hi all below is my add news script, it currently only upolads what ever is in teh picture[0] field

 

anyone have any ideas?

Thanks

 

<?php
function resampleimage($maxsize, $sourcefile, $destination, $imgcomp=0){
// SET THE IMAGE COMPRESSION
$g_imgcomp=100-$imgcomp;
  // CHECK TO SEE IF THE IMAGE EXISTS FIRST
  if(file_exists($sourcefile)){
  // FIRST WE GET THE CURRENT IMAGE SIZE
  $g_is=getimagesize($sourcefile);
    /********* CALCULATE THE WIDTH AND THE HEIGHT ***************/
    // CHECK TO SEE IF THE WIDTH AND HEIGHT ARE ALREADY SMALLER THAN THE MAX SIZE
    if($g_is[0] <= $maxsize && $g_is[1] <= $maxsize){
    // LEAVE WIDTH AND HEIGHT ALONE IF IMAGE IS SMALLER THAN MAXSIZE
    $new_width=$g_is[0];
    $new_height=$g_is[1];
    } else {
    // GET VALUE TO CALCULATE WIDTH AND HEIGHT
    $w_adjust = ($maxsize / $g_is[0]);
    $h_adjust = ($maxsize / $g_is[1]);
      // CHECK TO WHICH DIMENSION REQUIRES THE SMALLER ADJUSTMENT
      if($w_adjust <= $h_adjust){
      // CALCULATE WIDTH AND HEIGHT IF THE WIDTH VALUE IS SMALLER
      $new_width=($g_is[0]*$w_adjust);
      $new_height=($g_is[1]*$w_adjust);
      } else {
      // CALCULATE WIDTH AND HEIGHT IF THE HEIGHT VALUE IS SMALLER
      $new_width=($g_is[0]*$h_adjust);
      $new_height=($g_is[1]*$h_adjust);
      }
    }
  //SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER THE LAST "." )
$image_type = strrchr($sourcefile, ".");

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
	case '.jpg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.jpeg':
		$img_src = imagecreatefromjpeg($sourcefile);
		break;
	case '.png':
		$img_src = imagecreatefrompng($sourcefile);
		break;
	case '.gif':
		$img_src = imagecreatefromgif($sourcefile);
		break;
	default:
		echo("Error Invalid Image Type");
		die;
		break;
}
  // CREATE THE TRUE COLOR IMAGE WITH NE WIDTH AND HEIGHT
  $img_dst=imagecreatetruecolor($new_width,$new_height);
  // RESAMPLE THE IMAGE TO NEW WIDTH AND HEIGHT
  imagecopyresampled($img_dst, $img_src, 0, 0, 0, 0, $new_width, $new_height, $g_is[0], $g_is[1]);
  // OUTPUT THE IMAGE AS A JPEG.
  // THIS CAN BE CHANGED IF YOU WANT TRANSPARENCY OR PREFER ANOTHER FORMAT. MAKE SURE YOU CHANGE HEADER ABOVE.
  imagejpeg($img_dst, $destination, 100);
  // DESTROY THE NEW IMAGE
  imagedestroy($img_dst);
  return true;
  } else {
  return false;
  }
}

switch(@$_GET["action"]) {
Case "add":
session_start();
include($_SERVER['DOCUMENT_ROOT'] . '/includes/connection.php');
include($_SERVER['DOCUMENT_ROOT'] . '/includes/access.inc');
include($_SERVER['DOCUMENT_ROOT'] . '/includes/editor_level.inc');


$absolute_path = $_SERVER['DOCUMENT_ROOT'] . "/news/uploads/";//Absolute path to where files are uploaded
$thumb_path = $_SERVER['DOCUMENT_ROOT'] . "/news/uploads/thumbs/";   //Absolute path to where thumbs are to be stored if you want this
$size_limit = "yes";                                          //do you want a size limit yes or no.
$limit_size = "4000000";                                       //How big do you want size limit to be in bytes
$limit_ext = "yes";                                           //do you want to limit the extensions of files uploaded
$ext_count = "4";                                             //total number of extensions in array below
$extensions = array(".jpg", ".jpeg", ".png", ".gif");         //List extensions you want files uploaded to be

$extens = '';

        if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) {
           $extens = "any extension";
        } else {
        $ext_count2 = $ext_count+1;
        for($counter=0; $counter<$ext_count; $counter++) {
            $extens .= "  $extensions[$counter]";
        }
        }
        if (($limit_size == "") or ($size_limit != "yes")) {
            $limit_size = "any size";
        } else {
            $limit_size .= " bytes";
            $mb_size = ($limit_size/1000000);
        }

$NewsTitle = mysql_escape_string($_POST["NewsTitle"]);
$NewsShort = mysql_escape_string($_POST["NewsShort"]);
$NewsAttach = $_FILES['NewsAttachment']['name'][0];
$NewsImage1 = $_FILES['pictures']['name'][0];
$NewsImage2 = $_FILES['pictures']['name'][1];
$NewsFull = mysql_escape_string($_POST["NewsFull"]);
$NewsDate = date("Y-m-d");
if (($_POST["chkCons"]) == "1") {
$Cons = "1";
} else {
$Cons = "0";
}
if (($_POST["chkCity"]) == "1") {
$City = "1";
} else {
$City = "0";
}

mysql_query("INSERT INTO tblNews ( NewsTitle, NewsShort, NewsImage1, NewsAttachment, NewsFull, NewsImage2, NewsDateAdded, NewsArchive,Cons,City) Values ( '$NewsTitle', '$NewsShort', '$NewsImage1', '$NewsAttach', '$NewsFull', '$NewsImage2', '$NewsDate','0','$Cons','$City') ") or die(mysql_error());


$error=0;

  foreach ($_FILES["pictures"]["error"] as $key => $error) {
  $file_name =  $_FILES["pictures"]['name'][$error]; // can call this anything you like this will take the original name
  $file =  $_FILES["pictures"]['tmp_name'][$error];
  $file_size = $_FILES["pictures"]['size'][$error];
  
      if (($size_limit == "yes") &&($limit_size < $file_size)) {
        header("Location: /admin/news/edit_news.php?error=filesize");
        } else {
        $ext = strrchr($file_name,'.');
          if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
        echo $image_type;
	//header("Location: /admin/news/edit_news.php?error=filetype");
          }else{
          $j = $error + 1;
          // first move the file to
          move_uploaded_file($file, $absolute_path.$file_name);
          // Save full size image with max width/height
          resampleimage("175", $absolute_path.$file_name, $absolute_path.$file_name, 0); 
	  //resampleimage("125", $absolute_path.$file_name, $thumb_path.$file_name, 0);  
        }
        }
  $error++;
  }
  
$ESQL = mysql_query("SELECT `NewsID`,`City`,`Cons`,`NewsTitle` FROM `tblNews` WHERE `NewsTitle`='$NewsTitle'") or die(mysql_error());
$ER = mysql_fetch_array($ESQL);
if ($ER["Cons"] !="1" && $ER["City"] !="1") {
header("Location: /admin/news/edit_news.php?ID=".$ER["NewsID"]."&action=error1");
}
else{
    header("Location: /admin/news/default.php");
}
break;
default:

include($_SERVER['DOCUMENT_ROOT'] . '/includes/staff_header.php');
include($_SERVER['DOCUMENT_ROOT'] . '/includes/access.inc');

$size_limit = "yes";                                          //do you want a size limit yes or no.
$limit_size = "4000000";                                       //How big do you want size limit to be in bytes
$limit_ext = "yes";                                           //do you want to limit the extensions of files uploaded
$ext_count = "4";                                             //total number of extensions in array below
$extensions = array(".jpg", ".jpeg", ".png", ".gif");         //List extensions you want files uploaded to be


$extens = '';

        if (($extensions == "") or ($extensions == " ") or ($ext_count == "0") or ($ext_count == "") or ($limit_ext != "yes") or ($limit_ext == "")) {
           $extens = "any extension";
        } else {
        $ext_count2 = $ext_count+1;
        for($counter=0; $counter<$ext_count; $counter++) {
            $extens .= "  $extensions[$counter]";
        }
        }
        if (($limit_size == "") or ($size_limit != "yes")) {
            $limit_size = "any size";
        } else {
            $limit_size .= " bytes";
            $mb_size = ($limit_size/1000000);
        }

        $pichead = '<ul style="color:#FF0000; font-size:10px; list-style:none;">';
	$pichead .='<li>File extension must be '.$extens.'</li>';
        $pichead .='<li>Maximum file size is '.$mb_size.' Megabytes</li>';
        $pichead .='<li>No spaces in the filename</li>';
	$pichead .= '</ul>';

echo"<h2>Add News</h2>\n";
include($_SERVER['DOCUMENT_ROOT'] . '/admin/news/navigation.php');					
?>
<form action="<? echo $_SERVER['PHP_SELF'];?>?action=add" method="post" enctype="multipart/form-data">
<table width="100%" cellpadding="0" cellspacing="0">
<tr><td colspan="2">News Images:</td></tr>
<tr><td><table cellpadding="0" cellspacing="0">
<?php
$files = 2;   // Change this line for the number of uploads you want to allow
for($i=1; $i<=$files; $i++){
echo "<tr><td>Image $i :</td><td><input type=\"file\" name=\"pictures[]\" size=\"50\" /></td></tr>\n";
}
?>
<tr>
  <td>Attachment : </td>
  <td><input name="NewsAttachment[]" type="file" id="ufile[]" size="50" /></td>
</tr>
</table></td>
<td><? echo $pichead; ?></td></tr>

<tr>
  <td colspan="2"><hr /></td>
</tr>
<tr>
  <td colspan="2" style="width:80%;"><span style="width:20%;">News Title: </span></td>
</tr>
<tr>
<td colspan="2" style="width:80%;">
<input type="text" size="45" name="NewsTitle" /></td>
</tr>
<tr>
  <td colspan="2"> </td>
</tr>
<tr>
  <td colspan="2">News Brief :</td>
</tr>
<tr>
  <td colspan="2"><textarea name="NewsShort" id="NewsShort" style="width:550px; height:175px;"></textarea></td>
<tr>
  <td colspan="2"> </td>
</tr>
<tr>
  <td colspan="2" >News Full :</td>
</tr>
<tr>
  <td colspan="2"><textarea name="NewsFull" id="NewsFull" style="width:550px; height:175px;"></textarea></td>
  </tr>
<tr>
  <td colspan="2"> </td>
</tr>
<tr>
  <td colspan="2">Submit News to Websites</td>
</tr>
<tr>
  <td colspan="2">
  <table width="50%" border="0" cellpadding="0">
    <tr>
      <td><input name="chkCons" type="checkbox" value="1" /></td>
      <td>Construction Works</td>
      <td><input name="chkCity" type="checkbox" value="1" /></td>
      <td>City Works</td>
    </tr>
  </table>
  </td>
</tr>
<tr>
<td colspan="2"  style="height:32px;"><div style=" text-align:center">
<input type="submit" name=submit value="Add News" />
<input type="button" name="Reset" value="Cancel" alt="Cancel" onclick="document.location='/admin/news/default.php'" />
</div></td>
</tr>
</table>
</form>
<?php

include($_SERVER['DOCUMENT_ROOT'] . '/includes/staff_footer.php');
break;
}
?>

Link to comment
Share on other sites

at first glance, this:

  foreach ($_FILES["pictures"]["error"] as $key => $error) {
  $file_name =  $_FILES["pictures"]['name'][$error]; // can call this anything you like this will take the original name
  $file =  $_FILES["pictures"]['tmp_name'][$error];
  $file_size = $_FILES["pictures"]['size'][$error];

should be this i think:

  foreach ($_FILES["pictures"]["error"] as $n => $error) {
  $file_name =  $_FILES["pictures"]['name'][$n]; // can call this anything you like this will take the original name
  $file =  $_FILES["pictures"]['tmp_name'][$n];
  $file_size = $_FILES["pictures"]['size'][$n];

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.