jamesbrauman Posted September 29, 2008 Share Posted September 29, 2008 Hi, I have a text file with a different filename on each line. I know the current filename, and I want to find the filename of the next file (the value of the line after the line containing the current filename). I have tried this code but it doesnt seem to be working, but I cant figure out for the life of me why: File I am reading from: "picturelist.txt" Our current file: $current_filename The next file: $next_filename // get next filename $fhandle = fopen("picturelist.txt", "r"); $contents = fread($fhandle, filesize("picturelist.txt")); $line_array = explode("\n", $contents); fclose($fhandle); $current_key = 0; while ($line_array[$current_key] != $current_filename) { $current_key++; } $next_filename = $line_array[$current_key + 1]; Could someone please tell me what is wrong with that. Thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/ Share on other sites More sharing options...
.josh Posted September 29, 2008 Share Posted September 29, 2008 // put all lines from file into an array $files = file('picturelist.txt'); // find the value in the array and return the key $key = array_search($current_filename, $files); // assign the value of the next element to var $next_filename = $files[$key+1]; Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652710 Share on other sites More sharing options...
jamesbrauman Posted September 29, 2008 Author Share Posted September 29, 2008 // put all lines from file into an array $files = file('picturelist.txt'); // find the value in the array and return the key $key = array_search($current_filename, $files); // assign the value of the next element to var $next_filename = $files[$key+1]; Thanks for the quick reply, however it is always exiting with "Not found" now (A little addition I made to your nice code). Here is the code I have, including getting the current filename. // get current filename if (isset($_GET['current_filename'])) $current_filename = $_GET['current_filename']; else $current_filename = "0.jpg"; // put all lines from file into an array $files = file('picturelist.txt'); // find the value in the array and return the key $key = array_search($current_filename, $files) or exit("Not found"); // assign the value of the next element to var $next_filename = $files[$key+1]; $previous_filename =$files[$key-1]; And this is what "picturelist.txt" looks like: 0.jpg 1.jpg 2.jpg 3.jpg 4.jpg 5.jpg 6.jpg 7.jpg 8.jpg 9.jpg 10.jpg 11.jpg ... Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652717 Share on other sites More sharing options...
.josh Posted September 29, 2008 Share Posted September 29, 2008 file is in the same directory as script? print_r($files); after assignment see if it is holding the expected array. Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652718 Share on other sites More sharing options...
jamesbrauman Posted September 29, 2008 Author Share Posted September 29, 2008 file is in the same directory as script? print_r($files); after assignment see if it is holding the expected array. Yes it is, I did a print_r anyway.. Array ( [0] => 0.jpg [1] => 1.jpg [2] => 2.jpg [3] => 3.jpg [4] => 4.jpg [5] => 5.jpg [6] => 6.jpg [7] => 7.jpg [8] => 8.jpg [9] => 9.jpg [10] => 10.jpg [11] => 11.jpg [12] => 12.jpg [13] => 13.jpg [14] => 14.jpg [15] => 15.jpg [16] => 16.jpg [17] => 17.jpg [18] => 18.jpg [19] => 19.jpg [20] => 20.jpg [21] => 21.jpg [22] => 22.jpg [23] => 23.jpg [24] => 24.jpg [25] => 25.jpg [26] => 26.jpg [27] => 27.jpg [28] => 28.jpg [29] => 29.jpg [30] => 30.jpg [31] => 31.jpg [32] => 32.jpg [33] => 33.jpg [34] => 34.jpg [35] => 35.jpg [36] => 36.jpg [37] => 37.jpg [38] => 38.jpg [39] => 39.jpg [40] => 40.jpg [41] => 41.jpg [42] => 42.jpg [43] => 43.jpg [44] => 44.jpg [45] => 45.jpg [46] => 46.jpg [47] => 47.jpg [48] => 48.jpg [49] => 49.jpg [50] => 50.jpg [51] => 51.jpg [52] => 52.jpg [53] => 53.jpg [54] => 54.jpg [55] => 55.jpg [56] => 56.jpg [57] => 57.jpg [58] => 58.jpg [59] => 59.jpg [60] => 60.jpg [61] => 61.jpg [62] => 62.jpg [63] => 63.jpg [64] => 64.jpg [65] => 65.jpg [66] => 66.jpg [67] => 67.jpg [68] => 68.jpg [69] => 69.jpg [70] => 70.jpg [71] => 71.jpg [72] => 72.jpg [73] => 73.jpg [74] => 74.jpg [75] => 75.jpg [76] => 76.jpg [77] => 77.jpg [78] => 78.jpg [79] => 79.jpg [80] => 80.jpg [81] => 81.jpg [82] => 82.jpg [83] => 83.jpg [84] => 84.jpg [85] => 85.jpg [86] => 86.jpg [87] => 87.jpg [88] => 88.jpg [89] => 89.jpg [90] => 90.jpg [91] => 91.jpg [92] => 92.jpg [93] => 93.jpg [94] => 94.jpg [95] => 95.jpg [96] => 96.jpg [97] => 97.jpg [98] => 98.jpg [99] => 99.jpg [100] => 100.jpg [101] => 101.jpg [102] => 102.jpg [103] => 103.jpg [104] => 104.jpg [105] => 105.jpg [106] => 106.jpg [107] => 107.jpg [108] => 108.jpg [109] => 109.jpg [110] => 110.jpg [111] => 111.jpg [112] => 112.jpg [113] => 113.jpg [114] => 114.jpg [115] => 115.jpg [116] => 116.jpg [117] => 117.jpg [118] => 118.jpg [119] => 119.jpg [120] => 120.jpg [121] => 121.jpg [122] => 122.jpg [123] => 123.jpg [124] => 124.jpg [125] => 125.jpg [126] => 126.jpg [127] => 127.jpg [128] => 128.jpg [129] => 129.jpg [130] => 130.jpg [131] => 131.jpg [132] => 132.jpg [133] => 133.jpg [134] => 134.jpg [135] => 135.jpg [136] ...etc Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652721 Share on other sites More sharing options...
.josh Posted September 29, 2008 Share Posted September 29, 2008 hmm........ try this: $files = file('picturelist.txt', FILE_IGNORE_NEW_LINES); Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652723 Share on other sites More sharing options...
jamesbrauman Posted September 29, 2008 Author Share Posted September 29, 2008 hmm........ try this: $files = file('picturelist.txt', FILE_IGNORE_NEW_LINES); Not Found I'm going to post my whole code here, a short explaination is I am basically trying to build a file renamer/deleter. changetitles.php <?php // get current filename if (isset($_GET['current_filename'])) $current_filename = $_GET['current_filename']; else $current_filename = "0.jpg"; // put all lines from file into an array $files = file('picturelist.txt', FILE_IGNORE_NEW_LINES); // find the value in the array and return the key $key = array_search($current_filename, $files) or exit("Not found"); // assign the value of the next element to var $next_filename = $files[$key+1]; $previous_filename =$files[$key-1]; // if the command save was posted, rename previous image to the image title that was posted if ($_POST['command'] == "save") { $image_title = $_POST['image_title']; $file_directory= "funnyjunk_pictures/"; rename($file_directory.$previous_filename, $file_directory.$image_title.".jpg"); } // if the command delete was posted, delete previous image if ($_POST['command'] == "delete") { $file_directory = "funnyjunk_pictures/"; unlink($file_directory.$previous_filename); } ?> <HTML> <BODY onLoad="self.focus();document.edittitle.image_title.focus()" style="text-align: center"> <div style="width: 800px; border: 1px solid #CCCCCC; text-align: left"> <?php echo " <center><h1>$current_filename</h1><br /> <img src=\"funnyjunk_pictures/$current_filename\" /> <br /> <form name=\"edittitle\" action=\"changetitles.php?current_filename=$next_filename\" method=\"post\"> <input type=\"hidden\" name=\"command\" value=\"save\" /> <input type=\"hidden\" name=\"previous_picture\" value=\"$current_filename\" /> <input type=\"text\" name=\"image_title\" /> <input type=\"submit\" value=\"Save + Next\" /> </form> <br /> <form action=\"changetitles.php?current_filename=$next_filename\" method=\"post\"> <input type=\"hidden\" name=\"command\" value=\"delete\" /> <input type=\"hidden\" name=\"previous_picture\" value=\"$current_filename\" /> <input type=\"submit\" value=\"Delete + Next\" /> </form> </center> "; ?> </div> </BODY> </HTML> Edit: Forgot picturelist.txt 0.jpg 1.jpg 2.jpg 3.jpg 4.jpg 5.jpg 6.jpg 7.jpg 8.jpg 9.jpg 10.jpg 11.jpg 12.jpg 13.jpg 14.jpg 15.jpg 16.jpg 17.jpg 18.jpg 19.jpg 20.jpg 21.jpg 22.jpg 23.jpg 24.jpg 25.jpg 26.jpg 27.jpg 28.jpg 29.jpg 30.jpg 31.jpg 32.jpg 33.jpg 34.jpg 35.jpg 36.jpg 37.jpg 38.jpg 39.jpg 40.jpg 41.jpg 42.jpg 43.jpg 44.jpg 45.jpg 46.jpg 47.jpg 48.jpg 49.jpg 50.jpg 51.jpg 52.jpg 53.jpg 54.jpg 55.jpg 56.jpg 57.jpg 58.jpg 59.jpg 60.jpg 61.jpg 62.jpg 63.jpg 64.jpg 65.jpg 66.jpg 67.jpg 68.jpg 69.jpg 70.jpg 71.jpg 72.jpg 73.jpg 74.jpg 75.jpg 76.jpg 77.jpg 78.jpg 79.jpg 80.jpg 81.jpg 82.jpg 83.jpg 84.jpg 85.jpg 86.jpg 87.jpg 88.jpg 89.jpg 90.jpg 91.jpg 92.jpg 93.jpg 94.jpg 95.jpg 96.jpg 97.jpg 98.jpg 99.jpg 100.jpg 101.jpg 102.jpg 103.jpg 104.jpg 105.jpg 106.jpg 107.jpg 108.jpg 109.jpg 110.jpg 111.jpg 112.jpg 113.jpg 114.jpg 115.jpg 116.jpg 117.jpg 118.jpg 119.jpg 120.jpg 121.jpg 122.jpg 123.jpg 124.jpg 125.jpg 126.jpg 127.jpg 128.jpg 129.jpg 130.jpg 131.jpg 132.jpg 133.jpg 134.jpg 135.jpg 136.jpg 137.jpg 138.jpg 139.jpg 140.jpg 141.jpg 142.jpg 143.jpg 144.jpg 145.jpg 146.jpg 147.jpg 148.jpg 149.jpg 150.jpg 151.jpg 152.jpg 153.jpg 154.jpg 155.jpg 156.jpg 157.jpg 158.jpg 159.jpg 160.jpg 161.jpg 162.jpg 163.jpg 164.jpg 165.jpg 166.jpg 167.jpg 168.jpg 169.jpg 170.jpg 171.jpg 172.jpg 173.jpg 174.jpg 175.jpg 176.jpg 177.jpg 178.jpg 179.jpg 180.jpg 181.jpg 182.jpg 183.jpg 184.jpg 185.jpg 186.jpg 187.jpg 188.jpg 189.jpg 190.jpg 191.jpg 192.jpg 193.jpg 194.jpg 195.jpg 196.jpg 197.jpg 198.jpg 199.jpg 200.jpg 201.jpg 202.jpg 203.jpg 204.jpg 205.jpg 206.jpg 207.jpg 208.jpg 209.jpg 210.jpg 211.jpg 212.jpg 213.jpg 214.jpg 215.jpg 216.jpg 217.jpg 218.jpg 219.jpg 220.jpg 221.jpg 222.jpg 223.jpg 224.jpg 225.jpg 226.jpg 227.jpg 228.jpg 229.jpg 230.jpg 231.jpg 232.jpg 233.jpg 234.jpg 235.jpg 236.jpg 237.jpg 238.jpg 239.jpg 240.jpg 241.jpg 242.jpg 243.jpg 244.jpg 245.jpg 246.jpg 247.jpg 248.jpg 249.jpg 250.jpg 251.jpg 252.jpg 253.jpg 254.jpg 255.jpg 256.jpg 257.jpg 258.jpg 259.jpg 260.jpg 261.jpg 262.jpg 263.jpg 264.jpg 265.jpg Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652725 Share on other sites More sharing options...
.josh Posted September 29, 2008 Share Posted September 29, 2008 okay. so that optional flag for file() is for php 5+ so you must be using php 4, so instead we have to rtrim each value in the array first: // get current filename if (isset($_GET['current_filename'])) $current_filename = $_GET['current_filename']; else $current_filename = "0.jpg"; // put all lines from file into an array $files = file('picturelist.txt'); // trim each value foreach ($files as $key => $val) { $files[$key] = rtrim($files[$key]); } // find the value in the array and return the key $key = array_search($current_filename, $files); // assign the value of the next element to var $next_filename = $files[$key+1]; $previous_filename =$files[$key-1]; Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652732 Share on other sites More sharing options...
jamesbrauman Posted September 29, 2008 Author Share Posted September 29, 2008 Fantastic, works. Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/126221-get-value-of-next-line/#findComment-652734 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.