Hello,

I want a simple Php script which does the following:

(a) List all file names alphabetically such that the LAST 3 letters of the file name match with one of a few strings entered by the user at runtime
For eg, two strings PDF and TXT are specified by user at runtime, and in the directory there are 4 files: abcpdf.zip, abctxt.zip, xyzpdf.zip and xyztxt.zip
Then the script should create the following records in a mysql database:
(1) From the files abcpdf.zip and abctxt.zip, the script should extract the string 'abc' and create a record in table 'A' with the string abc as one of the fields
(2) In another table 'B' there should be one record for abctxt.zip and abcpdf.zip
This should also be done for xyzpdf.zip and xyztxt.zip, and for all files which have last 3 letters as 'pdf' or 'txt'.

At run time, upto 6 different strings (like 'pdf' or 'txt' should be specified by the user)

FOr the mysql records kindly create a generic statement of inserting data into a table, i will update that statement with the actual data required

Thanks in advance,
Arvind

Dani AI

Generated

The goal from is straightforward: scan a folder, pick files whose base name ends with one of a small list of suffixes provided at runtime, extract the prefix (the part before that suffix) and store results in two tables. was right to point toward filesystem + string parsing; pushed for doing some work yourself — below is a compact, practical example that does the heavy lifting, uses safe DB practices and notes a few edge cases to watch for.

This example treats the suffix match against the filename without its final extension (so abcpdf.zip -> base abcpdf -> suffix pdf -> prefix abc). It uses prepared statements via PDO, wraps inserts in a transaction, and avoids duplicate prefixes by relying on a UNIQUE key on A.prefix.

<?php
// config
$dir = __DIR__ . '/files';
$suffixString = 'pdf,txt'; // replace with runtime input (POST/CLI)
$suffixes = array_filter(array_map('strtolower', array_map('trim', explode(',', $suffixString))));

// DB (set your host/db/user/pass)
$pdo = new PDO('mysql:host=127.0.0.1;dbname=yourdb;charset=utf8mb4','dbuser','dbpass',[
  PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION
]);

$insA = $pdo->prepare('INSERT INTO A (prefix) VALUES (:prefix) ON DUPLICATE KEY UPDATE id=id');
$insB = $pdo->prepare('INSERT INTO B (filename, prefix, suffix) VALUES (:filename, :prefix, :suffix)');

$pdo->beginTransaction();
foreach (new DirectoryIterator($dir) as $f) {
  if (!$f->isFile()) continue;
  $filename = $f->getFilename();
  $base = pathinfo($filename, PATHINFO_FILENAME); // excludes final extension
  $lower = strtolower($base);
  foreach ($suffixes as $s) {
    if ($s === '') continue;
    $ls = strlen($s);
    if ($ls <= strlen($lower) && substr($lower, -$ls) === $s) {
      $prefix = substr($base, 0, -$ls);
      $insA->execute([':prefix'=>$prefix]);
      $insB->execute([':filename'=>$filename, ':prefix'=>$prefix, ':suffix'=>$s]);
      break;
    }
  }
}
$pdo->commit();

Notes and troubleshooting: ensure PHP can read the directory and file names (permissions), set the correct charset on PDO, and add a UNIQUE index on A.prefix to avoid duplicate entries. For large directories consider batching/inserting outside a transaction or using LOAD DATA for B. For background reading on the APIs used, see the PDO and DirectoryIterator docs: PDO and DirectoryIterator.

Recommended Answers

All 5 Replies

Does pdf and txt have to be as a string or jumbled up chars?

Member Avatar for Member #120589

What have you got so far? Put up your code so we can have a look and point you in the right direction.

Hello

Ardav, I havent coded anything as i dont know how to code in php

Josh, PDF and txt has to be a string

Regards
Arvind

Well good thats easier for you then.

You need to look into PHP scandir() and PHP explode() then take a look at the mysql_() functions.

This will basicly tell you what you need to know.

Member Avatar for Member #120589

@Arvin

Come on fella, you gotta do some work yourself. This ain't no charity. Help fair enough, but don't expect people to do all the work for you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.