Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
File manipulation and automatic numbered file names
Old 09-02-2007, 05:00 PM File manipulation and automatic numbered file names
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
Ok I am wondering how I can make automatic file names I don't think I am doing it correctly

If one file exist called 2.jpg then next file will be called 3.jpg then 4.jpg then 5.jpg and so on and so forth

How is this done I think it is

%d.jpg or something like this if I have a file that outputs jpgs how can I use this type of naming.

So HOW IS THIS DONE IN PHP?
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
 
Register now for full access!
Old 09-02-2007, 05:10 PM Re: File manipulation and automatic numbered file names
NullPointer's Avatar
Will Code for Food

Posts: 2,815
Name: Matt
Location: Irvine, CA
Trades: 0
If you are trying to access a list of numbered files systematically something like this will work:

PHP Code:
<?php

$files 
10 //the number of files you are accessing, ten in this case.
for($i 0$i $files$i++)
{
     echo 
'<img src="images/' $i '.jpg">';
}
?>
not sure if that helps.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
NullPointer is online now
Reply With Quote
View Public Profile Visit NullPointer's homepage!
 
Old 09-02-2007, 08:35 PM Re: File manipulation and automatic numbered file names
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Using PHP5, scandir will return the file content in an array. You could then iterate through this array to find this highest number .jpg, then postincrement it, which gives you the final file name.

I wrote up a function that should work as above since I'll prolly wind up using it myself. Gimme a yell if something doesn't work as it's not tested:

PHP Code:

//Returns an autogenerated filename
//@$dir string path to directory for scanning
//@$format string file format minus full-stop
//Returns false on failure or new file name to use
function fctnFileAutoName($dir$format 'jpg') {

    
//return false if no dir path is passed
    
if(empty($dir)) return false;
    
    
//php5 only - return false now if php < 5
    
if(!function_exists(scandir)) return false;
    
    
$arr_fileNames = array();
    
    
//scandir to return array of file names
    
if ($arr_contents scandir($dir)) {
        
        
//drop extension from filesnames and convert to integers
        
while ( false !== ( $file readdir($arr_contents)) ) {
            
            
$fileName explode('.'$file2);
            
            if(
$fileName[1] == $format) {
            
                
$fileName = ( intval($fileName[0]) ) ? intval($fileName[0]) : NULL;
                
                
//add to new array
                
$arr_fileNames[] =     $fileName;
            }
        
        }
//end loop
        
        //if no files that fit the integer naming scheme - return false
        
if (empty($arr_fileNames)) return false;
        
    } else {
    
        return 
false;
        
    }
    
    
    
//iterate through and find the highest integer
    
$int_lastFile 0;
    
    foreach( 
$arr_fileNames as $value) {
    
        
//overwrite $int_lastFile if $value is larger
        
$int_lastFile = ( $value $int_lastFile ) ? $value $int_lastFile;
    
    }
    
    
//post-increment $int_lastFile
    
$int_lastFile++;
    
    
//string together the autofilename
    
$str_autoFilename $int_lastFile '.' $format;
    
    
//return the new filename
    
return $str_autoFilename;

}
//EOF 
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-03-2007, 06:30 PM Re: File manipulation and automatic numbered file names
dansgalaxy's Avatar
Defies a Status

Posts: 6,521
Name: Dan
Location: Swindon
Trades: 0
This look slike it could be useful. in the future...
__________________
Discounted Web Hosting With XDnet!
>> Get 25% of hosting~ Promo: Webmaster-talk <<

Please login or register to view this content. Registration is FREE
dansgalaxy is offline
Reply With Quote
View Public Profile Visit dansgalaxy's homepage!
 
Old 09-03-2007, 11:44 PM Re: File manipulation and automatic numbered file names
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
PHP Code:
<?php 
$input 
$_GET['input'];
$output '%d'//hopefully that will make the file name a number
exec 'ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -ar 22050 -ab 32 -f flv -s 320x240 /home/forbushj/public_html/2vidd/'.$output.'.flv';
exec 'ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -an -ss 00:00:45 -an -r 1 -vframes 1 -y -s 150x100 /home/forbushj/public_html/2vidd/'.$output.'.jpg';
?>
this is the code I am using this code for more than one thing I am doing
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 09-04-2007, 12:44 AM Re: File manipulation and automatic numbered file names
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
What are you using to arrive on this chunk of processing code? A link? An upload form?
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-04-2007, 01:16 AM Re: File manipulation and automatic numbered file names
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
After seeing your post over at codingforums, I edited the above function to conform with the convention you mentioned over there. It will return a string 7 chars long, padded with 0's to the left

To make the function check for the next .jpg: fctnFileAutoName('/home/forbushj/public_html/2vidd/');

To make the function return the next .flv: fctnFileAutoName('/home/forbushj/public_html/2vidd/','flv');

PHP Code:
//Returns an autogenerated filename
//@$dir string path to directory for scanning
//@format string file format string minus full-stop
//Returns false on failure
function fileAutoname($dir$format 'jpg') {

    
//return false if no dir path is passed
    
if(empty($dir)) return false;
    
    
//php5 only - return false now if php < 5
    
if(!function_exists(scandir)) return false;
    
    
$arr_fileNames = array();
    
    
//scandir to return array of file names
    
if ($arr_contents scandir($dir)) {
        
        
//drop extension from filesnames and convert to integers
        
while ( false !== ( $file readdir($arr_contents)) ) {
            
            
$fileName explode('.'$file2);
            
            if(
$fileName[1] == $format) {
            
                
$fileName = ( intval($fileName[0]) ) ? intval($fileName[0]) : NULL;
                
                
//add to new array
                
$arr_fileNames[] =     $fileName;
            }
        
        }
//end loop
        
        //if no files that fit the integer naming scheme - return false
        
if (empty($arr_fileNames)) return false;
        
    } else {
    
        return 
false;
        
    }
    
    
    
//iterate through and find the highest integer
    
$int_lastFile 0;
    
    foreach( 
$arr_fileNames as $value) {
    
        
//overwrite $int_lastFile if $value is larger
        
$int_lastFile = ( $value $int_lastFile ) ? $value $int_lastFile;
    
    }
    
    
//post-increment $int_lastFile
    
$int_lastFile++;
    
    
//string together the autofilename
    
$str_autoFilename str_pad($int_lastFile7'0'STR_PAD_LEFT);
    
    
//return the new filename
    
return $str_autoFilename;

}
//EOF 

Your usage would become:

