Skip to content

Instantly share code, notes, and snippets.

@Aziz-Rahman
Created March 31, 2015 07:37
Show Gist options
  • Save Aziz-Rahman/edefa0c2159b5d22222d to your computer and use it in GitHub Desktop.
Save Aziz-Rahman/edefa0c2159b5d22222d to your computer and use it in GitHub Desktop.
Menggunakan fungsi end(), uniqid, explode, & md 5
<?php
// Connection
$conn = mysqli_connect( "localhost", "root", "", "gallery" ) or die ( 'Error !'. mysqli_error( $conn ) );
$pure_image = $_FILES['image']['name']; // nama ft
$files = end( explode( '.', $pure_image ) ); // memecah nm ft dan extention'nya, diambil kata dr titik paling belakang ( utk mengambil nm extention )
$file_name = $pure_image; // nama ft setelah dipecah/ dipisahkan dg extention'ny
$file_ext = $files; // ext
$hash_name = md5( uniqid( $file_name, true ) ); // mengenkrpsi nm file dan melakukan pangacakan nm file
$img = $hash_name. ".$file_ext"; // menggabungkan nmfile + extention
$path = 'images/'.$img;
$sql = "INSERT INTO images( image ) VALUES( '$img' )";
$save_gallery = mysqli_query( $conn, $sql );
move_uploaded_file( $_FILES['image']['tmp_name'], $path );
if ( $save_gallery ){
echo "<script>alert( 'Data tersimpan !' ); document.location='./';</script>";
}else{
die ( "Gagal tersimpan !". mysqli_error( $conn ) );
}
mysqli_close( $conn );
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment