views:

127

answers:

3

Hi there,

I looked the site to find a solution for my problem, but couldn't find the answer. Hopefully someone can help me with this:

I have a script that uploads and resizes an image without pagerefresh. I found it on the internet and adjusted it for my use. The script can be found underneath.

My problem:

First the upload.php script worked, but know I get error number 9.

What did I do to try to solve it myself:

  1. Tried it on a localmachine and on a server, both give the same error number 9
  2. Removed all the $_POST and set 'static' vars, didn't work and still got number 9

Can someone please help me, tnx in advance

Grtzzz

Wim

upload.php

<?php
/************************************************************            
 *-----------------------------------------------------------
 * Version  : 0.2
 * Author  : Wim Selles        
 * Date  : 2009-12-04
 * History  : Adjusted to use with AJAX upload
 *     Added check for allowed extension
 *     Added change filename to time 
 *     of uploading
 *-----------------------------------------------------------
 * Version  : 0.1
 * Author  : Eris        
 * Date  : 2005-11-19
 * History  : Initial version
 * Source  : http://www.phphulp.nl/php/scripts/9/464/
 *-----------------------------------------------------------
 ************************************************************/

if(!empty($_FILES['file'])){
 //---- filename generated by the server when uploading a file
 $tempfile     =  $_FILES['file']['tmp_name'];
 //---- directory with the final location 
 $dir      =  $_POST['uploadPath'];
 //---- Get the extension of the file    
 $ext      =  strtolower(substr(strrchr($_FILES['file']['name'], '.'), 1));
 $ext_image    =  array("gif", "jpg","jpeg","png");
 //---- new filename is time and extention
 $file      =  time().".".$ext;
 //---- resize / max height and width of image
 list($height,$width) = explode(',',$_POST['imageMess']);
 //---- The max filesize
 $limit_size    = $_POST['imageSize']*1024*1024;

 //---- error = 1 => file is not uploaded
 //---- error = 2 => No image file
 //---- error = 3 => File uploaded
 //---- error = 4 => Error during move upload file
 //---- error = 5 => File allready exsits
 //---- error = 6 => Error resizing jpg
 //---- error = 7 => Error resizing gif
 //---- error = 8 => Error resizing png
 //---- error = 9 => Imagecreate error for jpg
 //---- error = 10 => Resized (not error, but succes ;-))
 //---- error = 11 => Imagecreate error for gif 
 //---- error = 12 => Imagecreate error for png 
 //---- error = 13 => Filetype not allowed

 //----check if the file is realy uploaded
 if(filesize($tempfile)>=$limit_size){
  $error = 14;
 }elseif(!is_uploaded_file($tempfile)){
  $error = 1;
 }elseif(!in_array($ext, $ext_image)){
  //---- Check if file is allowed, if not, then show error
  $error = 13;
 }else{
  //---- get the dimensions of the file
  if(!$dim = getimagesize($tempfile)){
   $error = 2;
  }else{    
   //---- 0 = width 
   //---- 1 = height
   //---- 2 = type
   //---- we want to calculte if it is bigger then the maxsize if not keep it easy --> upload it
   if($dim[0] < $width && $dim[1] < $height){
    //----move upload file
    if(!file_exists($dir.$file)){
     if(@move_uploaded_file($tempfile,$dir.$file)){
      $error = 3;
     }else{
      $error = 4;    
     }
    }else{
     $error = 5;   
    }        
   }else{
    //---- we have to resize :(
    if($dim[0] > $dim[1]){
     $prop = $width / $dim[0];
     $dims[0] = $width;
     $dims[1] = round($dim[1] * $prop); 
    }else{
     $prop = $height / $dim[1];
     $dims[1] = $height;
     $dims[0] = round($dim[0] * $prop); 
    }
    //---- we know the new size
    if($dim[2] == 2){
     if(!$mimage = @imagecreatefromjpeg($tempfile)){
      $error = 6;
     }
    }
    //---- we know the new size
    if($dim[2] == 1){
     if(!$mimage = @imagecreatefromgif($tempfile)){
      $error = 7;
     }
    }
    //---- we know the new size
    if($dim[2] == 3){
     if(!$mimage = @imagecreatefrompng($tempfile)){
      $error = 8;
     }
    }
    $im = @imagecreatetruecolor($dims[0],$dims[1]);
    @imagecopyresampled($im, $mimage, 0, 0, 0, 0, $dims[0], $dims[1], $dim[0], $dim[1]);

    if(!file_exists($dir.$file)){
     if($dim[2] == 2){
      if(!@imagejpeg($im,$dir.$file,100)){
       $error = 9; // This is the error i still get
      }else{
       $error = 10;    
      }    
     }    
     if($dim[2] == 1){
      if(!@imagegif($im,$dir.$file)){
       $error = 11;
      }else{
       $error = 10;    
      }    
     }

     if($dim[2] == 3){
      if(!@imagepng($im,$dir.$file)){
       $error = 12;
      }else{
       $error = 10;      
      }    
       }
    }else{
     $error = 5;       
    }
    imagedestroy($im);
    imagedestroy($mimage);  
    //---- end resize
   }  
  }
 }
 ?>

 <script language="javascript" type="text/javascript">
     parent.stopUpload(<?php echo $error; ?>,'<?php echo "intranet/admin/uploadedImages/".$file;?>');
  //alert(<?php //echo $error2;?>);
    </script>

    <?php
}
?>

