How to find missing .mp4 files in a directory? - php

I have files likes this on my server:
aaaa.flv
aaaa.mp4
bbbb.flv
bbbb.mp4
cccc.flv
dddd.flv
dddd.mp4
This code isn't working:
$dir=$_SERVER{'DOCUMENT_ROOT'}."/test/";
foreach(glob($dir . "*.flv") as $file)
{
$strip_ext=substr($file, 0, strrpos($file, "."));
$mp4_ext=$strip_ext.".mp4";
if (!file_exists($dir . $mp4_ext)) {
echo "Non-matching pair! ".$strip_ext."<BR>";
}
}
How can I find my .flv files that don't have a matching .mp4?
I'd prefer to put a script in the directory and run it and have it spit out all the filenames without matching .mp4's, this would tell me files that I'll need to convert.

Rather than doing iterated directory checks for if a given .mp4 file exists, I'd rather try (and see if it chokes):
extracting all of the .flv files
extracting all of the .mp4 files
isolate the missing .mp4 files
start converting
Code: (Mock Demo)
chdir($_SERVER{'DOCUMENT_ROOT'}."/test/"); // change working directory so that glob's output excludes the path
$flvs=array_map(function($f){return basename($f,'.flv');},glob("*.flv")); // store .flv's and strip suffixes
$mp4s=array_map(function($f){return basename($f,'.mp4');},glob("*.mp4")); // store .mp4's and strip suffixes
$need_conversion=array_diff($flvs,$mp4s); // run ffmpeg() on these suffix-free files
// $need_conversion = array( 4 => 'cccc' )

Related

Iterate thru possible file extension to get the proper extension

I have a folder with many files in various formats eg .jpg, .png, .pdf, .doc etc... The files are on a remote server. I have a json file with list of filenames and its location but missing the extensions.
I want to rebuild the json file and add the proper extension to filename. How can I do this with php? Can anyone give me any ideas how to iterate thru possible extensions to get the right filename + ext on the server.
eg. I have a url like this - http://www.somesite.com/filename. I know on the server the file is pdf but how can I do this programatically for many files which may be different and rename the url?
Any ideas?
Use a For Loop To loop over all current know file extensions and execute a GET request to the $url . $extension and see if the server returns a file.
If the server returns a file, you can break the for loop.
You can nest 2 for loops in each other to do this far all know urls.
Example
$files = [
"http://test.com/test",
"http://test.com/test2"
];
$extensions = [
".jpg",
".docx"
];
foreach ($files as $file)
{
foreach ($extensions as $extension)
{
$foundFile = // Get requests here
if(FILE_IS_FOUND){
// Store file where ever you need it
break;
}
}
}
This example uses a Foreach Loop

PHP to extract "some-dir/file.ext" from TAR to "another-dir/file.ext"

