Jump to content

resize on the fly script, can i adjust this to work on multiple files?


jesushax

Recommended Posts

you would put the function or script in its own file.

 

then you would loop through your photo's calling the resize script in an img tag

 

<img src="imagescript.php" border=0 />

 

This topic has been discussed alot try here for a little guidance.

 

http://www.phpfreaks.com/forums/index.php/topic,191611.msg860398.html#msg860398

 

Ray

Link to comment
Share on other sites

i didnt even post the code lol whoooops

 

here it is

 

<?php

if(isset($_POST['submit']))
{

	/*
		Image Resizer / Thumbnailer Script
		Daniel Neri
		Viper Creations
		www.vipercreations.com
	*/

	//make sure this directory is writable!
	$path_thumbs = $_SERVER['DOCUMENT_ROOT'] . "/news/uploads/";

	//the new width of the resized image, in pixels.
	$img_thumb_width = 125; // 

	$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
	//List of allowed extensions if extlimit = yes
	$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");

	//the image -> variables
    $file_type = $_FILES['vImage']['type'];
        $file_name = $_FILES['vImage']['name'];
        $file_size = $_FILES['vImage']['size'];
        $file_tmp = $_FILES['vImage']['tmp_name'];

        //check if you have selected a file.
        if(!is_uploaded_file($file_tmp)){
           echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
           exit(); //exit the script and don't process the rest of it!
        }
       //check the file's extension
       $ext = strrchr($file_name,'.');
       $ext = strtolower($ext);
       //uh-oh! the file extension is not allowed!
       if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {
          echo "Wrong file extension.  <br>--<a href=\"$_SERVER[php_SELF]\">back</a>";
          exit();
       }
       //so, whats the file's extension?
       $getExt = explode ('.', $file_name);
       $file_ext = $getExt[count($getExt)-1];

       //create a random file name
       $rand_name = md5(time());
       $rand_name= rand(0,999999999);
       //the new width variable
       $ThumbWidth = $img_thumb_width;

   //////////////////////////
   // CREATE THE THUMBNAIL //
   //////////////////////////
   
       //keep image type
       if($file_size){
          if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
               $new_img = imagecreatefromjpeg($file_tmp);
           }elseif($file_type == "image/x-png" || $file_type == "image/png"){
               $new_img = imagecreatefrompng($file_tmp);
           }elseif($file_type == "image/gif"){
               $new_img = imagecreatefromgif($file_tmp);
           }
           //list the width and height and keep the height ratio.
           list($width, $height) = getimagesize($file_tmp);
           //calculate the image ratio
           $imgratio=$width/$height;
           if ($imgratio>1){
              $newwidth = $ThumbWidth;
              $newheight = $ThumbWidth/$imgratio;
           }else{
                 $newheight = $ThumbWidth;
                 $newwidth = $ThumbWidth*$imgratio;
           }
           //function for resize image.
           if (function_exists(imagecreatetruecolor)){
           $resized_img = imagecreatetruecolor($newwidth,$newheight);
           }else{
                 die("Error: Please make sure you have GD library ver 2+");
           }
           //the resizing is going on here!
           imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
           //finally, save the image
           ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
           ImageDestroy ($resized_img);
           ImageDestroy ($new_img);
           
           
        }

        //ok copy the finished file to the thumbnail directory
	move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");
	/*
		Don't want to copy it to a separate directory?
		Want to just display the image to the user?
		Follow the following steps:

		2. Uncomment this code:
	/*
	/* UNCOMMENT THIS IF YOU WANT */
	//echo "IMG:<img src=\"$path_big/$rand_name.$file_ext\" />";
	//exit();
	//*/

	//and you should be set!
             
	//success message, redirect to main page.		
	$msg = urlencode("$title was uploaded! <a href=\"upload.php\">Upload More?</a>");
		header("Location: upload.php?msg=$msg");
		exit();


}else{

	//if there is a message, display it
	if(isset($_GET['msg']))
	{
		//but decode it first!
		echo "<p>".urldecode($_GET['msg'])."</p>";
	}
	//the upload form
echo "
<form action=\"$_SERVER[php_SELF]\" method=\"post\"enctype=\"multipart/form-data\">\n
<p>File:<input type=\"file\" name=\"vImage\" /></p>\n
<p><input type=\"submit\" name=\"submit\" value=\"Submit\" /></p>";
}

?>

 

its good it works, but can i add multiples to it?

