php_novice2007 Posted March 17, 2007 Share Posted March 17, 2007 Hi, How would I write some php code that opens up a text file, looks for a specific line, and delete it? Basically I have a text file called photoList.txt, which has a list of all the photos stored, one line at a time. And if I want to delete the photo, I basially have to delete it from photoList.txt. I can't figure out the logic.. Thanks Link to comment https://forums.phpfreaks.com/topic/43068-finding-a-line-in-a-file/ Share on other sites More sharing options...
Barand Posted March 17, 2007 Share Posted March 17, 2007 here's one way <?php /** * Suppose we want to delete c.jpg */ $delete = 'c.jpg'; /** * read file into an array */ $photos = file('photolist.txt'); /** * find position of file to be deleted */ $index = array_search($delete."\n", $photos); /** * remove it from the array */ unset($photos[$index]); /** * write array to the file */ file_put_contents('photolist.txt', $photos); ?> Link to comment https://forums.phpfreaks.com/topic/43068-finding-a-line-in-a-file/#findComment-209239 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.