I use a .tar.gz created monthly by a 3rd party. I can unzip to a .tar; but I am unable to override the tar directory name when I extract files from the tar.
The tar has a single folder "dirname_latestDate" containing a number of files. I was hoping that by specifying individual files for extraction then the tar's "folder" would be ignored (test script cobbled from code on stackoverflow and elsewhere).
$archive = new PharData($theTar);
// error cheecking excluded
foreach($archive as $entry) {
$extractDir = basename($file) . '/';
if($file->isDir()) {
$dir = new PharData($file->getPathname());
foreach($dir as $child) {
$extract_file = $extractDir . basename($child);
$archive->extractTo('/mypath/my-dir', $extract_file, true);
}
}
}
But this still results in the files being placed in a sub-directory named as per tar folder e.g. /mypath/my-dir/unwanted-tar-dirname/file.ext.
I can copy the file from the sub-dir and then delete but this seems ineffficient and unnecessary. Is there any way to override the Tar folder name on un-tar?
The tar contains one file of about 4MB (uncompressed) and a few other miniscule files.
No, you can't change the output file name directly with extractTo.
You could however use file_put_contents in combination with $phar['filepath]->getContent() to extract the file manually.

PharData offsetExists on filename prefixed with ".\"

I have a .tar.gz file downloaded from an external API which we have to implement. It contains images for an object.
I'm not sure how they managed to compress it this way, but the files are basically prefixed with the "current directory". It looks like this in WinRAR:
And like this in 7-Zip, note the .tar first level, and "." second level:
-> ->
When calling
$file = 'archive.tar.gz';
$phar = new PharData($file, FilesystemIterator::CURRENT_AS_FILEINFO);
var_dump($phar->offsetGet('./12613_s_cfe3e73.jpg'));
I get the exception:
Cannot access phar file entry '/12613_s_cfe3e73.jpg' in archive '{...}/archive.tar.gz'
Calling a file which does not exist, e.g.:
var_dump($phar->offsetGet('non-existent.jpg'));
Or calling it without the directory seperator, e.g.:
var_dump($phar->offsetGet('12613_s_cfe3e73.jpg'));
I get a
Entry 12613_s_cfe3e73.jpg does not exist
Exception.
It is not possible to get the archive formatted differently. Does anyone have an idea how to solve this?
Ended up using Archive_Tar. There must be something wrong in the source code of PHP, though I don't think this is the "normal" way of packaging a .tar either.
Unfortunately I'm not very good at C, but it's probably in here (line 1214) or here.
This library seems to handle it just fine, using this example code:
$file = 'archive.tar.gz';
$zip = new Archive_Tar($file);
foreach ($zip->listContent() as $file) {
echo $file['filename'] . '<br>';
}
Result:
./12613_s_f3b483d.jpg
./12613_s_cfe3e73.jpg
./1265717_s_db141dc.jpg
./1265717_s_af5de56.jpg
./1265717_s_b783547.jpg
./1265717_s_35b11f9.jpg
./1265716_s_83ef572.jpg
./1265716_s_9ac2725.jpg
./1265716_s_c5af3e9.jpg
./1265716_s_c070da3.jpg
./1265715_s_4339e8a.jpg
Note the filenames are still prefixed with "./" just like they are in WinRAR.
If you want to stick to using PharData, i suggest a more conservative, two-step approach, where you first decompress the gz and then unarchive all files of the tar to a target folder.
// decompress gz archive to get "/path/to/my.tar" file
$gz = new PharData('/path/to/my.tar.gz');
$gz->decompress();
// unarchive all files from the tar to the target path
$tar = new PharData('/path/to/my.tar');
$tar->extractTo('/target/path');
But it looks like you want to select individual files from the tar.gz archive directly, right?
It should work using fopen() with a StreamReader (compress.zlib or phar) and selecting the individual file. Some examples:
$f = fopen("compress.zlib://http://some.website.org/my.gz/file/in/the/archive", "r");
$f = fopen('phar:///path/to/my.tar.gz//file/in/archive', 'r');
$filecontent = file_get_contents('phar:///some/my.tar.gz/some/file/in/the/archive');
Streaming should also work, when using Iterators:
$rdi = new RecursiveDirectoryIterator('phar:///path/to/my.tar.gz')
$rii = new RecursiveIteratorIterator($rdi, RecursiveIteratorIterator::CHILD_FIRST);
foreach ($rii as $splFileInfo){
echo file_get_contents($splFileInfo->getPathname());
}
The downside is that you have to buffer the stream and save it to file.
Its not a direct file extraction to a target folder.

Check for file with same filename, but different extension

I have a directory contain jpeg and raw image files. Some jpeg files have a raw file version of them, some don't. Luckily, if a jpeg has a raw file they are named the same (excluding the extension). So, I need a way to check this directory for a matching raw file of the same filename, exclusing file extesion. the raw file, file extension could be pretty much anything.
Any ideas how I can do this? I have the filename (excluding extesion) stored of $filename at the moment.
To explain further. I have a directory with the following files in it:
cat.jpg
dog.jpg
bird.jpg
cat.raf
dog.foo
I need to match cat.jpg to cat.rag and dog.jpg to dog.foo. These have just been extracted from a uploaded zip file.
Try searching for files starting with the same name:
$fileWithoutExtension = basename($filename, '.jpg');
$allFilesWithThisName = glob($fileWithoutExtension . '.*');
if (count($allFilesWithThisName)) {
echo 'There is another file with this name';
}
As you already have the filename w/o the extension, you can just check if the raw file exists (file_exists()):
if (file_exists($filename.'.raw')) {
echo 'RAW file exists:', $filename , "\n";
}
But this seems so trivial to me, that I might did not understood your question completely.

How we can read zip file and get information of files or folders contains without unzipping in PHP?

What I actually wanted to do is read zip file and then if it does contain folder then refuse it with some message.
I want user should upload zip file with files only without any directory structure.
So I want to read zip file contains and check file structure.
I am trying with following code snippet.
$zip = zip_open('/path/to/zipfile');
while($zip_entry = zip_read($zip)){
$filename = zip_entry_name($zip_entry);
//#todo check whether file or folder.
}
I have sorted out.
I am now checking filename as strings wherever I am getting string ending with "/" that am treating as directory else as file.
can't you parse path of $filename? something like $dirName = pathinfo($filename, PATHINFO_DIRNAME)

Categories