Link to comment
Share on other sites

this script uploads and resizes a image, i jus twant it resized dont need thumbnails for this script

 

but would like a link to a simple thumbnail for the future but for now id like to know if i could make the above work for up to 4 images

 

cheers

Link to comment
Share on other sites

I have this already made. It will resize as many images as you want. read the comments in the script for changes.

 

<?php
$absolute_path = "C:/Inetpub/wwwroot/phpforum/images/";       //Absolute path to where files are uploaded
$thumb_path = "C:/Inetpub/wwwroot/phpforum/images/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 = "600000";                                       //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

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

if(!isset($_POST['submit'])){
$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 = "<li><font size=\"2\" color=660000>File extension must be $extens<b>";
        $pichead .="</b></font>
        <li><font size=\"2\" color=660000>Maximum file size is $limit_size ($mb_size MB)</font></li>
        <li><font size=\"2\" color=660000>No spaces in the filename</font></li>";
?>
<html>
<head>
<title>HTML Form for uploading image to server</title>
</head>
<body>
<form action="" method="post" enctype="multipart/form-data">
<html>
<title>Add Vehicle Form</title>
<body>
<p><? echo $pichead; ?></p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Pictures:<br />
<?php
$files = 4;   // Change this line for the number of uploads you want to allow
for($i=1; $i<=$files; $i++){
echo "Picture $i <input type=\"file\" name=\"pictures[]\" /><br />\n";
}
?>
<input type="submit" name=submit value="Send" />
</p>
</form>
<?php
} else {
$i=0;
//$photoarray = array();
  foreach ($_FILES["pictures"]["error"] as $key => $error) {
  $file_name =  $_FILES["pictures"]['name'][$i]; // can call this anything you like this will take the original name
  $file =  $_FILES["pictures"]['tmp_name'][$i];
  $file_size = $_FILES["pictures"]['size'][$i];
  //$photoarray[$i+1]= $file_name;
  $endresult = "<font size=\"4\" color=990000>$file_name uploaded successfully</font>";
    if ($file_name == "") {
    $pic = $i+1;
    $endresult = "<font size=\"4\" color=990000>Pic#$pic Not selected</font>";
    }else{
      if(file_exists("$absolute_path/$file_name")) {
      $endresult = "<font size=\"4\" color=990000>File Already Existed</font>";
      } else {
        if (($size_limit == "yes") && ($limit_size < $file_size)) {
        $endresult = "<font size=\"4\" color=990000>File was to big</font>";
        } else {
        $ext = strrchr($file_name,'.');
          if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
          $endresult = "<font size=\"4\" color=990000>File is wrong type</font>";
          }else{
          // first move the file to
          move_uploaded_file($file, $absolute_path.$file_name);
          // Save full size image with max width/height
          resampleimage("800", $absolute_path.$file_name, $absolute_path.$file_name, 0);  // Comment this line out if you don't want to resize the image change the "800" to the size you want
          // Save thumb image with max width/height of 200
          resampleimage("200", $absolute_path.$file_name, $thumb_path.$file_name, 0); // Comment this line out if you don't want to create a thumbnail, change the 200 to the size you want
          }
        }
      }
    }
  $i++;
  echo $endresult."<br>";
  }
}
?>
</body>
</html>

 

Ray

Link to comment
Share on other sites

oh wait it does do it, hey your scripts great,

 

where abouts can i add sql to upload file names to a db

 

and also

 

could i add something that would detec a word doc doc or pdf and just upload them to a dir and thats it?

Cheers

Link to comment
Share on other sites

You can add any code you like after

 

move_uploaded_file($file, $absolute_path.$file_name);

 

if this line runs then all variables are met.

 

so example

$picarray(".jpeg", ".jpg", ".gif", ".png");
if(in_array(strrchr($file_name, "."), $picarray)){
// resize pictures 
          // Save full size image with max width/height
          resampleimage("800", $absolute_path.$file_name, $absolute_path.$file_name, 0);  // Comment this line out if you don't want to resize the image change the "800" to the size you want
          // Save thumb image with max width/height of 200
          resampleimage("200", $absolute_path.$file_name, $thumb_path.$file_name, 0); // Comment this line out if you don't want to create a thumbnail, change the 200 to the size you want
}
// run code to enter info into database

 

just keep everything inside the brackets

 

Ray

Link to comment
Share on other sites

thanks also got this error messsage when trying to upload a large file

 

