Page 1 of 1

Aways Save as a random different name

Posted: Thu Jun 17, 2010 8:13 am
by Dss.Lexius
Hi,
So, here is the code.

Code: Select all

<?php
$image_get[0] = "http://nstoia.com/grs/easyalbum/images/adrian.jpg";
$image_get[1] = "http://nstoia.com/grs/easyalbum/images/beverly.jpg";
$image_get[2] = "http://nstoia.com/grs/easyalbum/images/dog.jpg";
$image_get[3] = "http://nstoia.com/grs/easyalbum/images/group.jpg";
$image_get[4] = "http://nstoia.com/grs/easyalbum/images/kids.jpg";
$image_get[5] = "http://nstoia.com/grs/easyalbum/images/kenny.jpg";
$count = count($image_get);
$i=0;
while($i<$count) {
$image_save =  substr(strrchr($image_get[$i], "/"), 1);
$image = file_get_contents($image_get[$i]);
$f = fopen($image_save,'w+');
fwrite($f,$image);
fclose($f);
$i++;
}
?>
using this code we can download some image files and save them on the server. but i want to edit something in it.
i want images to be saved with different name (Always some random characters)

For example these images should be saved as

A911.jpg
5ctc.jpg
xxx7.gif

This is just a example ^

First i was using a computer program written by me, but now to automate and make my work easy i want to use online server.
on my computer i used the following function to get random chracters:

Code: Select all

Func __GetRandomChars($p_sLen = 4)
    Local $L_sRand = ""
    
    For $l_ia = 0 To ($p_sLen -1)
        Switch( Random(0 , 1 , 1 ) )
            Case 1
                $L_sRand &= Chr(Random(Asc("a"), Asc("f"), 1))
            Case 0
                $L_sRand &= Random(0, 9, 1)
        EndSwitch
    Next
    
    Return $L_sRand
EndFunc
and then downloaded and saved the file using this piece of code:

Code: Select all

InetGet($file01, $destination & "\" & __GetRandomChars() & ".gif", 1, 1)
but for making my work more easy, fast and automated, i want everything to be done on a online server. for this please help me...

Thanx

Re: Aways Save as a random different name

Posted: Thu Jun 17, 2010 11:02 am
by AbraCadaver
It would be too easy to rewrite your code into PHP, so for fun:

Code: Select all

function __GetRandomChars($len = 4) {
	return implode(array_rand(array_combine(array_merge(range(0,9),range('A','Z'),range('a','z')), array_merge(range(0,9),range('A','Z'),range('a','z'))), $len));
}

Re: Aways Save as a random different name

Posted: Thu Jun 17, 2010 2:25 pm
by Dss.Lexius
well, That's great... thanks a lot.
but i am new to PHP
can you tell me how do i use this function into my code?
Thanx

Re: Aways Save as a random different name

Posted: Thu Jun 17, 2010 2:47 pm
by AbraCadaver
Dss.Lexius wrote:well, That's great... thanks a lot.
but i am new to PHP
can you tell me how do i use this function into my code?
Thanx

Code: Select all

$filename = __GetRandomChars() . ".gif";
And for completeness, here is your original function in PHP. Not much different:

Code: Select all

function __GetRandomChars($p_sLen = 4) {
	$L_sRand = "";

	for($l_ia=0; $l_ia<$p_sLen; $l_ia++) {
		switch(mt_rand(0, 1)) {
			case 1:
				$L_sRand .= chr(mt_rand(ord("a"), ord("f")));
				break;
			case 0:
				$L_sRand .= mt_rand(0, 9);
				break;
		}
	}
	return $L_sRand;
}
Other than some small syntax differences and function name differences, the main differences I can see are:

1. switch cases in PHP need a break or else they continue to the next case.
2. A period . is the concatenation operator in PHP, the & operator is used to reference a variable.