2016-07-15 43 views
1
<?php 
error_reporting(~E_NOTICE); 
require_once 'dbconfig.php'; 

if($_POST) 
{ 
    $firstname = $_POST['firstname']; 
    $lastname = $_POST['lastname']; 
    $description = $_POST['description']; 
    $election_name=$_POST['election_name']; 
    $category_name=$_POST['category_name']; 

    $imgFile = $_FILES['user_image']['name']; 
    $tmp_dir = $_FILES['user_image']['tmp_name']; 
    echo $imgSize = $_FILES['user_image']['size']; 

    $upload_dir = 'user_images'; // upload directory 

    $imgExt = strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); // get image extension 

    // valid image extensions 
    $valid_extensions = array('jpg', 'jpeg', 'png', 'gif'); // valid extensions 

    // rename uploading image 
    $photo = rand(1000,1000000).".".$imgExt; 

    // allow valid image file formats 
    if(in_array($valid_extensions,$imgExt)) {    

    // Check file size '5MB' 
    if($imgSize > 5000000) { 
     move_uploaded_file($tmp_dir,"$upload_dir/$imgFile"); 
     try { 

      $stmt = $db_con->prepare("INSERT INTO candidate(firstname,lastname,description,election_name,category_name,photo) VALUES(:ename, :edept, :esalary, :elect, :cat, :ima)"); 
      $stmt->bindParam(":ename", $firstname); 
      $stmt->bindParam(":edept", $lastname); 
      $stmt->bindParam(":esalary", $description); 
      $stmt->bindParam(":elect", $election_name); 
      $stmt->bindParam(":cat", $category_name); 
      $stmt->bindParam(":ima", $photo); 

      if($stmt->execute()) 
      { 
       echo "Successfully Added"; 
      } 
      else{ 
       echo "Query Problem"; 
      } 
     } 
     catch(PDOException $e){ 
      echo $e->getMessage(); 
     } 
    } 
    else{ 
     echo "Sorry, your file is too large."; 
    } 
    } 
    else { 
     echo "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";  
    } 
?> 

我有這樣的代碼上傳圖像和文本文件,但它說對不起,只有JPG,JPEG,PNG & GIF文件是允許的。我嘗試過多次解決,但是我無法自己找到解決方案,請提供正確的方法。上傳圖片到文件夾,並在PHP中插入數據庫的路徑和文本文件

+0

.txt文件不在您允許的擴展名列表中... –

+0

上傳哪個文件? –

+0

發生了什麼?問題解決了? –

回答

0

只需添加txt擴展在烏爾aarray

$valid_extensions = array('jpg', 'jpeg', 'png', 'gif', 'txt'); 
1

您需要更改

if (in_array($valid_extensions, $imgExt)) { 

if (in_array($imgExt,$valid_extensions)) { 

in_array需要第一個參數作爲搜索值和第二個參數作爲數組

你總是返回false值,你得到了錯誤

+0

謝謝大家,我只是改變了這樣的,但仍然說對不起,只允許JPG,JPEG,PNG和GIF文件。 – Gezachew

+0

你的文件名是什麼,你必須上傳? – Saty

+0

Gezachew

0

添加在你的代碼這樣的擴展名爲.txt,

$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','.txt'); 

希望這將有助於。

0

你只需要修正你的代碼在這一行:

$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt'); // valid extensions 

它會奏效。

0
if(in_array($imgExt,$valid_extensions))  
$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt'); 

您將需要使用此代碼來修改代碼...

0

此代碼將在數據庫中插入圖像的文件夾和路徑:

<?php 
$grocery=$_POST['grocery']; 
$connect=new mysqli("localhost","root",""); 
mysqli_select_db($connect,'go-web'); 
$target_dir = "..images/"; 
$target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]); 
$uploadOk = 1; 
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION); 
// Check if image file is a actual image or fake image 
if(isset($_POST["submit"])) { 
    $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]); 
    if($check !== false) { 
     echo "File is an image - " . $check["mime"] . "."; 
     $uploadOk = 1; 
    } else { 
     die("File is not an image."); 
     $uploadOk = 0; 
    } 
} 
// Check if file already exists 
if (file_exists($target_file)) { 
    die("Sorry, file already exists."); 
    $uploadOk = 0; 
} 
// Check file size 
if ($_FILES["fileToUpload"]["size"] > 500000) { 
    die("Sorry, your file is too large."); 
    $uploadOk = 0; 
} 
// Allow certain file formats 
if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg") { 
    die("Sorry, only JPG, JPEG & PNG files are allowed."); 
    $uploadOk = 0; 
} 
// Check if $uploadOk is set to 0 by an error 
if ($uploadOk == 0) { 
    die("Sorry, your file was not uploaded."); 
// if everything is ok, try to upload file 
} else { 
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) { 
     echo "The file ". basename($_FILES["fileToUpload"]["name"]). " has been uploaded."; 
    } else { 
     die("Sorry, there was an error uploading your file."); 
    } 
    } 
    $query=mysqli_query($connect,"INSERT INTO product VALUES ('$grocery','$target_file')"); 
    if($query) 
    echo "Uploaded"; 
?>* 
0

據對我來說,

1)你寫的文本文件也正在上傳。但是,沒有文本文件擴展名添加到$valid_extensions

更改爲

$valid_extensions = array('jpg', 'jpeg', 'png', 'gif','txt'); // valid extensions 

2)作爲@Saty回答,你需要換你的參數

if (in_array($valid_extensions, $imgExt)) {

if (in_array($imgExt,$valid_extensions)) {

第一個參數作爲Search Value,第二個參數作爲Search Array

相關問題