Avatar of Marthaj
MarthajFlag for United States of America

asked on 

PHP unlink command and using the second parameter

I have been wondering about the second parameter of the unlink command.
When I use the unlink command, I always use the realpath of the file location.
What exactly does the second parameter do and should I be using it ?
This is how I currently use it:
// DELETE FILES 
   $folder_path = '';
   $files = '';
   
   $folder_path = $_SESSION['TmpWorkPdfsToUpload'];
   $files = glob($folder_path . '/*');  
   foreach($files as $file)
    { 
     if (is_file($file))
      { 
          try 
         {
           
         $strRealLocation = realpath($file);
         chmod($strRealLocation,0777);
         unlink($strRealLocation);
          echo '<BR>DELETED FILE: ' . $strRealLocation . PHP_EOL;
      
          }catch(Exception $e){
            echo 'Message: ' . $e->getMessage() . PHP_EOL;
         }
      }else{
         echo '<BR>NO FILES TO DELETE';
       }
    }

Open in new window

Thank you in advance.

PHP

Avatar of undefined
Last Comment
Marthaj
ASKER CERTIFIED SOLUTION
Avatar of Yujin Boby
Yujin Boby
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of David Favor
David Favor
Flag of United States of America image

2nd parameter only for streams, doesn't apply to your situation.

Tip: One simple test you can do is...

1) Write a short PHP script, with hardcoded absolute file path.

2) All script does is unlink() file + catch catch unlink() return code.

3) Then report EUID, EGID, return code value, absolute file path.

This will tell you the real userid/groupid of the process, along with exact error returned from unlink().

Putting all this data together might provide additional detail.
Avatar of David Favor
David Favor
Flag of United States of America image

Aside: Simple Solution...

Test on your Linux server.

The amount of time you've already invested... and likely additional time you'll invest in the future...

Trying to debug Windows related oddities... if your time is valuable... likely best to abandon Windows...

Since your production system is running Linux, just get your code running on Windows, then ssh into your Linux machine to work with code.
Avatar of Marthaj
Marthaj
Flag of United States of America image

ASKER

Thank you Yujin and David for responding.
I use realpath in my  coding and know the userid etc from using get_current_user etc.
I am hoping to roll out to a Linux operating system here in the near future but in the meanwhile...
PHP
PHP

PHP is a widely-used server-side scripting language especially suited for web development, powering tens of millions of sites from Facebook to personal WordPress blogs. PHP is often paired with the MySQL relational database, but includes support for most other mainstream databases. By utilizing different Server APIs, PHP can work on many different web servers as a server-side scripting language.

125K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo