2017-01-12 1 views
-2

데이터베이스에 업로드 할 수없는 것 같습니다. 여기MySql : 데이터베이스에 파일을 저장하는 방법?

이이 데이터베이스

<?php 
include_once 'connectiontwo.php'; 
if(isset($_POST['btn-upload'])) 
{  

$file = rand(1000,100000)."-".$_FILES['file']['name']; 
$file_loc = $_FILES['file']['tmp_name']; 
$file_size = $_FILES['file']['size']; 
$file_type = $_FILES['file']['type']; 
$folder="uploads/"; 

// new file size in KB 
$new_size = $file_size/1024; 
// new file size in KB 

// make file name in lower case 
$new_file_name = strtolower($file); 
// make file name in lower case 

$final_file=str_replace(' ','-',$new_file_name); 

if(move_uploaded_file($file_loc,$folder.$final_file)) 
{ 
$sql="INSERT INTO module_let(module_desc,type,size)  VALUES('$final_file','$file_type','$new_size')"; 
$conn->query($sql); 
?> 
<script> 
    alert('successfully uploaded'); 
    window.location.href='indexsample.php?success'; 
    </script> 
<?php 
} 
else 
{ 
?> 
<script> 
alert('error while uploading file'); 
    window.location.href='indexsample.php?fail'; 
    </script> 
<?php 
} 
} 
?> 
에 업로드 할 수 있도록이 코드가

<?php 
$servername = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "uphslletreviewer"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
die("Connection failed: " . $conn->connect_error); 
} 

?> 

데이터베이스에 연결할 수있는 파일 connectiontwo.php 내 코드입니다

귀하의 도움을 많이 주시면 감사하겠습니다 ... Im php to new. 고맙습니다!

+0

무엇을 작동하지 않습니다

if($conn->query($sql) === TRUE) { // successful } else { // Errorhandling // for finding the error a simple echo $conn->errno.": ".$conn->error } 

그리고 사미는 이미 언급 한 바와 같이

, 당신은 정말 문 준비를 사용해야합니까? 오류? 어떻게해야합니까? 자세한 정보를 제공하십시오. 또한 SQL 문자열을 comcaten하지 않고 항상 매개 변수를 사용하십시오. –

+2

데이터베이스에 파일을 저장하는 것은 좋은 생각이 아닙니다. 왜 파일 시스템을 사용하지 않고 데이터베이스의 경로 만 저장합니까? –

+0

final_file file_type 및 new_size를 데이터베이스에 업로드 할 수 없습니다. –

답변

관련 문제