Fatal error: Allowed memory size of 20971520 bytes exhausted (tried to allocate 8000 bytes) in /home/fhlinux153/c/cwservicesltd.co.uk/user/htdocs/upload.php on line 44

 

could you tell me what it means?

 

and with regards to the pdf and word doc uploads, good idea bad idea?

 

cheers

Link to comment
Share on other sites

Yes your host uses the default value for uploaded files. This value is 2MB. So you will be restricted to 2MB uploads unless you have access to the php.ini file or contact the hosting company and have them change it.

 

Ray

Link to comment
Share on other sites

yes change this line

 

$file_name =  $_FILES["pictures"]['name'][$i]; // can call this anything you like this will take the original name

 

lets say you want to make it a random value between 100 and 200

 

$filename = rand(100, 200);

 

You can pretty much do whatever.

 

Ray

 

 

Link to comment
Share on other sites

what about file name 1 2 3 4 into file1 file2 file3 file4?

 

When you change the name you have to remember to put the extension back on.

 

 

$ext = strrchr($_FILES["pictures"]['name'][$i], ".");
$n = $i+1;
$file_name = "file".$n.$ext;

 

Since the loop starts at 0 we have to add one to get it to start at 1

 

Ray

Link to comment
Share on other sites

You beat me to it.I was just about to ask what happened to my file extension!

 

I'm  trying to get the script to resize an image to 240px x 180px regardless of aspect ratio. Is there an easy way to do this? I've tried but I keep mangling the script.

Link to comment
Share on other sites

you can just take this out

 

    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);
      }
    }

 

and put in

$new_width= 240;
$new_height=180;

 

But remember you image will be stretched if the uploaded image doesn't have the same aspect ratio

 

Ray


Link to comment
Share on other sites

I was trying to do something alot more complicated than that. Its late and I'm tired.Thank you.

 

Is it possible to downsample the image while keeping the same dimensions to reduce the file size?

 

Also I noticed that the two directories specified contain images with identical dimensions. What happens to the original image?

Link to comment
Share on other sites

Is it possible to downsample the image while keeping the same dimensions to reduce the file size?

 

Yes there is but the code you have uses the default I have new code to change it.

 

the original image get resized based on the first resampling. Reason you move the file there first is because the temporary file being stored has an extension of .tmp. And the way I wrote the script it looks for the extension to resample the image. I could rewrite it looking at the file type but it would also require another variable to add to the function which is not a big deal.

 

If you tell me what you want it to do I can fix the code to do it.

 

Ray

 

Link to comment
Share on other sites

hi been working with yoru code to incorporate it with my news system, dont know what ive done to make it not work, can you point me in the right direction?

 

THanks

 

error:

Warning: Invalid argument supplied for foreach() in /home/fhlinux153/c/cwservicesltd.co.uk/user/htdocs/admin/news/upload.php on line 162

code:

