/    Sign up×
Community /Pin to ProfileBookmark

Using sha1_file() to idenify files

Hi, I’m working on a upload-download system, i was wondering if i can run a query with the sha1 hash of the file the user is trying to upload, and that upon the results, either save the path to the file and the hash in the db, or redirect the user to an existent file.

Thanks to all helpers

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@bokehNov 10.2006 — Yeah of course you can. I use that model for 304 responses, although without the DB. Make the checksum column unique and try to do the insert, if the query fails the file has already been uploaded at some previous time so should not be saved. Also MD5 will be fine and use less space in the DB. [code=php]$checksum = MD5(file_get_contents($_FILES[$fieldname]['tmp_name']));[/code]
Copy linkTweet thisAlerts:
@HellgYauthorNov 10.2006 — Yeah of course you can. I use that model for 304 responses, although without the DB. Make the checksum column unique and try to do the insert, if the query fails the file has already been uploaded at some previous time so should not be saved. Also MD5 will be fine and use less space in the DB. [code=php]$checksum = MD5(file_get_contents($_FILES[$fieldname]['tmp_name']));[/code][/QUOTE]
Yeah, i already set the hash column as unique, what was the function again you

recommended to that guy with the registration form in order to display customized error message?.

I might have more then one unique field in the table.
Copy linkTweet thisAlerts:
@bokehNov 10.2006 — [code=php]if(mysql_query($INSERT_query))
{
# success
}
else
{
$error = mysql_error();
if(preg_match('/keys*(d+)/i', $error, $match))
{
echo 'Duplicate entry on UNIQUE KEY '.$match[1];
}
else
{
# some other problem
}
}[/code]
[I]$match[1][/I] will hold a number. For the PRIMARY KEY that number will be 1. For each additional KEY column that increases by one so the first UNIQUE column after the PRIMARY KEY will be 2 etc.
×

Success!

Help @HellgY 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.13,
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,
)...