1
xsell
Create Random Name to uploaded file
  • 2009/12/23 0:49

  • xsell

  • Quite a regular

  • Posts: 245

  • Since: 2008/9/2 1


Hello

I use this Code to upload files in my site , I need way to Make the code Create Random Number for the Uploaded File . useing rand () or any other functions.

include_once '../../class/uploader.php';
$allowed_mimetypes = array('image/gif''image/jpeg''image/pjpeg''image/x-png''image/png');
$maxfilesize $xoopsModuleConfig['upload_max_size'];

$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH "/" "modules/test/upload/"$allowed_mimetypes$maxfilesize);
if (
$uploader->fetchMedia($_POST["xoops_upload_file"][0])) {
    if (!
$uploader->upload()) {

        
redirect_header(XOOPS_URL "/modules/test/index.php" 6""._MI_ATTACHMENTS1."");
    } else {
        echo 
""._MI_SENDME_ATTACHMENTS4."";
    }
} else {

    
redirect_header(XOOPS_URL "/modules/test/index.php" 5""._MI_ATTACHMENTS2."");
}

2
ghia
Re: Create Random Name to uploaded file
  • 2009/12/23 2:12

  • ghia

  • Community Support Member

  • Posts: 4953

  • Since: 2008/7/3 1


See this.

3
wishcraft
Re: Create Random Name to uploaded file

Best method is to use a combination of time, md5 and substr.. the following code will generate a prefix of 7 chars..

$prefix substr(md5(time()),0,7);

4
xsell
Re: Create Random Name to uploaded file
  • 2009/12/23 4:00

  • xsell

  • Quite a regular

  • Posts: 245

  • Since: 2008/9/2 1


Thank you.

@ghia .. thx for the link but that did not help me , i already know how to use the uploader class , I just Can not figuer Out how to rename the file to any random name.


@wishcraft .. Thx .. but i do not know how to apply that in my code .

I tried like this.

$prefix substr(md5(time()),0,7);
$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH "/" "modules/test/upload/"$allowed_mimetypes$maxfilesize);
if (
$uploader->fetchMedia($_POST["xoops_upload_file"][0], $prefix)) {
    if (!
$uploader->upload()) {


Uploads the file but dose not rename it :)

5
Dylian
Re: Create Random Name to uploaded file
  • 2009/12/23 6:01

  • Dylian

  • Friend of XOOPS

  • Posts: 237

  • Since: 2007/7/21


Try:
$prefix substr(md5(time()),0,7); 
$uploader = new XoopsMediaUploader(XOOPS_ROOT_PATH "/" "modules/test/upload/"$allowed_mimetypes$maxfilesize); 
if (
$uploader->fetchMedia($_POST["xoops_upload_file"][0])) { 
    
$uploader->setTargetFileName($prefix.$uploader->mediaName); // ADDED | UPDATED
    
if (!$uploader->upload()) {


Didn't try it but i think it should work.

For more information click [HERE] and [HERE].

Greets Dylian.

6
xsell
Re: Create Random Name to uploaded file
  • 2009/12/23 6:57

  • xsell

  • Quite a regular

  • Posts: 245

  • Since: 2008/9/2 1


Thank you Dylian ,

But that DID not work.

Fatal errorCall to undefined function setTargetFileName()


i have included uploader.php in the code , but for some reason it dose not Recall the settargetname Function.

7
Dylian
Re: Create Random Name to uploaded file
  • 2009/12/23 7:13

  • Dylian

  • Friend of XOOPS

  • Posts: 237

  • Since: 2007/7/21


Whoops forgot something... I've updated the previous post with the correct code.

Greets Dylian.

8
xsell
Re: Create Random Name to uploaded file
  • 2009/12/23 7:22

  • xsell

  • Quite a regular

  • Posts: 245

  • Since: 2008/9/2 1


No worry Dylian , I managed to find out the Soluation . its just little Correction of ur code .

$uploader->setTargetFileName($prefix);


That done it and Now i have the files renamed but with one Problem it Dose Not add the file Extenstion to the file name .. it only save's it as 5455. or jdkj. without jpg format or the correct format. any idea?

9
Dylian
Re: Create Random Name to uploaded file
  • 2009/12/23 7:27

  • Dylian

  • Friend of XOOPS

  • Posts: 237

  • Since: 2007/7/21


See my example... (It adds the original filename and the extension)

[edit] This is the updated line in my original post:
$uploader->setTargetFileName($prefix.$uploader->mediaName);

[/edit]
Greets Dylian.

10
xsell
Re: Create Random Name to uploaded file
  • 2009/12/23 7:30

  • xsell

  • Quite a regular

  • Posts: 245

  • Since: 2008/9/2 1


That Worked Very Good .. Thank you Man. :)

Login

Who's Online

375 user(s) are online (276 user(s) are browsing Support Forums)


Members: 0


Guests: 375


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits