• Howdy! Welcome to our community of more than 130.000 members devoted to web hosting. This is a great place to get special offers from web hosts and post your own requests or ads. To start posting sign up here. Cheers! /Peo, FreeWebSpace.net
managed wordpress hosting

PHP Help

themoose

Sup, Recoil here.
NLC
OK, at the moment, my script for free image hosting, renames all files uploaded to completely random numbers.

Here is the code it uses for the random numbers:
Code:
{
$number2 = rand(0,100000000) . substr($name, -5, 5);
}
else
{
$number2 = rand(0,100000000) . substr($name, -4, 4);
}

and here is the code for the name that it gathers (something like that)
Code:
$name
So, Basically;

User Selects image 'banner.jpg' ($name)
User Presses Upload, and it renames it to something like 287193.jpg

how do I change it so:
User Presses Upload, and it renames it to something like bann282.jpg (or even banner928.jpg)?

I hope I made myself clear :)
 
Something like this:

Code:
<?php

$name = "ban";
$number2 = substr($name, -3, 3) .rand(0,1000); 


echo $number2;



?>

For me this outputted ban480
 
Last edited:
$name is what you want the beginning part to be, you can just get the filename and make that $name instead.
$number2 = substr($name, -3, 3) .rand(0,1000); takes 3 characters off $name and adds a random number from 0-1000.
 
ok, thanks, i will try that

edit//

ok i replaced the code i put in the first post with:
PHP:
{
$number2 = substr($name, -3, 3) .rand(0,1000);
}
else
{
$number2 = substr($name, -3, 3) .rand(0,1000);
}

and it renamed it to: png473 , which of course is not right, it has no extention.
Help, plz?

edit again//
i realise now that it took off the first 3 characters, the image was called bg.png and it took off bg. . this is not good:- is there another way to do this that works on all image names?
 
Last edited:
This is a working example. bannnnwwe.jpg is here the full filename with the extension, and will print out ban and 3 random numbers, and .jpg in this case. I have tested it, and it works.



PHP:
$name = "bannnnwwe.jpg";
$number2 = substr($name, 0, 4) .rand(0,1000) .substr($name,-4,4); 


echo $number2;

When I ran this example, it printed out
bann219.jpg
 
Last edited:
yup, that works, thanks
I also tested it with an image with a short filename, bg.png, and it came out bg.p456.png, which works.
Thanks :)
 
Back
Top