Remove last row from csv file using PHP

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • venkateshm2280
    New Member
    • Jun 2019
    • 1

    Remove last row from csv file using PHP

    Is it possible to remove the last row of the downloaded csv file, and the removed row will gets added in next sheet. May I know is this possible in PHP. As I searched in google and I didnt find any solutions.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    what exactly do you mean with 'next sheet' - removing a line from a file is relatively easy and could be done in different ways - personally i would probably use the file-method so i would have the lines in an array already. now you can do with it whatever you want - like deleting the original file and write all except the last line back to a file and then put the last line wherever you want to.

    PS: note - it will split the lines at newline characters - so if the values in a csv line contain such then it might be necessary to use another method like fgetcsv or such.
    Last edited by gits; Jun 14 '19, 01:59 PM.

    Comment

    • Vanisha
      New Member
      • Jan 2023
      • 26

      #3
      To remove the last row from a CSV file using PHP, you can use the following steps:

      Open the CSV file for reading and writing using the fopen() function.
      Use the fseek() function to move the file pointer to the end of the file.
      Use the fgets() function to read the last line of the file.
      Use the ftruncate() function to truncate the file to remove the last line.
      Close the file using the fclose() function.
      If you want to learn any technologies, join CETPA Infotech.

      Comment

      Working...