⚠ In case you've missed it, we have migrated to our new website, with a brand new forum. For more details about the migration you can read our blog post for website migration. This is an archived forum. ⚠

  •     

profile picture

Uploading Files



Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 15:00 PM

Hi,

Is possible encrypt using SHA1 a upload file. Help me with this problem, I need create a form with upload file, but upload file must be encrypted in SHA1.

Thanks.

victor
  • profile picture
  • Member

Posted 29 October 2012 - 18:45 PM

HI, Javier Cintrón!
I think that you should change this code:
grocery_crud.php str. 4529

private function trim_file_name($name, $type) {
// Remove path information and dots around the filename, to prevent uploading
// into different directories or replacing hidden system files.
// Also remove control characters and spaces (\x00..\x20) around the filename:
$file_name = trim(basename(stripslashes($name)), ".\x00..\x20");
// Add missing file extension for known image types:
if (strpos($file_name, '.') === false &&
preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
$file_name .= '.'.$matches[1];
}

//Ensure that we don't have disallowed characters and add a unique id just to ensure that the file name will be unique
$file_name = substr(uniqid(),-5).'-'.preg_replace("/([^a-zA-Z0-9\.\-\_]+?){1}/i", '-', $file_name);

return $file_name;
}




$file_name = substr(uniqid(),-5).'-'.preg_replace("/([^a-zA-Z0-9\.\-\_]+?){1}/i", '-', $file_name);

victor
  • profile picture
  • Member

Posted 29 October 2012 - 18:51 PM

for example :

$file_name = sha1(date()).'.'.substr(strrchr($file_name,'.'), 1);

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 19:04 PM

Hi Victor,

Thanks for your answer, try what you sent me does not work, that is not what I'm doing wrong. Please help me.

Thanks.

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 19:10 PM

[quote name='victor' timestamp='1351536716' post='4028']
for example :

$file_name = sha1(date()).'.'.substr(strrchr($file_name,'.'), 1);

[/quote]

[color=#282828][font=helvetica, arial, sans-serif]Hi Victor,[/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]Thanks for your answer, try what you sent me does not work, that is not what I'm doing wrong. Please help me.[/font][/color]

[color=#282828][font=helvetica, arial, sans-serif]Thanks.[/font][/color]

victor
  • profile picture
  • Member

Posted 29 October 2012 - 19:11 PM


private function trim_file_name($name, $type) {
// Remove path information and dots around the filename, to prevent uploading
// into different directories or replacing hidden system files.
// Also remove control characters and spaces (\x00..\x20) around the filename:
$file_name = trim(basename(stripslashes($name)), ".\x00..\x20");
// Add missing file extension for known image types:
if (strpos($file_name, '.') === false &&
preg_match('/^image\/(gif|jpe?g|png)/', $type, $matches)) {
$file_name .= '.'.$matches[1];
}
//Ensure that we don't have disallowed characters and add a unique id just to ensure that the file name will be unique
// $file_name = '123456'.substr(uniqid(),-5).'-'.preg_replace("/([^a-zA-Z0-9\.\-\_]+?){1}/i", '-', $file_name);
$file_name = sha1(date()).'.'.substr(strrchr($file_name,'.'), 1);
return $file_name;
}


result : da39a3ee5e6b4b0d3255bfef95601890afd80709.jpg

victor
  • profile picture
  • Member

Posted 29 October 2012 - 19:14 PM

or you need another result?

victor
  • profile picture
  • Member

Posted 29 October 2012 - 19:18 PM

The function trim_file_name() returning file's name. You can change the function as you like. bu

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 19:19 PM

When add command line, display the following error "An error has occuerred on uploading".

victor
  • profile picture
  • Member

Posted 29 October 2012 - 19:23 PM

deleted

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 19:31 PM

I send the file now. Thanks.

victor
  • profile picture
  • Member

Posted 29 October 2012 - 20:22 PM

update your library. On my localhost it works perfectly

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 21:55 PM

Hi Victor,

I try in Ubuntu, and working perfect. Thanks. A last question, when he says he needs to encrypt a file, this is the right way.

Thanks.

victor
  • profile picture
  • Member

Posted 29 October 2012 - 22:05 PM

A last question, when he says he needs to encrypt a file, this is the right way.

sorry, i don't understand this question.

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 22:07 PM

This is the correct way to encrypt a file.

victor
  • profile picture
  • Member

Posted 29 October 2012 - 22:24 PM

This is the correct way to encrypt a [b]file's name.[/b]

it's better:

$file_name = sha1(microtime().$_SERVER['REMOTE_ADDR']).'.'.substr(strrchr($file_name,'.'), 1);

if you want to encrypt the contents of the file you need to do different way.
sorry, I'm bad in English.

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 22:57 PM

I need both. If posible or your have rutine. Thanks.

victor
  • profile picture
  • Member

Posted 29 October 2012 - 23:02 PM

for what it is necessary? do you need an Encrypting / Decrypting file?

victor
  • profile picture
  • Member

Posted 29 October 2012 - 23:03 PM

or only encrypting?

Javier Cintrón
  • profile picture
  • Member

Posted 29 October 2012 - 23:04 PM

The files that users upload is confidential information.