Iklan_1

Education & Financial Konsulting

Education & Financial Konsulting
Education & Financial Konsulting

Agrobisnis & Pariwisata

Agrobisnis & Pariwisata
Agrobisnis & Pariwisata

Digital & Network Development

Digital & Network Development
Digital & Network Development

Cara membuat fungsi upload data di php


<!DOCTYPE html>
<html>
<head>
	<?php include '../config/config_conect.php'; ?>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Upload Data</title>
</head>
	<?php 
if( isset($_POST ["upload"]) ){
	//var_dump($_POST);
	if( upload_data ($_POST) > 0 ) {
        echo "
        <script>
        alert('Data Identitas Web berhasil di Upload !')
        document.location.href = '../rekam_medis/rekam_data.php'
        </script>
        ";
        }else {
          echo "
        <script>
        alert('Data Identitas Web gagal di Upload !')
        //document.location.href = '../rekam_medis/rekam_data.php'
        </script>
          ";
          
        }
}
function upload_data ($data) {
    global $koneksi;
    $id_rm      	= htmlspecialchars($data ["id_rm"]);
    $nama       	= htmlspecialchars($data ["nama"]);
    $id_dataFile  = htmlspecialchars($data ["id_dataFile"]);
    $nama_file		= upload_file();
    
        
  $query = "INSERT INTO data_file  
        VALUES
        ('', '$nama_file', '$id_rm', '$nama') ";


    mysqli_query($koneksi, $query);
    return mysqli_affected_rows($koneksi);
}
function upload_file () {
    $namaFile   = $_FILES['nama_file'] ['name'];
    $error      = $_FILES['nama_file'] ['name'];
    $tmpName    = $_FILES['nama_file'] ['tmp_name'];

    //cek data upload
    $ektensiFilesValid = ['PDF','pdf', 'doc', 'docx',  'jfif', 'JPG', 'jpg',  'PNG', 'png', 'JPEG' ,'jpeg'];


    $ekstensiFile = explode('.',$namaFile);
    $ekstensiFile = strtolower(end($ekstensiFile));

    if( !in_array($ekstensiFile, $ektensiFilesValid) ) {
        echo "<script>
                alert('File yang anda upload bukan tidak sesuai !');
              </script>";
        return false;
    }

    // gambar siap di upload
    // generate nama file
    $namaFileBaru = uniqid();
    $namaFileBaru .= '.';
    $namaFileBaru .= $ekstensiFile;
    move_uploaded_file($tmpName, '../../app/file/' . $namaFileBaru);
    return $namaFileBaru;

}

 ?>
 <?php  
					$id = $_GET['id'];
					$query = mysqli_query($koneksi, "SELECT * FROM rekam_medis WHERE id_rm = '$id' ");
					$Get_akses =mysqli_fetch_array($query);  
				?>
<body>
	<center>
		<h4> FORM UPLOAD DATA</h4>
		<form action="" method="POST" enctype="multipart/form-data">
		<table>
			<tr>
				<td>Nama</td>
				<td>:</td>
				<td>
					<input type="hidden" value=" <?php echo $Get_akses ['id_rm']; ?> " name="id_rm">
					<input type="text"   value=" <?php echo $Get_akses ['nama']; ?> " name="nama">
				</td>
			</tr>
			<tr>
				<td>File</td>
				<td>:</td>
				<td>
					<input type="hidden" name="id_dataFile">
					<input type="file" name="nama_file">
				</td>
			</tr>
			</tr>
			<tr>
				<td></td>
				<td></td>
				<td><button type="submite" name="upload">Simpan</button></td>
			</tr>
		</table>
		</form>
	</center>
</body>
</html>

Menambah url untu view file


   </td>
   <?php  
    $id = $rows_dp['id_siswa'];
    $query = mysqli_query($koneksi, "SELECT * FROM dokumen WHERE id_siswa = '$id' ");
    $Get_akses =mysqli_fetch_array($query);  
    ?>
   <td>
      <a href="/latihan_pgsd/app/file/<?php echo $Get_akses ['nama_file']; ?>">View File</a>  
   </td>

Share Post:

Yogi Iskandar


Yogi Iskandar

Yogi Iskandar

Sponsor By:

SUBSCRIBER


SUBSCRIBER

Iklan_Foot