ankur0101 Posted December 18, 2011 Share Posted December 18, 2011 Hi, I want to store files to variable array using glob() like $files[0] = xyz.txt $files[1] = pqr.txt . . . $files[n] = nfile.txt I know how to list files from directory using glob() <?php foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?> How can I do that ? Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/ Share on other sites More sharing options...
Zane Posted December 18, 2011 Share Posted December 18, 2011 You mean $files = glob('*txt); Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1299014 Share on other sites More sharing options...
ankur0101 Posted December 19, 2011 Author Share Posted December 19, 2011 No, What I mean s if there are 3 files, then PHP will first count number of files. (May be using for loop or whatever) Then at every count it will do $count++ so as to know how many files exist i.e. number of files Then $files[0] = xyz.txt $files[1] = pqr.txt . . . $files[n] = nfile.txt Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1299256 Share on other sites More sharing options...
scootstah Posted December 19, 2011 Share Posted December 19, 2011 $files = array(); foreach (glob("*.txt") as $filename) { $files[] = $filename; } Like that? If you want to count it just do echo count($files); Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1299260 Share on other sites More sharing options...
trq Posted December 19, 2011 Share Posted December 19, 2011 No, What I mean s if there are 3 files, then PHP will first count number of files. (May be using for loop or whatever) Then at every count it will do $count++ so as to know how many files exist i.e. number of files Then $files[0] = xyz.txt $files[1] = pqr.txt . . . $files[n] = nfile.txt You shouldn't say "No" without testing. Zane's code does exactly what your example shows us you want. Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1299299 Share on other sites More sharing options...
ankur0101 Posted December 21, 2011 Author Share Posted December 21, 2011 I will try and get back here. Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1300084 Share on other sites More sharing options...
ankur0101 Posted December 22, 2011 Author Share Posted December 22, 2011 Hi, It worked. here is my code. <?php $files_in_directory = "home/"; $files = array(); foreach (glob($files_in_directory."*.zip") as $filename) { $files[] = $filename; } $counter = count($files); echo "<p>$counter</p>"; print_r($files); for($i=0; $i<$counter; $i++) { echo "<p>$files[$i]</p>"; } ?> Can I use $files[$i] to upload file using FOR LOOP ? Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1300439 Share on other sites More sharing options...
trq Posted December 22, 2011 Share Posted December 22, 2011 Can I use $files[$i] to upload file using FOR LOOP ? Upload to where? Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1300442 Share on other sites More sharing options...
ankur0101 Posted December 22, 2011 Author Share Posted December 22, 2011 uploading to amazon s3 using AmazonS3::create_mpu_object() Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1300452 Share on other sites More sharing options...
ankur0101 Posted December 22, 2011 Author Share Posted December 22, 2011 Hi, Finally I have uploaded files on amazon s3, but problem is my files are in folder home and glod() shows path like /home/ankur123.zip So problem is it has created folder home on s3 which I dont want. how can I print only name of file ? Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1300460 Share on other sites More sharing options...
trq Posted December 22, 2011 Share Posted December 22, 2011 basename Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1300648 Share on other sites More sharing options...
ankur0101 Posted January 5, 2012 Author Share Posted January 5, 2012 Hello, It worked. Thank you. Link to comment https://forums.phpfreaks.com/topic/253424-how-to-store-file-names-derived-from-glob-in-variable-array/#findComment-1304471 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.