• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

Some functions in PHP

OriginXT

New Member
How to do these in PHP-Need Help!

I'm not a newbie, but I'm not a pro in PHP, so...

Do you know how to:

1) resize an image
2) how to download a file (a image) when clicked.

Please in PHP THANKS!

I need it for my gallery script

(http://granddbz.com/php/gallery.zip)

[edit]I removed finding the size of an image and added how do we resize an image[/edit]
 
Last edited:
Originally posted by Canuckkev
1) http://www.php.net/manual/en/function.filesize.php

What exactly do you mean by download a image? Instead of showing it in the browser, you want the link to automatically download it like it was a zip or exe?

No Not filesize()!!! It doesn't show the filesize of an image! It only shows the length of the text!

Yes I would like the link to automatically download the image like if it was a zip file!
 
no, filesize() will show the size of any file [in bytes] you pass through it.
 
What about automatically tarring or zipping an file (preferably an image) so that it is faster to download?
 
To download a picture (jpg for example):
PHP:
<?php
header("Content-type: application/jpeg"); 
readfile("http://www.yoursite.com/dir/".$filename) ;
?>
To resize an image, see www.php.net/manual/en/ref.image.php comments .
To compress, use bz2, it is better than zip or tgz :
PHP:
<?php
header("Content-type: application/bz2"); 
$fp = fopen("http://www.yoursite.com/dir/".$filename, "r");
$contents = fread ($fp, filesize ($filename));
fclose ($fp);
echo bzcompress ($contents, 9) ;
?>
 
Originally posted by Salam
To download a picture (jpg for example):
PHP:
<?php
header("Content-type: application/jpeg"); 
readfile("http://www.yoursite.com/dir/".$filename) ;
?>
To resize an image, see www.php.net/manual/en/ref.image.php comments .
To compress, use bz2, it is better than zip or tgz :
PHP:
<?php
header("Content-type: application/bz2"); 
$fp = fopen("http://www.yoursite.com/dir/".$filename, "r");
$contents = fread ($fp, filesize ($filename));
fclose ($fp);
echo bzcompress ($contents, 9) ;
?>

THANKS Salam! You are the best!

But, the compressing doesnt work, also the download thing, for example, http://mydomain.com/script.php?filename=someimage.jpg

It doesn't work.

THANKS
 
Code:
<?
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE"))
header("Content-Disposition: filename=$myfile" . "%20"); // For IE
else
header("Content-Disposition: attachment; filename=$myfile"); // For Other browsers
print $image;
?>
 
Thanks biggulp for the code .
What was that %20 in IE section ?
But, the compressing doesnt work, also the download thing, for example, http://mydomain.com/script.php?filename=someimage.jpg

It doesn't work.
You are using PHP 4.2.3 and in 4.2.3, register_globals is off (default) .
So change $filename to {$HTTP_GET_VARS['filename']} :
PHP:
<?php
header("Content-type: application/jpeg"); 
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE"))
header("Content-Disposition: filename={$HTTP_GET_VARS['filename']}"); // For IE
else header("Content-Disposition: attachment; filename={$HTTP_GET_VARS['filename']}"); 
readfile("http://www.yoursite.com/dir/".$HTTP_GET_VARS['filename']) ;
?>
If you want a small program to unzip bz2, try http://www.7-zip.org/dl/7zip224.exe (463 KB) .
 
Last edited:
Okay, here's my script:
PHP:
<?php
if(!empty($HTTP_GET_VARS['filename'])&&file_exists($HTTP_GET_VARS['filename'])){
$file=$HTTP_GET_VARS['filename'];
$format=explode(".",$file);
header("Content-type: application/$format[1]"); 
if (strstr($_SERVER["HTTP_USER_AGENT"], "MSIE"))
header("Content-Disposition: filename={$file}"); // For IE
else header("Content-Disposition: attachment; filename={$file}"); 
readfile($file);
}else{echo"File does not exist!";}
?>

When I go to http://granddbz.com/test.php?filename=others/staff/gohan.gif

it asks me to download a file called "test". Hey Salam, please tell me what's going on!

ps I made a PHP page called test.php on my site.
 
Back
Top