Spiga

zend framework losing session on swf/flash upload

March 04, 09 by Gabi Solomon

Recently i tried to integrate the well known swfupload script into a website.
If you are here you must probably know that normally the flash request looses session since it doesn’t send cookie headers. The recommended workaround is to send the sessionid as a parameter through post, and in the action page set the session_id() to the post variable before the session start.

I did this but for some reason, my zend application refused to accept the new session_id. I struggled with it for quite a few hours, and finally narrowed it down to a php problem since the session_id would get set properly but after session start the session would be empty.

Really hitting a dead end i headed to stackoverflow ( really thankful for the guys that made this site possible ) and tried my luck there and wrote a question. And i got my answer :D

Seemed like the problem was with the php extension Suhosin witch has a session protection to prevent session stealing. A very useful thing but in this case it made me spend quite a few hours puzzled and staring at the monitor.

Hope you this is also your problem,
Cheers

How to upload & resize images in PHP with thumb creation

August 02, 08 by Gabi Solomon

Many times in my coding projects i had to do an upload form that will upload an image. This by it self is not so hard to do, and i bet many of you can write the code in under a minute :) ). But as we all know webspace is not free ( excluding free hosting ) and certainly not unlimited, so we must try and make the best use out of it and most of the time you will need to resize the images uploaded to a certain size, and maybe even make a thumb image.

This where the fun starts, every coder has his own methods of dealing with image resizing, as do I :D .
I came across a class on phpclasses.org called class.upload.php and fall in love with it :) ).

It is the ideal class to quickly integrate file upload and image manipulation in your site. That's all you need for a gallery script for instance.

It manages the uploaded file and allows you to do whatever you want with the file as many times as you want. If the file is an image, you can convert and resize it, rotate it, crop it in many ways; You can also add borders, frames, bevels, add of text labels and watermarks or apply graphic filters such as contrast or brightness correction, colorization, negative, threshold, greyscale, reflections and more. Transparency and true color are fully supported. JPEG, PNG, GIF and BMP are supported.

Security features and file management functions are provided. The class can also work on local files, which is useful for batch processing images online, and can circumvent open_basedir restrictions. Uploaded files can also be output directly to the browser.

The class is mature and well documented, already widely used around the world. From version 0.25, the class is fully internationalized. It is also compatible with PHP 4 and 5.

Example of usuage

PHP:
  1. $foo = new Upload($_FILES['form_field']);
  2. if ($foo->uploaded) {
  3.   // save uploaded image with no changes
  4.   $foo->Process('/home/user/files/');
  5.   if ($foo->processed) {
  6.     echo 'original image copied';
  7.   } else {
  8.     echo 'error : ' . $foo->error;
  9.   }
  10.   // save uploaded image with a new name
  11.   $foo->file_new_name_body = 'foo';
  12.   $foo->Process('/home/user/files/');
  13.   if ($foo->processed) {
  14.     echo 'image renamed "foo" copied';
  15.   } else {
  16.     echo 'error : ' . $foo->error;
  17.   }
  18.   // save uploaded image with a new name,
  19.   // resized to 100px wide
  20.   $foo->file_new_name_body = 'image_resized';
  21.   $foo->image_resize = true;
  22.   $foo->image_convert = gif;
  23.   $foo->image_x = 100;
  24.   $foo->image_ratio_y = true;
  25.   $foo->Process('/home/user/files/');
  26.   if ($foo->processed) {
  27.     echo 'image renamed, resized x=100
  28.           and converted to GIF';
  29.     $foo->Clean();
  30.   } else {
  31.     echo 'error : ' . $foo->error;
  32.   }
  33. }

Small problems i had with this class

Even though it is a great class that i use every time i need to do an upload, i had a few problems with it, and have toke me a while to solve. As a suggestion for debug do a print of "$foo->log" variable to see what happened during the preparation of the image.

1. Handling uploads from flash uploader

It took me a while to solve this one, since images uploaded with flash were not being re sized, but after a some debugging and searching i managed to figure out the cause: the mime type for the file was application/octet-stream so the class didnt treat it as an image.
Solution:
You can find it on the class forum

2. Handling very large images

This one really put me to the test, every time i uploaded an image that was large ( above 1,5 Mb ) it just show an empty page, not an error ... nothing. Couldn't figure it out. I looked in the php error log ... nothing. After like 2 hours of almost throwing my monitor out the window i managed to find a solution. It was because of the server, more exactly the memory limit. During the processing of large images, the script would use a lot of memory which would cause the script to stop once it reached the limit with no error message.
Solution:
Add this in your script:
ini_set ( "memory_limit", "40M")

Or in a .htaccess file:
php_value memory_limit 40M

That is it i dindt had any more troubles with this class, it is a pretty stable class witch didnt gave me to much trouble.

I hope you will enjoy it too.
Cheers.