/    Sign up×
Community /Pin to ProfileBookmark

Renaming a file upon upload

Hi

I have the following code

$uploaddir = ‘../images/deletables/’; // Relative path under webroot
$uploaddir2 = ‘/images/deletables/’; // Relative path under webroot

if(!empty($_FILES[‘userfile’][‘name’])){

$uploadfile = $uploaddir.basename.($_FILES[‘userfile’][‘name’]);
$loc = $uploaddir2.basename.($_
FILES[‘userfile’][‘name’]);

if (move_uploaded_file($_FILES[‘userfile’][‘tmp_name’], $uploadfile)) {

$sql = mysql_query(“INSERT INTO hb_photo(photo_location) VALUES (‘”.my_safe($loc).”‘)”);

$id = mysql_insert_id();

$sql = mysql_query(“INSERT INTO hb_album_photo(album_id, photo_id) VALUES (‘”.my_safe($album).”‘, ‘”.my_safe($id).”‘)”);
}

}

How would i give the file name a number. For example 21.jpg, as opposed to the actual filename when uploaded.

Thanks

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@birdy247authorSep 28.2008 — anyone?
Copy linkTweet thisAlerts:
@themoonSep 29.2008 — you will need to create 2 functions.

one to extract the file extension, and the other to generate random number for the file to be renamed.

once you are done, instead of using the same file name:
[code=php]$_FILES['userfile']['name'][/code]

try renaming it this way:
[code=php]<?php
$s_fileName = $_FILES['userfile']['name'];
$s_ext = getExt($s_fileName); //example: txt or jpg or gif, etc..
$s_append_ext = ".$s_ext";
$s_renameFile = RandNumberFunction(); //example: 225654
$s_renameFile = $s_renameFile.$s_append_ext;

$uploadfile = $uploaddir.basename.($s_renameFile);
?>[/code]
×

Success!

Help @birdy247 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...