By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,629 Members | 1,480 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,629 IT Pros & Developers. It's quick & easy.

php image upload ,resize &making thumbnail

P: 23
hii all
Truly telling i hav got this code from net &
i am finding error while running the code below..

code:-
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $idir = "photo/";   // Path To Images Directory
  3. $tdir = "photo/thumbs/";   // Path To Thumbnails Directory
  4. $twidth = "125";   // Maximum Width For Thumbnail Images
  5. $theight = "100";   // Maximum Height For Thumbnail Images
  6.  
  7. if (!isset($_GET['subpage'])) {   // Image Upload Form Below   ?>
  8.   <form method="post" action="addphoto.php?subpage=upload" enctype="multipart/form-data">
  9.    File:<br />
  10.   <input type="file" name="imagefile" class="form">
  11.   <br /><br />
  12.   <input name="submit" type="submit" value="Sumbit" class="form">  <input type="reset" value="Clear" class="form">
  13.   </form>
  14. <? } else  if (isset($_GET['subpage']) && $_GET['subpage'] == 'upload') {   // Uploading/Resizing Script
  15.   $url = $_FILES['imagefile']['name'];   // Set $url To Equal The Filename For Later Use
  16.   if ($_FILES['imagefile']['type'] == "image/jpg" || $_FILES['imagefile']['type'] == "image/jpeg" || $_FILES['imagefile']['type'] == "image/pjpeg") {
  17.     $file_ext = strrchr($_FILES['imagefile']['name'], '.');   // Get The File Extention In The Format Of , For Instance, .jpg, .gif or .php
  18.     $copy = copy($_FILES['imagefile']['tmp_name'], "$idir" . $_FILES['imagefile']['name']);   // Move Image From Temporary Location To Permanent Location
  19.     if ($copy) {   // If The Script Was Able To Copy The Image To It's Permanent Location
  20.       print 'Image uploaded successfully.<br />';   // Was Able To Successfully Upload Image
  21.       $simg = imagecreatefromjpeg("$idir" . $url);   // Make A New Temporary Image To Create The Thumbanil From
  22.       $currwidth = imagesx($simg);   // Current Image Width
  23.       $currheight = imagesy($simg);   // Current Image Height
  24.       if ($currheight > $currwidth) {   // If Height Is Greater Than Width
  25.          $zoom = $twidth / $currheight;   // Length Ratio For Width
  26.          $newheight = $theight;   // Height Is Equal To Max Height
  27.          $newwidth = $currwidth * $zoom;   // Creates The New Width
  28.       } else {    // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height)
  29.         $zoom = $twidth / $currwidth;   // Length Ratio For Height
  30.         $newwidth = $twidth;   // Width Is Equal To Max Width
  31.         $newheight = $currheight * $zoom;   // Creates The New Height
  32.       }
  33.       $dimg = imagecreate($newwidth, $newheight);   // Make New Image For Thumbnail
  34.       imagetruecolortopalette($simg, false, 256);   // Create New Color Pallete
  35.       $palsize = ImageColorsTotal($simg);
  36.       for ($i = 0; $i < $palsize; $i++) {   // Counting Colors In The Image
  37.        $colors = ImageColorsForIndex($simg, $i);   // Number Of Colors Used
  38.        ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']);   // Tell The Server What Colors This Image Will Use
  39.       }
  40.       imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight);   // Copy Resized Image To The New Image (So We Can Save It)
  41.       imagejpeg($dimg, "$tdir" . $url);   // Saving The Image
  42.       imagedestroy($simg);   // Destroying The Temporary Image
  43.       imagedestroy($dimg);   // Destroying The Other Temporary Image
  44.       print 'Image thumbnail created successfully.';   // Resize successful
  45.     } else {
  46.       print '<font color="#FF0000">ERROR: Unable to upload image.</font>';   // Error Message If Upload Failed
  47.     }
  48.   } else {
  49.     print '<font color="#FF0000">ERROR: Wrong filetype (has to be a .jpg or .jpeg. Yours is ';   // Error Message If Filetype Is Wrong
  50.     print $file_ext;   // Show The Invalid File's Extention
  51.     print '.</font>';
  52.   }
  53. } ?>
  54.  
error :-
Expand|Select|Wrap|Line Numbers
  1. Warning: copy(photo/DSC00031.JPG): failed to open stream: Permission denied in /hsphere/local/home/wwwuser/home.mishra.biz/addphoto.php on line 18 ERROR: Unable to upload image.
  2.  
line 18 is $copy=....

anyone can help me i would b grateful..
Nov 21 '07 #1
Share this Question
Share on Google+
7 Replies


Markus
Expert 5K+
P: 6,050
Sounds like you dont have the right file permissions set.

You need to chmod the directory to a more appropriate level!
[php]
<?php
chmod("/directory/to/chmod", 0755);
?>
[/php]

Lemme know if this helps!

Also! Remember to wrap your php code in the CODE tags.

IT helps people look through your code :)
Nov 21 '07 #2

Atli
Expert 5K+
P: 5,058
Hi.

Please use [code] tags when posting your code examples. (See How to ask a question)

[code=php] ...PHP code goes here... [/code]

Thank you.
Nov 21 '07 #3

P: 23
Sounds like you dont have the right file permissions set.

You need to chmod the directory to a more appropriate level!
[php]
<?php
chmod("/directory/to/chmod", 0755);
?>
[/php]

Lemme know if this helps!

Also! Remember to wrap your php code in the CODE tags.

IT helps people look through your code :)
i hav executed the code u said
Expand|Select|Wrap|Line Numbers
  1.       <?php
  2.       chmod("/directory/to/chmod", 0755);
  3.         ?>
  4.  
  5.  
but it gives error , i replaced 'directory' by 'photo' the folder i hav created to store uploaded photo..but still it is giving error

see the error:-

Warning: chmod(): No such file or directory in /local/home/wwwuser/home.mishra.biz/chmod.php on line 2


please tell me what else to do..
i need the solution soon..
if u can i will be obliged..
Nov 23 '07 #4

Atli
Expert 5K+
P: 5,058
How exactly did the path you use look?

If you want to use a relative path, a path relative to the current directory, there should not be a / in front of it.

On the other hand, if you are using an absolute path, a path relative to the root directory, you should put a / in front of it.

Like:
Expand|Select|Wrap|Line Numbers
  1. # An absolute path
  2. chmod("/var/www/html/images/image.jpg", 0755);
  3.  
  4. # A relative path, assuming the current directory is /var/www/html
  5. chmod("images/image.jpg", 0755);
  6.  
Nov 23 '07 #5

Markus
Expert 5K+
P: 6,050
It's not the image he's wanting to chmod() is it?

Am i right in thinking he would also need to chmod the php file that is executing? i.e. the uploader script aswell as the actual directory where the images would be stored? Or is that not necessary?
Nov 23 '07 #6

Atli
Expert 5K+
P: 5,058
It's not the image he's wanting to chmod() is it?

Am i right in thinking he would also need to chmod the php file that is executing? i.e. the uploader script aswell as the actual directory where the images would be stored? Or is that not necessary?
It doesn't really matter whether it is a file or a directory. All directories in a Linux system are essentially files anyways so the chmod command works the same on both.

He would not have to chmod the PHP file that is being executed, only the file or directory he intends to change.

The user running the HTTP server only needs read and execute permission on the PHP file to be able to execute it, but it needs write permission on the file the PHP script is meant to change.
Nov 24 '07 #7

Markus
Expert 5K+
P: 6,050
It doesn't really matter whether it is a file or a directory. All directories in a Linux system are essentially files anyways so the chmod command works the same on both.

He would not have chmod the PHP file that is being executed, only the file or directory he intends to change.

The user running the HTTP server only needs read and execute permission on the PHP file to be able to execute it, but it needs write permission on the file the PHP script is meant to change.
Oh i see.

I wonder how (s)he's getting along with it _
Nov 24 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.