Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Import script

Status
Not open for further replies.

mufka

ISP
Dec 18, 2000
587
US
I need to create a script in PHP to fetch and manipulate some data. It needs to do the following:

1. Allow the user to browse and select a folder containing files. The folder will contain a number of files sequentially numbered. Half will be .jpg and half will be .txt. The filenames will match. (file1.jpg and file1.txt)

2. Take the name of the jpg file and set as variable $filename.

3. Take the text content of the corresponding txt file and set it as variable $text.

4. Take the base filename and split it into two parts (file and 1) and add those values to variables $file and $number.

5. Repeat until all of the files in the folder have been processed.

I don't think any of this is mysql, just some data manipulation before adding the variables to the database.

Any tips would be appreciated.
 
i'd suggest that the folder should be on the server. that will make things much easier. the server cannot not communicate with the client (using the file protocol) unless the client happens to be on a public IP and/or the same subnet as the server and has filesharing turned on.

assuming it's on the server, in pseudo code
Code:
//allow user choice of director
open the root directory using diropen()
scan a list of all directory using readdir() and is_dir().  remember to ignore the dot/double dot
for each directory entry create an entry in a session array
build an unordered list of checkboxes and directory names
display to the user in a <form> tag
//*******

//process request
grab id of checkbox
lookup the id in the session array and derive directory name
use glob() to grab the jpgs from the requested directory into an array of filenames
send array to next process segment
//********

//separate file name information
//assume the jpgs are in an array called $files
$file = array();
$number=array();
$text=array()
$pattern = '/([a-z]+)(\d+).jpg/i'
foreach ($files as $f){
 //pattern match and assign matches to the $file/$text arrays)
 list ($discard, $file[], $number[]) = preg_match($pattern, $f);
 //get the context of the corresponding text file
 $text[] = file_get_contents(basename($f,'.jpg') . '.txt');
}
//you now have three numerically synchronised arrays of file, number and text
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top