Page 1 of 1

Help with something sily envolving headers

Posted: Thu Feb 25, 2010 4:19 pm
by olidenia
I have this script, and what I want to do is Upload an image to a folder, this part is OK and then give me the url of the images, this part is OK The problem I have is passing the two first inputs in the form, I dont know how to edit the header bit to get it work.

Script:

Code: Select all

<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"?>
 
<?php
 
// upload the file
if ((isset($_POST["submitted_form"])) && ($_POST["submitted_form"] == "image_upload_form")) {
    
    // file needs to be jpg,gif,bmp,x-png and 4 MB max
    if (($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg" || $_FILES["image_upload_box"]["type"] == "image/gif" || $_FILES["image_upload_box"]["type"] == "image/x-png") && ($_FILES["image_upload_box"]["size"] < 4000000))
    {
        
  
        // some settings
        $max_upload_width = 2592;
        $max_upload_height = 1944;
          
        // if user chosed properly then scale down the image according to user preferances
        if(isset($_REQUEST['max_width_box']) and $_REQUEST['max_width_box']!='' and $_REQUEST['max_width_box']<=$max_upload_width){
            $max_upload_width = $_REQUEST['max_width_box'];
        }    
        if(isset($_REQUEST['max_height_box']) and $_REQUEST['max_height_box']!='' and $_REQUEST['max_height_box']<=$max_upload_height){
            $max_upload_height = $_REQUEST['max_height_box'];
        }   
 
        
        // if uploaded image was JPG/JPEG
        if($_FILES["image_upload_box"]["type"] == "image/jpeg" || $_FILES["image_upload_box"]["type"] == "image/pjpeg"){    
            $image_source = imagecreatefromjpeg($_FILES["image_upload_box"]["tmp_name"]);
        }       
        // if uploaded image was GIF
        if($_FILES["image_upload_box"]["type"] == "image/gif"){ 
            $image_source = imagecreatefromgif($_FILES["image_upload_box"]["tmp_name"]);
        }   
        // BMP doesn't seem to be supported so remove it form above image type test (reject bmps)   
        // if uploaded image was BMP
        if($_FILES["image_upload_box"]["type"] == "image/bmp"){ 
            $image_source = imagecreatefromwbmp($_FILES["image_upload_box"]["tmp_name"]);
        }           
        // if uploaded image was PNG
        if($_FILES["image_upload_box"]["type"] == "image/x-png"){
            $image_source = imagecreatefrompng($_FILES["image_upload_box"]["tmp_name"]);
        }
        
 
        $remote_file = "image_files/".$_FILES["image_upload_box"]["name"];
        imagejpeg($image_source,$remote_file,100);
        chmod($remote_file,0644);
    
    
 
        // get width and height of original image
        list($image_width, $image_height) = getimagesize($remote_file);
    
        if($image_width>$max_upload_width || $image_height >$max_upload_height){
            $proportions = $image_width/$image_height;
            
            if($image_width>$image_height){
                $new_width = $max_upload_width;
                $new_height = round($max_upload_width/$proportions);
            }       
            else{
                $new_height = $max_upload_height;
                $new_width = round($max_upload_height*$proportions);
            }       
            
            
            $new_image = imagecreatetruecolor($new_width , $new_height);
            $image_source = imagecreatefromjpeg($remote_file);
            
            imagecopyresampled($new_image, $image_source, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);
            imagejpeg($new_image,$remote_file,100);
            
            imagedestroy($new_image);
        }
        
        imagedestroy($image_source);
        
        
        header("Location: submit1.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
        exit;
    }
    else{
        header("Location: submit1.php?upload_message=asegurese que la imagen es de formato jpg, gif o png y que sea menor de 4MB&upload_message_type=error");
        exit;
    }
}
?>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Editar imagen</title>
<style type="text/css">
<!--
body,td,th {
    font-family: Arial, Helvetica, sans-serif;
    color: #333333;
    font-size: 12px;
}
 
.upload_message_success {
    padding:4px;
    background-color:#009900;
    border:1px solid #006600;
    color:#FFFFFF;
    margin-top:10px;
    margin-bottom:10px;
}
 
.upload_message_error {
    padding:4px;
    background-color:#CE0000;
    border:1px solid #990000;
    color:#FFFFFF;
    margin-top:10px;
    margin-bottom:10px;
}
 
-->
</style></head>
 
<body>
 
<h1 style="margin-bottom: 0px">Editar imagen</h1>
 
 
        <?php if(isset($_REQUEST['upload_message'])){?>
            <div class="upload_message_<?php echo $_REQUEST['upload_message_type'];?>">
            <?php echo htmlentities($_REQUEST['upload_message']);?>
            </div>
        <?php }?>
 
 
<form action="submit1.php" method="post" enctype="multipart/form-data" name="image_upload_form" id="image_upload_form" style="margin-bottom:0px;">
<label>Imagen, tamaño maximo 4MB. puede ser de los siguientes formatos jpg, gif,  png:</label><br />
 
          <input type="text" name="titulo" value=""><br>
          <textarea name="mensaje" rows="" cols=""></textarea><br>
          <input name="image_upload_box" type="file" id="image_upload_box" size="40" />
          <input type="submit" name="submit" value="Enviar" />     
     
     <br />
    <br />
 
     
      <label>La imagen se ajustará al predeterminado: (182 x 128 px max):</label>
      <br />
      <input name="max_width_box" type="text" id="max_width_box" value="182" size="4">
      x      
      
      <input name="max_height_box" type="text" id="max_height_box" value="128" size="4">
      px.
      <br />
      <br />
      <p style="padding:5px; border:1px solid #EBEBEB; background-color:#FAFAFA;">
      
  </p>
 
      
 
<input name="submitted_form" type="hidden" id="submitted_form" value="image_upload_form" />
          </form>
 
 
 
 
<?php if(isset($_REQUEST['show_image']) and $_REQUEST['show_image']!=''){?>
<p>
    
    <table border="1"  width="100%">
  <tr><!-- Row 1 -->
     <td><img src="image_files/<?php echo $_REQUEST['show_image'];?>" /></td><!-- Col 1 -->
     <td>
     <p><?php echo $_POST['titulo'];  ?></p>
     <p><?php echo $_POST["mensaje"];  ?></p>
     </td><!-- Col 2 -->
  </tr>
</table>
 
</p>
 
<?php }?>
I want to pas the variables thru the header:

Code: Select all

header("Location: submit1.php?upload_message=image uploaded&upload_message_type=success&show_image=".$_FILES["image_upload_box"]["name"]);
        exit;

Re: Help with something sily envolving headers

Posted: Thu Feb 25, 2010 4:35 pm
by AbraCadaver
Why doesn't the header() call you have work?

Re: Help with something sily envolving headers

Posted: Thu Feb 25, 2010 4:39 pm
by olidenia
The two new fields are my addition, the script works fine, it calls it self again and starts at the top of the page, but I just can't echo the values at the end of the page on submit.

It's easy to see what I want the script to do, get the code in the first code box above, and save it as submit1.php and create a folder called image_files execute the script on your localhost and you will see what I cant get to work.

Thanks beforehand

Re: Help with something sily envolving headers

Posted: Thu Feb 25, 2010 4:46 pm
by AbraCadaver
I see:

Code: Select all

$titulo = urlencode($_POST['titulo']);
$mensaje =urlencode($_POST['mensaje']);
$image = $_FILES["image_upload_box"]["name"];
header("Location: submit1.php?upload_message=image uploaded&upload_message_type=success&show_image=$image&titulo=$titulo&mensaje=$mensaje");
exit;
Then in the echos at the bottom you want to use $_GET instead of $_POST.

Re: Help with something sily envolving headers

Posted: Thu Feb 25, 2010 4:57 pm
by olidenia
Not working, did you get it to work? If yes can you post all of the code with your modifications. Thanks :wink:

Code: Select all

TitleMessage
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\xampp\htdocs\dbtest\submit1.php:3) in C:\xampp\xampp\htdocs\dbtest\submit1.php on line 79

Re: Help with something sily envolving headers

Posted: Thu Feb 25, 2010 5:00 pm
by AbraCadaver
olidenia wrote:Not working, did you get it to work? If yes can you post all of the code with your modifications. Thanks :wink:

Code: Select all

TitleMessage
Warning: Cannot modify header information - headers already sent by (output started at C:\xampp\xampp\htdocs\dbtest\submit1.php:3) in C:\xampp\xampp\htdocs\dbtest\submit1.php on line 79
Your original one shouldn't have worked either. Get rid of the tags in red:

Code: Select all

<?php ini_set("memory_limit", "200000000"); // for large images so that we do not get "Allowed memory exhausted"[color=#FF0000]?>[/color]
 
[color=#FF0000]<?php[/color]

Re: Help with something sily envolving headers

Posted: Thu Feb 25, 2010 5:13 pm
by olidenia
Magick, it did the job well, now I can continue my scrip, :D

Many many thanks.