PHP Code:
$input = ( !empty( $_GET['input'] ) && is_numeric$_GET['input'] ) ) ? $_GET['input'] : false;
if( 
$input ) {

    
$outputJPG fctnFileAutoName('/home/forbushj/public_html/2vidd/');
    
$outputFLV fctnFileAutoName('/home/forbushj/public_html/2vidd/','flv');
    
    if( 
$outputJPG && $outputFLV ) {
    
        
exec('ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -ar 22050 -ab 32 -f flv -s 320x240 /home/forbushj/public_html/2vidd/'.$outputFLV.'.flv');
        
exec('ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -an -ss 00:00:45 -an -r 1 -vframes 1 -y -s 150x100 /home/forbushj/public_html/2vidd/'.$outputJPG.'.jpg');

    } else {

    die(
'Error: Invalid input.');

    }
    
} else {

    die(
'Expecting a numeric value for GET[input].');


__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-04-2007, 09:42 PM Re: File manipulation and automatic numbered file names
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
Will this code work and I have to give the input as any type of media the output is what is different. either jpg or flv based on file name
Quote:
Originally Posted by metho View Post
After seeing your post over at codingforums, I edited the above function to conform with the convention you mentioned over there. It will return a string 7 chars long, padded with 0's to the left

To make the function check for the next .jpg: fctnFileAutoName('/home/forbushj/public_html/2vidd/');

To make the function return the next .flv: fctnFileAutoName('/home/forbushj/public_html/2vidd/','flv');

PHP Code:
//Returns an autogenerated filename
//@$dir string path to directory for scanning
//@format string file format string minus full-stop
//Returns false on failure
function fileAutoname($dir$format 'jpg') {

    
//return false if no dir path is passed
    
if(empty($dir)) return false;
    
    
//php5 only - return false now if php < 5
    
if(!function_exists(scandir)) return false;
    
    
$arr_fileNames = array();
    
    
//scandir to return array of file names
    
if ($arr_contents scandir($dir)) {
        
        
//drop extension from filesnames and convert to integers
        
while ( false !== ( $file readdir($arr_contents)) ) {
            
            
$fileName explode('.'$file2);
            
            if(
$fileName[1] == $format) {
            
                
$fileName = ( intval($fileName[0]) ) ? intval($fileName[0]) : NULL;
                
                
//add to new array
                
$arr_fileNames[] =     $fileName;
            }
        
        }
//end loop
        
        //if no files that fit the integer naming scheme - return false
        
if (empty($arr_fileNames)) return false;
        
    } else {
    
        return 
false;
        
    }
    
    
    
//iterate through and find the highest integer
    
$int_lastFile 0;
    
    foreach( 
$arr_fileNames as $value) {
    
        
//overwrite $int_lastFile if $value is larger
        
$int_lastFile = ( $value $int_lastFile ) ? $value $int_lastFile;
    
    }
    
    
//post-increment $int_lastFile
    
$int_lastFile++;
    
    
//string together the autofilename
    
$str_autoFilename str_pad($int_lastFile7'0'STR_PAD_LEFT);
    
    
//return the new filename
    
return $str_autoFilename;

}
//EOF 
Your usage would become:

PHP Code:
$input = ( !empty( $_GET['input'] ) && is_numeric$_GET['input'] ) ) ? $_GET['input'] : false;
if( 
$input ) {

    
$outputJPG fctnFileAutoName('/home/forbushj/public_html/2vidd/');
    
$outputFLV fctnFileAutoName('/home/forbushj/public_html/2vidd/','flv');
    
    if( 
$outputJPG && $outputFLV ) {
    
        
exec('ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -ar 22050 -ab 32 -f flv -s 320x240 /home/forbushj/public_html/2vidd/'.$outputFLV.'.flv');
        
exec('ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -an -ss 00:00:45 -an -r 1 -vframes 1 -y -s 150x100 /home/forbushj/public_html/2vidd/'.$outputJPG.'.jpg');

    } else {

    die(
'Error: Invalid input.');

    }
    
} else {

    die(
'Expecting a numeric value for GET[input].');


__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 09-04-2007, 10:38 PM Re: File manipulation and automatic numbered file names
metho's Avatar
Ultra Talker

Posts: 481
Location: Gold Coast - Brisbane QLD, Australia
Trades: 0
Dunno m8, as mentioned it's untested. The function uses arguments for the directory path and format only. If successful, it will return the file name to use; that's it's only purpose. You just add the extension to the returned name;

Because you haven't posted anything in regards as to how this script is supposed to work (form, link etc) the function is generic. How you decide to use it is up to you.
__________________
I do
Please login or register to view this content. Registration is FREE
based.
Spend a lot of time in
Please login or register to view this content. Registration is FREE
.
And
Please login or register to view this content. Registration is FREE
chews up the rest.

Last edited by metho; 09-04-2007 at 10:43 PM..
metho is offline
Reply With Quote
View Public Profile Visit metho's homepage!
 
Old 09-04-2007, 10:41 PM Re: File manipulation and automatic numbered file names
Ultra Talker

Posts: 483
Trades: 0
Depending on how many files you are intending to have, I would suggest a different direction... not that the scandir won't work, but it will get slow inching your way through an array when you've got a few thousand files there.

If it were me, I'd basically do a binary search type approach.

So for example, imagine you currently have 1.jpg through to 45.jpg. Instead of doing scandir and iterating through the items, simply hit the file_exists function, testing an ever doubling number until the file doesn't exist, then continue to break down your search by half until you find the next file...

The steps would be:
- Does 1.jpg exist? Yes.
- Does 2.jpg exist? Yes.
- Does 4.jpg exist? Yes.
- Does 8.jpg exist? Yes.
- Does 16.jpg exist? Yes.
- Does 32.jpg exist? Yes.
- Does 64.jpg exist? No... now search from the middle point of 32 and 64, so...
- Does 48.jpg exist? No. Mid-point of 32 and 48...
- Does 40.jpg exist? Yes. Mid-point of 40 and 48...
- Does 44.jpg exist? Yes. Mid-point of 44 and 48...
- Does 46.jpg exist? No. Mid-point of 44 and 46...
- Does 45.jpg exist? Yes.

46.jpg doesn't exist but 45.jpg does exist... therefore your next image number is 46.jpg.

This may seem convoluted but as you can see, there were only 12 attempts at getting a number. If my math is correct, even just 30 attempts will let you find the next number up to over a billion...

Hopefully this process makes sense!
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-04-2007, 11:09 PM Re: File manipulation and automatic numbered file names
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
By the way the commands demonstrated in exec function uses ffmpeg
PHP Code:
<?php //first one gets input from a form which runs with ffmpeg then it does sampling rate at //22050HZ audio bitrate is 32 forced to flv size is 320x240 and then the output
exec 'ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -ar 22050 -ab 32 -f flv -s 320x240 /home/forbushj/public_html/2vidd/'.$output.'.flv';
//next gets input from form and takes a thumbnail frame at 45 seconds and saves it as a jpg
exec 'ffmpeg -i /home/forbushj/public_html/uploadvidd/'.$input.' -an -ss 00:00:45 -an -r 1 -vframes 1 -y -s 150x100 /home/forbushj/public_html/2vidd/'.$output.'.jpg'?>


Cool can you demonstrate this with 0000001 - 9999999 how this would be done.

Quote:
Originally Posted by TwistMyArm View Post
Depending on how many files you are intending to have, I would suggest a different direction... not that the scandir won't work, but it will get slow inching your way through an array when you've got a few thousand files there.

If it were me, I'd basically do a binary search type approach.

So for example, imagine you currently have 1.jpg through to 45.jpg. Instead of doing scandir and iterating through the items, simply hit the file_exists function, testing an ever doubling number until the file doesn't exist, then continue to break down your search by half until you find the next file...

The steps would be:
- Does 1.jpg exist? Yes.
- Does 2.jpg exist? Yes.
- Does 4.jpg exist? Yes.
- Does 8.jpg exist? Yes.
- Does 16.jpg exist? Yes.
- Does 32.jpg exist? Yes.
- Does 64.jpg exist? No... now search from the middle point of 32 and 64, so...
- Does 48.jpg exist? No. Mid-point of 32 and 48...
- Does 40.jpg exist? Yes. Mid-point of 40 and 48...
- Does 44.jpg exist? Yes. Mid-point of 44 and 48...
- Does 46.jpg exist? No. Mid-point of 44 and 46...
- Does 45.jpg exist? Yes.

46.jpg doesn't exist but 45.jpg does exist... therefore your next image number is 46.jpg.

This may seem convoluted but as you can see, there were only 12 attempts at getting a number. If my math is correct, even just 30 attempts will let you find the next number up to over a billion...

Hopefully this process makes sense!
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE


Last edited by goheadtry; 09-04-2007 at 11:17 PM..
goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 09-05-2007, 01:15 AM Re: File manipulation and automatic numbered file names
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
If you have a better way of doing this let me know
this is how I am probably going to do it unless someone knows of a better way

__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Old 09-05-2007, 08:02 AM Re: File manipulation and automatic numbered file names
Ultra Talker

Posts: 483
Trades: 0
To be perfectly honest, the code that I would be writing would be untested and would still need to be tweaked for your situation.

Instead, I'd suggest doing a search for 'binary search' (Wikipedia or something like that probably has an implementation) and then tweak *that* code, knowing that it's right to begin with.

When you're looking at it, you'll find that binary search code is normally looking for a value: in your instance that 'value' would be "file-(n-1).jpg exists but file-(n).jpg does not exist".

It should be pretty simple.
__________________

Please login or register to view this content. Registration is FREE
TwistMyArm is offline
Reply With Quote
View Public Profile
 
Old 09-08-2007, 10:18 PM Re: File manipulation and automatic numbered file names
goheadtry's Avatar
Webmaster Talker

Posts: 730
Name: John
Location: United States of America, California
Trades: 0
I found a good alternative method
http://www.howtoforge.com/video_stre...tpd_flowplayer

Not exactly for what I need but it has source code that I can use.
__________________
Free $1 gift card when you signup at
Please login or register to view this content. Registration is FREE

Please login or register to view this content. Registration is FREE

goheadtry is offline
Reply With Quote
View Public Profile Visit goheadtry's homepage!
 
Reply     « Reply to File manipulation and automatic numbered file names
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.51386 seconds with 12 queries