JS file:

$(document).ready(function(){
 //---- When the button is clicked to submit the file
 $('#submitfile').submit(startUpload);
});

function startUpload(){
 $('#f1_upload_process').show();
 return true;
}
stopUpload=function (response, filename){
 //---- The upload errors
 var uploadError = new Array;
 uploadError[1] =  'Bestand is niet geupload';       // file is not uploaded
 uploadError[2] = 'Dit is geen afbeeldingsbestand';     // No image file
 uploadError[4] = 'Error tijdens het verplaatsen van het bestand'; // Error during move upload file
 uploadError[5] = 'Bestand bestaat reeds';        // File allready exsits
 uploadError[6] = 'Error tijdens het resizen van een jpg bestand'; // Error resizing jpg
 uploadError[7] = 'Error tijdens het resizen van een gif bestand'; // Error resizing gif
 uploadError[8] = 'Error tijdens het resizen van een png bestand'; // Error resizing png
 uploadError[9] = 'Error tijdens het creeeren van een jpg bestand'; // Imagecreate error for jpg
 uploadError[11] = 'Error tijdens het creeeren van een gif bestand'; // Imagecreate error for gif
 uploadError[12] = 'Error tijdens het creeeren van een png bestand'; // Imagecreate error for png
 uploadError[13] = 'Bestandstype niet toegestaan';      // Filetype not allowed
 uploadError[14] = 'Het bestand is te groot';       // File to big
 //---- If fileupload went good, insert image into RTE
 if (response == 3 || response == 10){
  //alert(filename);
  $('#profilePhoto').attr("src",filename);
  $('#f1_upload_process').hide();
  $('#result').html('');
  $('#fileLocation').val('');
  return false;
 } else {
  $('#f1_upload_process').hide();
  $('#result').html(uploadError[response]);
  //alert('Response= ' + response + ' New filename= ' + filename);
 }
 return true;
}

The form:

<form action="intranet/admin/upload.php" method="post" enctype="multipart/form-data" target="upload_target" id="submitfile">

                <img id="profilePhoto" src="intranet/admin/uploadedImages/unknown.png" height="100px"/>

                <img id="f1_upload_process" src="include/css/images/ajax-loader3.gif" />

                <br />
    <br />

    <input name="file" type="file" id="fileLocation" />
    <input type="submit" name="submitBtn" value="Upload" />

    <br />
    <br />

    <p class="text">Toegestane extensies: *.jpg | *.gif | *.png</p>

    <br />

    <p id="result"></p>

   </form>

   <iframe id="upload_target" name="upload_target" style="width:0;height:0;border:0px solid #fff;"></iframe>
+2  A: 

Try to remove the @ in this line:

 if(!@imagejpeg($im,$dir.$file,100)){

It suppresses the error imagejpeg throws. Without it, you might see what your problem is.

Jimmy Shelter
If it turns out to be a memory problem (always my 1st bet), remember that a 3000 x 3000 Pixel JPG eats up not 9 MB, but at least 3 x 3000 x 3000 = 27 MB of memory.
Pekka
A: 

Remove @ from the following line:

  if(!@imagejpeg($im,$dir.$file,100)){

@ suppresses errors. If it's removed and display_errors is on, you should see a more descriptive error message.

My guess is that imagejpeg() can't write to the file because of insufficient permissions.

Jordan Ryan Moore
A: 

Damn,

I'm stupid :(

As suggested I removed the @ and saw with my own eyes what I've done wrong.

The error was in my directory structure it is like this

|mainFolder| |_|subfolder| |upload.php ||imageFolder|

The $dir = $_POST['uploadPath']; was a folder that moved the files to "mainfolder/subfolder/imageFolder" but it needed to be "imageFolder/" because I use the upload.php file in the "subfolder".

Thx for all your help

Grtz

Wim

Megapool020