2015-01-10 3 views
-1

나는 이 이미지를 blob로 MySQL 데이터베이스로 보내는 파일 업로드 검사기를 코딩하려고 애쓰다. 이미지는 (양식 ID는 1)의 중력 양식 파일 업로드에서 가져옵니다. 아래 스크립트를 실행하면 mallampati_images 테이블에 blob이 전송되지 않지만 파일 형식 경고가 표시됩니다. 또한이 오류를 출력합니다 :왜 내 BLOB가 전송되지 않습니까?

Warning: file_get_contents(/home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/uploads/mallampati.png): failed to open stream: No such file or directory in /home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/themes/twentytwelve/functions.php on line 535 

파일에 대한 링크의 형식을 지정하는 방법은 잘 모르겠습니다. 나는 몇 시간 동안 그물에서 찾을 수있는 모든 방법을 시도해 왔지만 그것을 작동하게 만들 수는 없다.

function testimage($path) 
{ 
    if(!preg_match("/\.(png|jpg|gif)$/",$path,$ext)) return 0; 

    $ret = null; 
    switch($ext[1]) 
    { 
     case 'png': $ret = @imagecreatefrompng($path); break; 
     case 'jpg': $ret = @imagecreatefromjpeg($path); break; 
     case 'gif': $ret = @imagecreatefromgif($path); break; 
     default: $ret = 0; 
    } 

    return $ret; 
} 

add_action("gform_after_submission_1", "input_fields", 10, 3); 
function input_fields($entry){ 
    global $wpdb; 

    if (isset($_FILES['input_1'])) { 
    $file_url = $entry['1']; 
    //$img_blob = file_get_contents($file_url); 
    $validate = testimage($file_url); 

    $udir = wp_upload_dir(); 
    $basedir = $udir['basedir']; 
    $target=$basedir.'/'.basename($_FILES['input_1']['name']); 

    //$try = $_FILES['input_1']['tmp_name']; 
    $img_blob = file_get_contents ($target); 
    echo "<script type='text/javascript'>alert('value: $target');</script>"; 
    } 

    if(!empty($validate)) { 
    echo "<script type='text/javascript'>alert('The file was of the correct format');</script>"; 

    $SQL = "INSERT INTO mallampati_images (img_blob) VALUES ($img_blob)"; 
    $wpdb->query($SQL); 
    } 
} 
+0

왜 downvote입니까? – Raoul

+1

$ _FILES [ 'input_1'] [ 'tmp_name']은 (는) 앱 경로에서 검색하려는 상대 경로가/home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/themes/twentytwelve 일 수 있습니다. 다른 경로를 시도해보십시오. 먼저 하드 코딩 된 경로로 테스트하십시오. 추신 : 당신을 downvoted하지 않았습니다. – Codeek

+1

'BLOB'을 삽입하기 위해'wpdb'에서 지원이있는 것처럼 보이지 않으므로 mysqli를 직접 사용해야 할 것입니다. 불행하게도 그들은 또한 mysqli 인스턴스를 getter로 사용할 수 없으므로 wpdb를 확장하거나 새로운 mysqli 인스턴스를 만들어야한다.이 질문을 참조하십시오 : http://stackoverflow.com/q/27643175/215966 – prodigitalson

답변

0

나는 마침내 그것을 만든 : 나는 스크립트 (즉 ... 것 같다, 올바른 절대 링크입니다)

링크를 다시 보려면 아래의 코드를 편집했다.

중력 양식은 제가보고있는 예제처럼 파일을 정확하게 처리하지 못하는 것 같습니다. 문제는 SQL 쿼리에서 잘못된 파일 경로가 사용되었다는 것입니다.

function testimage($path) 
{ 
    if(!preg_match("/\.(png|jpg|gif)$/",$path,$ext)) return 0; 

    $ret = null; 
    switch($ext[1]) 
    { 
     case 'png': $ret = @imagecreatefrompng($path); break; 
     case 'jpg': $ret = @imagecreatefromjpeg($path); break; 
     case 'gif': $ret = @imagecreatefromgif($path); break; 
     default: $ret = 0; 
    } 

    return $ret; 
} 

add_action("gform_after_submission_1", "input_fields", 10, 2); 
function input_fields($entry){ 
    global $wpdb; 

    if(isset($entry[1])){ 
     $valid = testimage($entry[1]); 

     if($valid){ 
     $mpFilePath= $entry[1]; 
     $blob = file_get_contents($mpFilePath) or die ('cannot read file'); 
     $blob = mysql_escape_string($blob); 
     $SQL = "INSERT INTO mallampati_images (img_blob) VALUES ('$blob')"; 
     $wpdb->query($SQL) or die ('query failed'); 
     } 
     else{ 
     echo "<script type='text/javascript'>alert('Incorrect file format');</script>"; 
     } 
    } 
} 
관련 문제