1

Hello im trying to make a script that deletes some images from the server. I read every single post about unlink() and the major permissions / wrong directory errors , link1 , link2 and i tried every solution i found but still cant find whats wrong.

 $baseDir=dirname(__DIR__).'/media/com_iproperty/pictures/';
 $deleteImage=$prefix.'_'.$imgParts[0].'.'.$imgParts[$imgLen-1];
 var_dump($deleteImage); //196040_DSCN2675.JPG
 $deleteThumb=$prefix.'_'.$imgParts[0].'_thumb.'.$imgParts[$imgLen-1];
 $user='root';
 var_dump($baseDir); ///var/www/vhosts/spiti360.gr/httpdocs/demo2/media/com_iproperty/pictures/
 $commandyo='rm '.$baseDir.$deleteImage;
 if (file_exists($baseDir.$deleteImage)) {
   echo "exists";
   chown($baseDir.$deleteImage,$user);
   chmod($baseDir.$deleteImage,0777);
   //system($commandyo);
   unlink($baseDir.$deleteImage);
 }                         
 die();

I always take as output the "exists" string, that means that the file exists and it isnt a directory error.

When i use :

root@server ~ # locate 196040_DSCN2675.JPG

Output:

/var/www/vhosts/spiti360.gr/httpdocs/demo2/media/com_iproperty/pictures/196040_DSCN2675.JPG

The image didnt deleted.

I enabled the error display and i take this:

Warning: chown(): Operation not permitted in /var/www/vhosts/spiti360.gr/httpdocs/demo2/custom_scripts/eAgentSingle.php on line 562

Warning: chmod(): Operation not permitted in /var/www/vhosts/spiti360.gr/httpdocs/demo2/custom_scripts/eAgentSingle.php on line 563

Im using both chown and chmod on file and i also chmod 777 on pictures folder as i read from here but nothing seems to change.

drwxrwxrwx 2 root root 26333184 Jul  6 13:33 pictures

I even tried to use system and rm command but didnt work also. I aknowledge that this is a permission error , but whatever solution i used found from stackoverflow or google wont work.

Do i miss something ?

EDIT: I used : chown -R root:www-data /var/www/vhosts/spiti360.gr/httpdocs/demo2/media/com_iproperty/pictures/

and i set usergroup for pictures folder to www-data

drwxrwxrwx 2 root www-data 26333184 Jul  6 14:15 pictures

But still doesnt work.

Community
  • 1
  • 1
IseNgaRt
  • 601
  • 1
  • 4
  • 22

1 Answers1

0
drwxrwxrwx 2 root root 26333184 Jul  6 13:33 pictures

You have to set the user and/or group to www-data, i can't remember if both have to be, otherwise apache won't have permissions to delete it.

Jesper
  • 3,816
  • 2
  • 16
  • 24
  • 1
    Only the group needs to be www-data – Lost F. Jul 06 '15 at 11:55
  • I used this : `root@server ~ # sudo chgrp -R www-data /var/www/vhosts/spiti360.gr/httpdocs/demo2/media/com_iproperty/pictures/` `drwxrwxrwx 2 root www-data 26333184 Jul 6 13:33 pictures` Still wont work. – IseNgaRt Jul 06 '15 at 12:08
  • You tried to edit the group on the image too ? Since you're trying to chmod and chown on the image, the image has to have group too. – Jesper Jul 06 '15 at 12:12
  • Yeap i used chgrp to www-data on image also – IseNgaRt Jul 06 '15 at 12:24
  • Can i ask why you're doing the chmod and chown just before deleting a image ? Have you tried just to unlink it directly ? – Jesper Jul 06 '15 at 12:33