SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Talking Need help to change perms on image uploads

    Hi

    I have a fully functional image upload script. The problem is that when I try to delete images using unlink($image); it does not work.

    PHP Code:
          //delete thumbnail and main image
          
          
          
    if($_REQUEST[delt]):
              
    $r=mysql_query("delete from " $prev "property_pics where pid=" $_REQUEST[pid]);
      
    //line 178        unlink($_REQUEST[source]);
      //line 179        unlink($_REQUEST[thumb]);
              
    if($r):

                     echo
    "<script>alert('1 Photo successfully deleted');window.location.href='" .$_SERVER[PHP_SELF] . "?id=" $_REQUEST[id] . "';</script>";
                  echo
    "<br><table  align='center' cellpadding='5' cellspacing='0' width='70%' style='border:solid 1px $dark'><tr><td align='center' height='25'><div class='lnk'>" .$msg "</div></td></tr></table><br>"
              endif; 
    I get the following error messages:
    Warning: unlink() [function.unlink]: Success in /prop.pics.php on line 178
    Warning: unlink() [function.unlink]: Permission denied in /prop.pics.php on line 179


    When the files are uploaded they are designated 644 and assigned to owner 99 99 and no group on the server. I think I need to somehow assign them to my ownership and group??

    Thanks in advance for all the help I know you guys will as usual offer on this

  2. #2
    SitePoint Enthusiast
    Join Date
    May 2008
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this function

    umask(0); //set the permission to '777'

  3. #3
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are there any security issues using umask(0); ? I am not familiar with this at all I expected a variant of chmod() would be necessary.

    It is important that I get this to work not only correctly but of course securely. I have the following code to upload and manipulate the images, at what point should I be setting the perms?

    PHP Code:
         if($_POST[sbmt]):
                 for($i=0;$i<count($_FILES[foto][tmp_name]);$i++):
                if($_FILES[foto][tmp_name][$i]):
                   $r=mysql_query("insert into " . $prev . "property_pics(id) values (\"" . $_REQUEST[id] . "\")");
                    $pid=mysql_insert_id();
                   $e=explode(".",$_FILES[foto][name][$i]);
                   $ext=substr($_FILES[foto][name][$i],-3,3); 
                   $size=$foto_size;
                   $ext=strtolower($e[1]);
                   if($ext=="peg"){$ext="jpg";}
                   copy($_FILES[foto][tmp_name][$i],"../pics/" . $pid . "." . $ext);
                   copy($_FILES[foto][tmp_name][$i],"../pics/thumb-" . $pid . "." . $ext);
                       
                  
                              
                   mysql_query("update " . $prev . "property_pics set file='" .  $pid . "." . $ext . "',thumb='thumb-" . $pid . "." . $ext . "' where pid=" . $pid);                
                   $nw=145;$nh=133;
                   $img=$tpath . "/thumb-" . $pid . "." . $ext;
                   $dimensions = GetImageSize($img);
                   $thname = $img;
                   $w=$dimensions[0];
                   $h=$dimensions[1];
                   if($ext=="jpg"):
                       $img2 = ImageCreateFromJpeg($img);
                   elseif($ext=="gif"):
                      $img2 = ImageCreateFromGif($img);
                   elseif($ext=="png"): 
                      $img2 = ImageCreateFromPng($img);
                   endif;    
                   $thumb=ImageCreateTrueColor($nw,$nh);
                   $wm = $w/$nw;
                   $hm = $h/$nh;
         
                    $h_height = $nh/2;
                   $w_height = $nw/2;
                   if($w > $h)
                   {
                       $adjusted_width = $w/$hm;
                       $half_width = $adjusted_width / 2;
                       $int_width = $half_width - $w_height;
                       ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                       if($ext=="jpg"):
                            ImageJPEG($thumb,$thname,95); 
                       elseif($ext=="gif"):
                            ImageGIF($thumb,$thname,95); 
                       elseif($ext=="png"): 
                            @ImagePNG($thumb,$thname,95); 
                       endif;    
                    }
                    elseif(($w < $h) || ($w == $h))
                    {
                        $adjusted_height = $h / $wm;
                        $half_height = $adjusted_height / 2;
                        $int_height = $half_height - $h_height;
                        ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                        if($ext=="jpg"):
                            ImageJPEG($thumb,$thname,95); 
                        elseif($ext=="gif"):
                           ImageGIF($thumb,$thname,95); 
                        elseif($ext=="png"): 
                           ImagePNG($thumb,$thname,95); 


                        endif;    
                    }
                    else
                    {
                          ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);     
                        if($ext=="jpg"):
                           ImageJPEG($thumb,$thname,95); 
                        elseif($ext=="gif"):
                           ImageGIF($thumb,$thname,95); 
                        elseif($ext=="png"): 
                             ImagePNG($thumb,$thname,95); 
                        endif;    
                     }
                     imagedestroy($img2);
                     
                       $target=420;
                       $img=$tpath . "/" . $pid . "." . $ext;
                       $dimensions = GetImageSize($img);
                       $thname = $img;
                       $w=$dimensions[0];
                       $h=$dimensions[1];
                       $width=$w;
                       $height=$h;
                       if(($width>$target && $height>$target) || $width>$target):
                           $cent=100-round((($width-$target)/$width)*100);
                           $width=round($width*$cent/100);
                           $height=round($height*$cent/100);
                       elseif($height>$target):
                           $cent=100-round((($height-$target)/$height)*100);
                           $width=round($width*$cent/100);
                           $height=round($height*$cent/100);
                       endif;
                       if(($width>$target && $height>$target)|| $width>$target):
                            $cent=100-round((($width-$target)/$width)*100);
                            $width =round($width*$cent/100);
                            $height=round($height*$cent/100);
                       elseif($height>$target):
                            $cent=100-round((($height-$target)/$height)*100);
                            $width =round($width*$cent/100);
                            $height=round($height*$cent/100);
                       endif;
                       $nw=$width;$nh=$height;
                       
                       if($ext=="jpg"):
                           $img2 = ImageCreateFromJpeg($img);
                       elseif($ext=="gif"):
                          $img2 = ImageCreateFromGif($img);
                       elseif($ext=="png"): 
                          $img2 = ImageCreateFromPng($img);
                       endif;    
                       $thumb=ImageCreateTrueColor($nw,$nh);
                       $wm = $w/$nw;
                       $hm = $h/$nh;
             
                        $h_height = $nh/2;
                       $w_height = $nw/2;
                       if($w > $h)
                       {
                           $adjusted_width = $w/$hm;
                           $half_width = $adjusted_width / 2;
                           $int_width = $half_width - $w_height;
                           ImageCopyResampled($thumb,$img2,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h); 
                           if($ext=="jpg"):
                                ImageJPEG($thumb,$thname,95); 
                           elseif($ext=="gif"):
                                ImageGIF($thumb,$thname,95); 
                           elseif($ext=="png"): 
                                ImagePNG($thumb,$thname,95); 
                           endif;    
                        }
                        elseif(($w < $h) || ($w == $h))
                        {
                            $adjusted_height = $h / $wm;
                            $half_height = $adjusted_height / 2;
                            $int_height = $half_height - $h_height;
                            ImageCopyResampled($thumb,$img2,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); 
                            if($ext=="jpg"):
                                ImageJPEG($thumb,$thname,95); 
                            elseif($ext=="gif"):
                               ImageGIF($thumb,$thname,95); 
                            elseif($ext=="png"): 
                               ImagePNG($thumb,$thname,95); 
                            endif;    
                        }
                        else
                        {
                              ImageCopyResampled($thumb,$img2,0,0,0,0,$nw,$nh,$w,$h);     
                            if($ext=="jpg"):
                               ImageJPEG($thumb,$thname,95); 
                            elseif($ext=="gif"):
                               ImageGIF($thumb,$thname,95); 
                            elseif($ext=="png"): 
                                 ImagePNG($thumb,$thname,95); 
                            endif;    
                         }
                         imagedestroy($img2);
                    endif;
                endfor;
     
                    echo"<script>alert('Photo successfully Updated');window.location.href='property.pictures.php?id=" . $_REQUEST[id] . "';</script>";        
             endif;      
          
          $r=mysql_query("select * from " . $prev . "property where id=". $id);
          $d=@mysql_fetch_array($r);
          ?>
          
         
          
           <table width="70%" border="0" align="center" cellpadding=3 cellspacing=1 bgcolor="<?php echo $dark?>">
           <tr class="header"><td height="25" colspan="3" >&nbsp;<a href='property.list.php?id=<?php echo $_REQUEST[id]?>' class=header><u>Property Management</u></a> > <?php echo $d[propertyref]?> # Upload Pictures </td>
           <td align="right"><input type="button" class="lnk" value='Add New' onClick="javascript:window.location.href='new.property.pics.php?id=<?php echo $_REQUEST[id]?>'"></td></tr>
           
           
           
           <tr class="lnk" bgcolor="white">       
          <br>
          
          
          
          
           <form method="post" action="<?php echo $_SERVER['PHP_SELF']?>?id=<?php echo $_REQUEST[id]?>" enctype="multipart/form-data">
          <table width="70%" border="0" align="center" cellpadding="3" cellspacing="1" bgcolor="<?php echo $dark?>">
          <input type="hidden" name="id" value="<?php echo $_REQUEST[id]?>">
          <tr class=lnk bgcolor="#ffffff"><td valign="top">Photo</td>
          <td><table width="98%"  border="0" cellspacing="0" cellpadding="4" align="center">      
          <?php
          
    for($i=1;$i<=12;$i++):
              echo
    "<tr><td><input type='file' name='foto[]' class='lnk' size='35'></td></tr>\n";      
          endfor;
          
    ?>
          <tr bgcolor="#FFFFFF" class="lnk"><td><strong>Image type should be .jpg</strong></td></tr>
          </table>
          <input type="hidden" name="source" value="<?php echo $source?>">
          <input type="hidden" name="thumb" value="<?php echo $thumb?>"></td></tr>
         
          <tr bgcolor="<?php echo $dark?>"><td align="center" height="25" class="header" colspan="2"><input type="submit" name="sbmt" value='Submit  Photo' class="lnk">&nbsp;&nbsp;<input type="Button"  value="Back" onClick="JavaScript:window.location.href='property.pictures.php?id=<?php echo $id?>';" class="lnk"></td></tr></table>
          </form>
    regards
    C

  4. #4
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Having pondered over this some more I do not quite understand why PHP cannot delete an image that it has created?

    If user "server" for instance creates a file why can it not delete it also. The images can be viewed OK on the webpage the perms are set to 644.

    Any answers?

    C

  5. #5
    SitePoint Evangelist ColinHughes's Avatar
    Join Date
    Sep 2004
    Location
    Spain
    Posts
    473
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Is this a PHP issue or an Apache issue? Have I posted this to the correct forum?

    Would it be within the forum rules for me to also ask the above question on the Apache forum?

    Thanx


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •