2017-04-01 48 views
-1
文件夹中插入

荫尝试使用PHP和MySQL插入文件夹中的图像,但图像不是在文件夹中的数据库 存储以及这是我的PHP代码图像不使用PHP和MYSQL

if(isset($name)) { 
$pass = base64_encode($pass); 
$cpass = base64_encode($cpass); 
$image_name = $_FILES['image']['name']; 
$image_temp = $_FILES['image']['tmp_name']; 
$image_type = $_FILES['image']['type']; 
if ($image_type == 'image/png' || $image_type == 'image/jpg' || $image_type == 'image/iso' || $image_type = 'image/gif') { 
    $upload = move_uploaded_file('uploads1/' . $_FILES['image']['name']); 
     if($upload) { 
      $reg_query = mysqli_query($conn, "INSERT INTO `quiz_register`(`name`, `email`, `mobile`, `password`, `cpassword`, `image`) VALUES ('$name','$mail','$cell','$pass','$cpass','$image_name')"); 
      if ($reg_query) { 
       echo 'success'; 
      } else { 
       echo 'fail'; 
      } 
     } 
    } 
} 

这是我的HTML代码

 <form method="post" action="" enctype="multipart/form-data"> 
     <input type="text" name="name" placeholder="name" class="form-control" id="name"> <br> 
     <input type="email" name="mail" placeholder="[email protected]" class="form-control" id="mail"> <br> 
     <input type="text" name="mobile" placeholder="Mobile" class="form-control" id="mobile"> <br> 
     <input type="password" name="password" placeholder="password" class="form-control" id="password"> <br> 
     <input type="password" name="cpassword" placeholder="confirm-password" class="form-control" id="cpassword"> <br> 
     <input type="file" name="image" id="file" class="form-control" required> <br> 
</form> 

enter image description here

回答

0

我最近做了这个。以下是我使用和它的工作就像一个魅力

$binary=base64_decode($file); 
$file = fopen('../newSite/profileImages/'.$filename, 'wb'); 
// Create File 
$is_written = fwrite($file, $binary); 
fclose($file); 

if($is_written > 0) { 

    $connection = mysqli_connect('localhost', 'username', 'pass','db name'); 

      $query = "UPDATE users SET image = '$filename' WHERE id = '$user_id';"; 

    $result = mysqli_query($connection, $query) ; 

    if($result){ 
     echo "success"; 
    }else{ 
     echo "failed"; 
    } 

    mysqli_close($connection); 
} 
0
<?php 

     if(isset($_POST['submit'])) 
     { 
     $filename=$_FILES['image']['name']; 
     $filetempname=$_FILES['image']['tmp_name']; 
     $fname=md5($_SERVER['REMOTE_ADDR'].rand()).$filename; 
     $filepath="uploads/uploads1/".$fname; 
     $upload=move_uploaded_file($filetempname,$filepath1); 
if($upload) { 
      $reg_query = mysqli_query($conn, "INSERT INTO `quiz_register`(`name`, `email`, `mobile`, `password`, `cpassword`, `image`) VALUES ('$name','$mail','$cell','$pass','$cpass','$filepath')"); 
      if ($reg_query) { 
       echo 'success'; 
      } else { 
       echo 'fail'; 
      } 
     } 
?> 

您检查这个代码,可能会对你有所帮助。

+1

此代码对你有帮助 – 2017-04-01 06:38:18

+0

我想你的代码,但是当IAM插入,图像列显示quiz_register表空白,其余的值将除了图像 –

+0

在其中输入IMAGE_NAME您使用$ filepath1我是更新我的代码 – 2017-04-01 06:59:50

0
just reviewed your code i think the following code could work you please go through it and let me know if there is some issue.. 

<?php 
$con = mysqli_connect("localhost","root","","test"); 


if(isset($_POST['submit'])) 
{ 
     $pass = base64_encode($_POST['password']); 
     $cpass = base64_encode($_POST['cpassword']); 
     $name=$_POST['name']; 
     $mail=$_POST['mail']; 
     $cell=$_POST['mobile']; 
     $image_name = $_FILES['image']['name']; 
     $image_temp = $_FILES['image']['tmp_name']; 
     $image_type = $_FILES['image']['type']; 
     if ($image_type == 'image/png' || $image_type == 'image/jpg' ||       $image_type == 'image/iso' || $image_type = 'image/gif') 
    { 
     $upload = move_uploaded_file($_FILES["image"]["tmp_name"],'uploads1/' . $_FILES['image']['name']); 
     if($upload) 
     { 
      $reg_query = mysqli_query($con, "INSERT INTO `quiz_register`(`name`, `email`, `mobile`, `password`, `cpassword`, `image`) VALUES ('$name','$mail','$cell','$pass','$cpass','$image_name')"); 
      if ($reg_query) { 
       echo 'success'; 
      } else { 
       echo 'fail'; 
      } 
     } 
    } 
} 
?>