troubleshooting Question

PHP Unlink Not Working

Avatar of dogsareit
dogsareitFlag for United States of America asked on
PHP
15 Comments1 Solution23 ViewsLast Modified:
Hello folks. I am using Windows 10, ocalhost and PHP 5.6.04. I need some help here please - getting desperate!
I can not get 'unlink' to work. It simple will not delete my files.
The flow of the program is that I read each email from an email box.
If the email has an PDF attachment, I extract it and write to the folder 'workExtractedFiles'
The email may have one or more PDF attachments.
I then execute some validation and if it passes, I upload it to a AWS bucket.
Then BEFORE I read the next email, I need to delete the files that have been uploaded so they are not uploaded again which results under the wrong person.
But, they won't delete. I have posted coding where I extract and write the PDFS to files.
It works very well. Originally I used put_contents but after running into a problem deleting
the files, I switched to using fwrite etc. The odd thing is it will delete the files in the
workPdfsToUpload, but not the 'workExtractedFiles' folder.
I compared the properties of both folders, 'workExtractedFiles' and 'workPdfsToUpload' and they are the same including permissions etc.
I am running out of time in resolving this issue.
I have posted the section that initially write it to a file and the section that uploads
and deletes it (supposedly).
I had also created a function to delete the files in the folders and it will delete the
files EXCEPT for the files in workExtractedFiles.
Can someone please help me.
Thank you in advance for any help.
Below is the code I use to write out thePDF after extracting it and it works very well.
 if($filename) 
         {
            $attachment = getPart($inbox, $email_number, $partNumber, $part->encoding);
            
             if (stripos($filename,'.pdf') == 0)
             {
               echo '<BR><BR>NOT A PDF FILE - SKIPPING: ' . $filename;
                $_SESSION['GrdNbrOfInvalidPdfs'] = ($_SESSION['GrdNbrOfInvalidPdfs'] + 1);
                $wrkNotPdf = './workNonPdfs/' . $filename;
                //file_put_contents($wrkNotPdf,$attachment) or die('FPUT CONTENTS FAILED');
                $wrkSavePdf = './workNonPdfs/' . $filename;
                  $strSavArrayFileName = $filename;
                  $fp = fopen($wrkSavePdf, 'w+');
                    fwrite($fp, $attachment);
                  fclose($fp);   
                    chmod($wrkSavePdf,0777);               
                echo '<BR><BR>ERROR ** NOT A PDF FILE - WROTE TO WORKNONPDFS FOLDER FILENAME: ' . $filename;
                 $strLogMsg = 'ERROR - NOT PDF FILE - MOVED TO WorkNonPdfs FOLDER- FILENAME: ' . strval($filename) . PHP_EOL;
                 RtnWriteLogMsg($strLogMsg);
                 $_SESSION['GrdNbrOfInvalidPdfs'] = ($_SESSION['GrdNbrOfInvalidPdfs'] + 1);
              }else{
               //SECTION TO WRITE ATTACHMENT TO FILE - WORKFOLDER
                 $wrkSavePdf = './workExtractedFiles/' . $filename; 
                  $strSavArrayFileName = $filename;
                  $fp = fopen($wrkSavePdf, 'w+');
                    fwrite($fp, $attachment);
                  fclose($fp);
                    chmod($wrkSavePdf,0777);               
               // COMMENTED OUT 05-28 AT 6-23 PM
                // file_put_contents($wrkSavePdf, $attachment) or die('FPUT CONTENTS FAILED');
               $strLogMsg = 'VALID PDF FILE - WROTE TO WORKEXTRACTEDFILES FOLDER FILENAME: ' . strval($filename) . PHP_EOL;
                 $_SESSION['NbrAttachs'] = ($_SESSION['NbrAttachs'] + 1);
                $_SESSION['GrdNbrOfValidPdfs'] = ($_SESSION['GrdNbrOfValidPdfs'] + 1 );
                echo '<BR>$_SESSION[GrdNbrOfValidPdfs]: ' . $_SESSION['GrdNbrOfValidPdfs'];
                RtnWriteLogMsg($strLogMsg);
                 echo '<BR><BR>VALID PDF FILE - WROTE TO WORKEXTRACTEDFILES FOLDER FILENAME:' . $filename;
              }

And this is the code to read the folder and upload to AWS - won't delete my files. says it is, but they are still there and hence when I read the next email, they are uploaded again under the current person. And I know I execute the RtnToDelete after the foreach loop ends but I thought what the heck.
Function RtnWrtFolder($strSupplierCode,$strConfirmNbr,$strSwitch)
{
 
    // READ EACH PDF IN THE WORKEXTRACTED FOLDER 
    $folder_path = './workExtractedFiles'; 

    $files = glob($folder_path . '/*');  

   foreach($files as $filename)
        {
            echo '<BR><BR>PROCESSING FILE: ' . $filename;
            echo '<BR>SUPPLIER & CONF.NBR MATCHES - PROCESSING PDFS FROM EMAIL';
            echo '<BR>WRITING PDFS TO AGENTS FOLDER';
                
         // CREATE NEW FILENAME FOR UPLOADING
         $strOnlyFileName = str_replace('./workExtractedFiles/', '',$filename);
         
         $wrkNewFile = trim('./workPdfsToUpload/') . $strOnlyFileName;
         
          $fp = fopen($wrkNewFile, 'w+');
            fwrite($fp, $filename);
          fclose($fp);
            chmod($wrkNewFile,0777);
      
          // DEFINE PATHING FROM WHERE TO FIND FILE AND WHERE TO UPLOAD
         $wrkProdFileLocation = $filename;
         $wrkFileName = $filename;
         $wrkAWSFileLocation = $_SESSION['RegionsLocation'] . '/' . $strOnlyFileName;
            
         // UPLOAD TO AWS BUCKET
         echo '<br>$wrkProdFileLocation: ' . $wrkProdFileLocation;
         echo '<br>$wrkAWSFileLocation: ' . $wrkAWSFileLocation;
         
          RtnAWSUpload($wrkProdFileLocation,$wrkAWSFileLocation);
         
         // DELETE FILE FROM workPdfsToUpload FOLDER
         if (is_file($wrkNewFile))
            {
                 unlink($wrkNewFile); 
               }
         // DELETE FILE FROM ./workExtractedFiles/ FOLDER
         if (is_file($filename))
            {
                 unlink($filename); 
               }
         
         // INSERT RECORD INTO TABLE 
         $strSwitch = 'REGION';
         RtnInsertFileName($wrkFileName,$strSupplierCode,$strConfirmNbr,$strSwitch);
      }
      
   // DELETE FILES IN ALL WORK FOLDERS
   RtnToDeleteFiles();
}

Function RtnToDeleteFiles()
{
// DELETE FILES IN workExtractedFiles 
$folder_path = './workExtractedFiles'; 
$files = glob($folder_path . '/*');  
foreach($files as $file)
   { 
    if(is_file($file)) 
        unlink($file); 
       //ECHO '<BR>$file: ' , $file;
      $strLogMsg = 'DELETING PROCESSED EXTRACTED FILE: ' .  $file;
      RtnWriteLogMsg($strLogMsg);
   }
   
// DELETE FILES IN workPdfsToUpload   
$folder_path = './workPdfsToUpload'; 
$files = glob($folder_path . '/*');  
foreach($files as $file)
   { 
    if(is_file($file)) 
       unlink($file);
       // ECHO '<BR>$file: ' , $file;
      $strLogMsg = 'DELETING PDF UPLOADED FILE: ' .  $file;
      RtnWriteLogMsg($strLogMsg);
   }


// DELETE FILES IN workErrors   
$folder_path = './workErrors'; 
$files = glob($folder_path . '/*');  
foreach($files as $file)
   { 
   
    if(is_file($file)) 
        unlink($file); 
      //  ECHO '<BR>$file: ' , $file;
      $strLogMsg = 'DELETING PDF UPLOADED FILE: ' .  $file;
      RtnWriteLogMsg($strLogMsg);
   }
}

Any help appreciated. Thank you in advance.








Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 15 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros