<?php
ini_set('error_reporting', E_ALL);
date_default_timezone_set('Europe/Istanbul');
$dbhost = 'localhost';
$dbname = 'dbname';
$dbuser = 'dbuser';
$dbpass = 'dbpass]';
define("BASE_URL", "http://localhost/");
try {
$pdo = new PDO("mysql:host={$dbhost};dbname={$dbname}", $dbuser, $dbpass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch( PDOException $ex ) {
echo "Connection error :" . $ex->getMessage();
}
if(isset($_POST['form1'])) {
$valid = 1;
if(empty($_POST['filetoupload'])) {
$valid = 0;
$error_message .= "filetoupload alanı boş<br>";
}
$path = $_FILES['resim']['name'];
$path_tmp = $_FILES['resim']['tmp_name'];
if($path == '') {
$valid = 0;
$error_message .= "Resim seçin<br>";
} else {
$ext = pathinfo( $path, PATHINFO_EXTENSION );
$file_name = basename( $path, '.' . $ext );
if( $ext!='jpg' && $ext!='png' && $ext!='jpeg' && $ext!='gif' ) {
$valid = 0;
$error_message .= 'Tur jpg, jpeg, gif ve png Uzantısı Olmalı<br>';
}
}
if($valid == 1) {
$statement = $pdo->prepare("SHOW TABLE STATUS LIKE 'tbl_resim'");
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row) {
$ai_id=$row[10];
}
$final_name = 'resim-'.$ai_id.'.'.$ext;
move_uploaded_file( $path_tmp, '../uploads/'.$final_name );
$statement = $pdo->prepare("INSERT INTO tbl_resim (filetoupload,name) VALUES (?,?)");
$statement->execute(array($_POST['filetoupload'],$final_name));
$success_message = 'Resim yüklendi.';
}
}
?>
<section class="content">
<div class="row">
<div class="col-md-12">
<?php if($error_message): ?>
<div class="callout callout-danger">
<p>
<?php echo $error_message; ?>
</p>
</div>
<?php endif; ?>
<?php if($success_message): ?>
<div class="callout callout-success">
<p><?php echo $success_message; ?></p>
</div>
<?php endif; ?>
<form class="form-horizontal" action="" method="post" enctype="multipart/form-data">
<div class="box box-info">
<div class="box-body">
<div class="form-group">
<label for="" class="col-sm-2 control-label">filetoupload<span>*</span></label>
<div class="col-sm-4">
<input type="text" class="form-control" name="filetoupload">
</div>
</div>
<div class="form-group">
<label for="" class="col-sm-2 control-label">Upload Resim <span>*</span></label>
<div class="col-sm-4" style="padding-top:6px;">
<input type="file" name="resim">
</div>
</div>
<div class="form-group">
<label for="" class="col-sm-2 control-label"></label>
<div class="col-sm-6">
<button type="submit" class="btn btn-success pull-left" name="form1">Submit</button>
</div>
</div>
</div>
</div>
</form>
</div>
</div>
</section>bu tek sayfa hali
bu kısmı senin attığın gibi sadece upload.php yapacaksan direk yapıştır form sayfası ayrı ise formun gönder kısmında buton name="form1" yap
if(isset($_POST['form1'])) {
$valid = 1;
if(empty($_POST['filetoupload'])) {
$valid = 0;
$error_message .= "filetoupload alanı boş<br>";
}
$path = $_FILES['resim']['name'];
$path_tmp = $_FILES['resim']['tmp_name'];
if($path == '') {
$valid = 0;
$error_message .= "Resim seçin<br>";
} else {
$ext = pathinfo( $path, PATHINFO_EXTENSION );
$file_name = basename( $path, '.' . $ext );
if( $ext!='jpg' && $ext!='png' && $ext!='jpeg' && $ext!='gif' ) {
$valid = 0;
$error_message .= 'Tur jpg, jpeg, gif ve png Uzantısı Olmalı<br>';
}
}
if($valid == 1) {
$statement = $pdo->prepare("SHOW TABLE STATUS LIKE 'tbl_resim'");
$statement->execute();
$result = $statement->fetchAll();
foreach($result as $row) {
$ai_id=$row[10];
}
$final_name = 'resim-'.$ai_id.'.'.$ext;
move_uploaded_file( $path_tmp, '../uploads/'.$final_name );
$statement = $pdo->prepare("INSERT INTO tbl_resim (filetoupload,name) VALUES (?,?)");
$statement->execute(array($_POST['filetoupload'],$final_name));
$success_message = 'Resim yüklendi.';
}
}
?>