please help me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ramu12rk
Forum Newbie
Posts: 1
Joined: Wed Apr 29, 2009 7:40 am

please help me

Post by ramu12rk »

Iam new to php
so plz give the code for what i required


Write in PHP a program that will scan a directory and all of its sub directories (and their sub sub directories and so on recursively), and populate a table in a MySql database with information about each file. The size, last modified date-time, directory name, file name, file extension for each file must be entered as a row in the MySql table.

Code to run in PHP Version 5.1.6 using MySql server 5.0.45 called by

http://serverip/scandir.php

to populate a MySql database, given the following settings, with the following information:

Settings defined as variables in the php code:

To define the sql server, connection, Database and Table details, variables:
$SqlServerHost, $SqlServerPort, $SqlUsername, $SqlPassword, $SqlDbName, $SqlTableName are to be declared and set by you.

$dirtostart - defines the unix or Windows path name of the root where the scan will start from. If you develop this on WIndows, you could set this to "C:\" or "C:\WINDOWS\". If you develop on linux, you would set this to "/usr/" or "/etc/httpd/" or another valid directory.

The table will have the following fields, and if the table does not exist, the code is to create it. The code can assume the database exists and the user has full privileges.

CREATE TABLE files (
prikey INT PRIMARY KEY AUTO_INCREMENT,
dir VARCHAR(200),
filename VARCHAR(200),
extension VARCHAR(50),
modified DATETIME,
size INT UNSIGNED,
version INT UNSIGNED,
filefound INT
)

"version" field:

if a row already exists in the table for a file with the same directory, file name and extension, then the code is to check the size and last modified fields. If either or both of the size or datetime fields are different, then the code is to UPDATE the size and last modified date time fields to their new value and INCREMENT the 'version' field. If neither of the size nor lastmodified fields have changed - that is the file is identical to the last scan, then only set the 'found' field to 1 for that row.


"found" field:

At the beginning of execution, your code is to execute a
UPDATE files SET filefound=0;
sql command.

During the code whenever a file is found that already has a row with the same directory, file name and extension - whether or not it has the same or different modified and/or size fields - the filefound field must be set to 1. This field is designed to allow me to perform a 'select filename, version,filefound from files WHERE filefound=0;' sql command and identify the files that have been deleted from the directory tree. Performing this query is not part of this scope of work - only setting up the data in the table to allow this to be performed is part of the scope.


Output from the code to the screen (using eho commands) is to be one of:
"Cannot connect to SQL using:" and list the SQL parameters
"Cannot open the database named " and database name
"Cannot create table " table name
"Cannot insert into table " table name
or any other descriptive error message,

or

"Completed. Updated " how many rows of the table were updated " rows and added " how many new rows were added " rows. Found " how many rows only had their found filed set to 1 " files that had not changed."

The deliverable for this project is the file "scandir.php". I will then put it on my apache server with php, and create the sql database and user required, and check the code works as required.

You may find this link useful to see how to recurse directories in php:
http://forums.thewickedflea.com/showthread.php?tid=26

On linux, you may assume that the user account that is running the apache server has read access to the directory being scanned.
Mark Baker
Forum Regular
Posts: 710
Joined: Thu Oct 30, 2008 6:24 pm

Re: please help me

Post by Mark Baker »

Sounds like a homework assignment to me
Post Reply