<?php
include($_SERVER['DOCUMENT_ROOT'] . '/includes/header.php');
include($_SERVER['DOCUMENT_ROOT'] . '/includes/admin_head.php');
include($_SERVER['DOCUMENT_ROOT'] . '/includes/admin_access.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

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

if(!isset($_POST['submit'])){
$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 = "<li><font size=\"2\" color=660000>File extension must be $extens<b>";
        $pichead .="</b></font>
        <li><font size=\"2\" color=660000>Maximum file size is $limit_size ($mb_size MB)</font></li>
        <li><font size=\"2\" color=660000>No spaces in the filename</font></li>";

?>
<p><? echo $pichead; ?></p>
<form action="<?=$_SERVER['PHP_SELF']?>" method="post">
<p>Pictures:<br />
<?php
$files = 2;   // Change this line for the number of uploads you want to allow
for($i=1; $i<=$files; $i++){
echo "Picture $i <input type=\"file\" name=\"pictures[]\" /><br />\n";
}
?>
<h3>Add News</h3>
<table width="100%">
<tr>
  <td  style="width:80%;"><span style="width:20%;">News Title: </span></td>
</tr>
<tr> 
<td style="width:80%;"> 
<input type="text" size="45" name="txtNewsTitle" /></td>
</tr>
<tr>
  <td> </td>
</tr>
<tr>
  <td>News Brief :</td>
</tr>
<tr>
  <td><textarea name="txtNewsShort" cols="70" rows="12" id="txtNewsShort"></textarea></td>
  </tr>
<tr>
  <td > </td>
</tr>
<tr>
   <td><strong>Attachments:</strong></td>
</tr>
<tr>
  <td>Attachment<input name="ufile[]" type="file" id="ufile[]" size="25" /> </td>
</tr>
<tr>
  <td> </td>
</tr>
<tr>
  <td >News Full :</td>
</tr>
<tr>
  <td ><textarea name="txtNewsFull" cols="70" rows="12" id="txtNewsFull"></textarea></td>
  </tr>
<tr>
  <td > </td>
</tr>
<tr> 
<td  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/index.php'" />
</div></td>
</tr>
</table>
</form>
<?php
} else {
$i=0;
//$photoarray = array();
  foreach ($_FILES["pictures"]["error"] as $key => $error) {
  $file_name =  $_FILES["pictures"]['name'][$i]; // can call this anything you like this will take the original name
  $file =  $_FILES["pictures"]['tmp_name'][$i];
  $file_size = $_FILES["pictures"]['size'][$i];
  //$photoarray[$i+1]= $file_name;
  $endresult = "<font size=\"4\" color=990000>$file_name uploaded successfully</font>";
    if ($file_name == "") {
    $pic = $i+1;
    $endresult = "<font size=\"4\" color=990000>Pic#$pic Not selected</font>";
    }else{
      if(file_exists("$absolute_path/$file_name")) {
      $endresult = "<font size=\"4\" color=990000>File Already Existed</font>";
      } else {
        if (($size_limit == "yes") && ($limit_size < $file_size)) {
        $endresult = "<font size=\"4\" color=990000>File was to big</font>";
        } else {
        $ext = strrchr($file_name,'.');
          if (($limit_ext == "yes") && (!in_array($ext,$extensions))) {
          $endresult = "<font size=\"4\" color=990000>File is wrong type</font>";
          }else{
          // first move the file to
          move_uploaded_file($file, $absolute_path.$file_name);
          // Save full size image with max width/height
          resampleimage("800", $absolute_path.$file_name, $absolute_path.$file_name, 0);  // Comment this line out if you don't want to resize the image change the "800" to the size you want
          // Save thumb image with max width/height of 200
          resampleimage("125", $absolute_path.$file_name, $thumb_path.$file_name, 0); // Comment this line out if you don't want to create a thumbnail, change the 200 to the size you want
          }
	  //mysql here
$ext = strrchr($_FILES["pictures"]['name'][$i], ".");
$n = $i+1;
$file_name = "file".$n.$ext;

$path1= $_SERVER['DOCUMENT_ROOT'] . "/news/uploads/".$HTTP_POST_FILES['ufile']['name'][0];
copy($HTTP_POST_FILES['ufile']['tmp_name'][0], $path1);

$NewsTitle = $_POST["txtNewsTitle"];
$NewsShort = $_POST["txtNewsShort"];
$NewsAttach = $HTTP_POST_FILES['ufile']['name'][0];
$NewsFull = $_POST["txtNewsFull"];
$NewsDate = date("d/m/y");


mysql_query("INSERT INTO tblNews(NewsTitle, NewsShort, NewsImage1, NewsAttachment, NewsFull, NewsImage2, NewsDateAdded, NewsArchive) Values ( '$NewsTitle', '$NewsShort', '$$file_name', '$NewsAttach', '$NewsFull', '$$file_name', '$NewsDate','0') ") or die(mysql_error());
header("Location: /admin/news/index.php");

        }
      }
    }
  $i++;
  echo $endresult."<br>";
  }
}
echo "</div>";
include($_SERVER['DOCUMENT_ROOT'] . '/includes/footer.php');
?>

Link to comment
Share on other sites

right i figured it out but dont know how to work change it

 

the for each statment means do this code for every image, but that would mean that each textfield in the form would have to have a field for every file thats there

 

meaning i need to execute the sql outside of the foreach statement

 

so where in this script can i execute sql after the for each is finished

 

cos i cant do a then {sql} after the for each can i, any ideas anyone?

 

thanks

Link to comment
Share on other sites

I am looking at your script now, but for starters the reason why the foreach loop fails is because you left out a form parameter.

 

<form action="<?=$_SERVER['PHP_SELF']?>" method="post" enctype="multipart/form-data" />

 

You need the "enctype" when working with files :)

 

Also did you want to rename all the files being uploaded?? If not which one's do you want to rename and rename to what??

 

Ray

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.