2014-09-27 20 views
10

Tôi mới dùng php, tôi đã tạo mã để tải hình ảnh lên cơ sở dữ liệu sql và truy xuất hình ảnh bằng cách sử dụng php.Lỗi nghiêm trọng: Gọi hàm không xác định finfo_open() trong php

Đây là mã của tôi:

<html> 
<head><title>File Insert</title></head> 
<body> 
<h3>Please Choose a File and click Submit</h3> 

<form enctype="multipart/form-data" action= 
"<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> 
<input type="hidden" name="MAX_FILE_SIZE" value="10000000" /> 
<input name="userfile" type="file" /> 
<input type="submit" value="Submit" /> 
</form> 

<?php 

// check if a file was submitted 
if(!isset($_FILES['userfile'])) 
{ 
    echo '<p>Please select a file</p>'; 
} 
else 
{ 
    try { 
    $msg= upload(); //this will upload your image 
    echo $msg; //Message showing success or failure. 
    } 
    catch(Exception $e) { 
    echo $e->getMessage(); 
    echo 'Sorry, could not upload file'; 
    } 
} 

// the upload function 

function upload() { 
    include "mysqlconnect.php"; 
    $maxsize = 10000000; //set to approx 10 MB 

    //check associated error code 
    if($_FILES['userfile']['error']==UPLOAD_ERR_OK) { 

     //check whether file is uploaded with HTTP POST 
     if(is_uploaded_file($_FILES['userfile']['tmp_name'])) {  

      //checks size of uploaded image on server side 
      if($_FILES['userfile']['size'] < $maxsize) { 

       //checks whether uploaded file is of image type 
       //if(strpos(mime_content_type($_FILES['userfile']['tmp_name']),"image")===0) { 
       $finfo = finfo_open(FILEINFO_MIME_TYPE); 
       if(strpos(finfo_file($finfo, $_FILES['userfile']['tmp_name']),"image")===0) {  

        // prepare the image for insertion 
        $imgData =addslashes (file_get_contents($_FILES['userfile']['tmp_name'])); 

        // put the image in the db... 
        // database connection 
        mysql_connect($host, $user, $pass) OR DIE (mysql_error()); 

        // select the db 
        mysql_select_db ($db) OR DIE ("Unable to select db".mysql_error()); 

        // our sql query 
        $sql = "INSERT INTO test_image 
        (image, name) 
        VALUES 
        ('{$imgData}', '{$_FILES['userfile']['name']}');"; 

        // insert the image 
        mysql_query($sql) or die("Error in Query: " . mysql_error()); 
        $msg='<p>Image successfully saved in database with id ='. mysql_insert_id().' </p>'; 
       } 
       else 
        $msg="<p>Uploaded file is not an image.</p>"; 
      } 
      else { 
       // if the file is not less than the maximum allowed, print an error 
       $msg='<div>File exceeds the Maximum File limit</div> 
       <div>Maximum File limit is '.$maxsize.' bytes</div> 
       <div>File '.$_FILES['userfile']['name'].' is '.$_FILES['userfile']['size']. 
       ' bytes</div><hr />'; 
       } 
     } 
     else 
      $msg="File not uploaded successfully."; 

    } 
    else { 
     $msg= file_upload_error_message($_FILES['userfile']['error']); 
    } 
    return $msg; 
} 

// Function to return error message based on error code 

function file_upload_error_message($error_code) { 
    switch ($error_code) { 
     case UPLOAD_ERR_INI_SIZE: 
      return 'The uploaded file exceeds the upload_max_filesize directive in php.ini'; 
     case UPLOAD_ERR_FORM_SIZE: 
      return 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form'; 
     case UPLOAD_ERR_PARTIAL: 
      return 'The uploaded file was only partially uploaded'; 
     case UPLOAD_ERR_NO_FILE: 
      return 'No file was uploaded'; 
     case UPLOAD_ERR_NO_TMP_DIR: 
      return 'Missing a temporary folder'; 
     case UPLOAD_ERR_CANT_WRITE: 
      return 'Failed to write file to disk'; 
     case UPLOAD_ERR_EXTENSION: 
      return 'File upload stopped by extension'; 
     default: 
      return 'Unknown upload error'; 
    } 
} 
?> 
</body> 
</html> 

Bây giờ tôi đã nhận lỗi, Fatal error: Call to undefined function finfo_open() trên dòng này

$finfo = finfo_open(FILEINFO_MIME_TYPE); . 

ai đó có thể giúp tôi để sửa lỗi này.

Xin cảm ơn trước.

+0

finfo_open chỉ khả dụng cho php> = 5.3.0 http://www.php.net/manual/en/function.finfo-open.php. Bạn có thể muốn kiểm tra lại máy chủ của mình đã cập nhật với php_info(). Liên kết này có thể trợ giúp: http://stackoverflow.com/questions/21293996/wordpress-plugin-call-to-undefined-function-finfo-open – Jinandra

+0

ok cảm ơn @sanki –

+0

@sanki: từ liên kết của bạn, tôi xóa điều đó tập tin php.ini semicolon nơi dòng này "extension = php_fileinfo.dll" nhưng vẫn hiển thị lỗi đó –

Trả lời

12

Tôi đã gặp vấn đề tương tự với PHP trên IIS. Hãy chắc chắn rằng bạn có fileinfo.so hoặc php_fileinfo.dll được kích hoạt trong php.ini như vậy, mà phụ thuộc vào nền tảng và các phiên bản của PHP và hệ điều hành:

Nên có một dòng tương tự như

;extension=fileinfo.so hoặc ;extension=php_fileinfo.dll

loại bỏ các dấu chấm phẩy ; (bỏ ghi chú nó)

extension=fileinfo.so hay extension=php_fileinfo.dll

Sau đó lưu và khởi động lại apache, nginx, IIS hoặc bất kỳ máy chủ web nào bạn đang sử dụng.

Nếu cách này không hiệu quả, hãy đảm bảo rằng tiện ích mở rộng fileinfo đã được cài đặt.

Các vấn đề liên quan