I assume we are only talking about images here....
it seems that optimizing to an exact file size (as opposed to fixed image dimensions) isn't that easy to do (this is assuming you are only going to use PHP's built in functions and can't or won't be able to find a seperate 3rd party library to do what you want). Firstly, it is going to depend on the physical dimensions of the image - try getting a 1000px x 1000px image with a large set of colours down to anything under 10k - it aint gonna be done. Additionally - the more you look at reducing the files size, the more image degredation you will get, and in some cases this will probably negate the value of uploading an image to the site.
Remember - making the most of image optimisation means a manual process to get the balance of offsetting quality and filesize right.
Of course - if this is still something you want to do then take a look at the [man]imagejpeg/man function. The 3rd parameter in this is the "quality" of the final jpeg produced. Obviously the lower this is (allows values from 0-100 with 100 being the best), the smaller the final file size will be, and the more the image will degrade.
An alternative would be to restrict the file size that a user can upload to the site. When a file gets uploaded, check the size of it, and only accept it if it fits your preset size limit, you could also do a dimension check if you were worried about large dimension images being uploaded.
Anyhow - hope this points you in the right direction!