Process multiple forms in one PHP file

Hello. I'm trying to make a "image resizer" which first uploads a file to my server, then resizes it.

It works fine if I remove the second form and add the the inputs to the the first form, but then I can't show the original image's size dimensions, since it needs to be uploaded first as far as I know...
What should I do to make the second form work?
<form action="" method="post" enctype="multipart/form-data">
<input type="file" name="file" id="file"><br />
<input type="submit" name="submit" value="Continue"><br />

if ($_FILES["imgsource"]["error"] > 0)
    echo "Return Code: " . $_FILES["imgsource"]["error"] . "<br />";
      $tmpdir = 'temp';
      $gendir = 'generated';
      $fname = $_FILES["file"]["name"];
      $tmpname = $_FILES["file"]["tmp_name"];
      $file_type = $_FILES["file"]["type"];
      list($width, $height) = getimagesize($tmpdir."/".$fname);
      $newfname = md5(time());
      echo "<br />Original Image: <br /><img src='".$tmpdir."/".$fname."'> <br /> Image size: ".$width." x ".$height."<br /><br />"; 
      echo '<form action="" method="post">
            New Height: <br />
            <input type="text" name="imgh"><br /><br />
            New Width: <br />
            <input type="text" name="imgw"><br /><br />
            <input type="submit" name="submit2" value="Confirm resize">
if(isset($_POST['submit 2'])){ 
         $imgh = $_POST['imgh'];
         $imgw = $_POST['imgw']; 
         if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){
         $new_img = imagecreatefromjpeg($tmpdir."/".$fname);
         }elseif($file_type == "image/x-png" || $file_type == "image/png"){
         $new_img = imagecreatefrompng($tmpdir."/".$fname);
         }elseif($file_type == "image/gif"){
         $new_img = imagecreatefromgif($tmpdir."/".$fname);

      $resized_img = imagecreatetruecolor($imgw,$imgh);
      imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $imgw, $imgh, $width, $height);
      ImageDestroy ($resized_img);
      ImageDestroy ($new_img);
      echo "<img src='".$gendir."/".$newfname."'> <br /> Should work!";

Open in new window

Who is Participating?
Julian HansenCommented:
You can't process two forms from a page - only one gets submitted


1. Combine them
2. Put hidden variables in the second one with width and height values stored there using same field names as for the first form
3. Look at an AJAX upload tool that uploads the file and returns the metrics to the calling script which then updates the relevant fields in the page.

I would recommend 2 in this case
Ante0Author Commented:
Yes, I will have to go with 2 or 3... as I want to show the original image and it's size before confirming and resizing it.
Thanks :)
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.