2012-05-28 64 views
0

我在我的表單中有一個上傳字段。copyserviceimage函數ajax wordpress

我有這個代碼在我的form.php的

<form id="photoform" name="photoform" method="post" onSubmit="return false" enctype="multipart/form-data"> 
        <div id="upphotosection"></div> 
        <label>Upload photo</label> 
        <input name="uploadphoto" id="uploadphoto" type="file" /> 
        <div class="innerformclear"></div> 
        <input id="hidden" value="" name="hidden" type="hidden" /> 
        <label>&nbsp;</label> 
        <input name="upphoto_submit" id="upphoto_submit" type="submit" value="Submit"/> 
        <div style="clear:both;">&nbsp;</div> 
        <label>&nbsp;</label> 
        <div id="result_upphoto_submit"></div> 
        </form> 

這是代碼在formaction.php

header('content-type: application/json; charset=utf-8'); 
header("access-control-allow-origin: *"); 

include("../../../wp-load.php"); 
session_start(); 

    $err = ''; 
    $success = ''; 
    global $wpdb, $PasswordHash, $current_user, $user_ID; 

    if($upphoto = $_FILES['uploadphoto']["name"] != ''){ 
    $path = 'images/profilephotos/';  
    $upphoto = copyServiceImage($path,$_FILES['uploadphoto']) ;} 

    $postid = 52; 


############### Check Duplication 
$sel_photo = "SELECT * FROM `pro_table` WHERE `post_id` = '".$postid."'"; 
$sel_res = mysql_query($sel_photo) ; 
if(mysql_num_rows($sel_res) == 0){ 
$ins_photo = "Insert into pro_table (post_id,photo_photo,int_status) values ('$postid','$upphoto','0')"; 
$ins_res = mysql_query($ins_photo); 
} 
else{ 
$upd_photo = "Update pro_table set photo_photo = '$upphoto' ,int_status='0' where post_id = '$postid' "; 
$upd_res = mysql_query($upd_photo); 
} 


echo $_GET['callback'] . '('.json_encode("success").')'; 

這是Java腳本文件中的代碼。

$(document).ready(function() { 
///////////// Submit action for upload photo   
    $("#upphoto_submit").click(function() { 

     if(document.getElementById('uploadphoto').value == ''){ 
      alert("Please upload Photo"); 
      document.getElementById('uploadphoto').focus(); 
      return false; 
     } 
     else { 
      $('#result_upphoto_submit').html('<img src="http://www.test.com/test/uploads/2012/04/ajax-loader.gif" class="loader" align="absmiddle" />&nbsp;').fadeIn(); 
       var input_data = $('#photoform').serialize(); 
       $.ajax({ 
       type: "POST", 
       url: "http://www.test.com/test/themes/test/formaction.php", 
       dataType: 'jsonp', 
       data: input_data, 
       success: function(msg){ 
        $('#result_upphoto_submit').html('&nbsp;'); 
        if(msg == 'success') { 
         msg = '<p class="success_custom">Photo successfully added.</p>'; 
         $('<div>').html(msg).appendTo('div#upphotosection').hide().fadeIn('slow'); 
        } else { 

         msg = ' Exists'; 
         alert("Error in updation"); 

        } 

       } 
       }); 
       return false; 
     }  

     }); 
}); 

如果我瀏覽圖片並點擊提交,它會顯示我的成功消息Photo successfully added。但在個人資料照片文件夾中沒有圖像,管理面板中沒有任何數據存儲。我無法跟蹤錯誤。我該如何糾正?

回答

0

是否有錯誤?

$_FILES["uploadphoto"]["error"]; 

也是我不知道WordPress的是如何處理的圖像,但我錯過了文件移動:

if ($_FILES["uploadphoto"]["error"] != UPLOAD_ERR_OK) { 
    $tmp_name = $_FILES["uploadphoto"]["tmp_name"]; 
    $name = $_FILES["pictures"]["name"]; 
    $path = "images/profilephotos/".$name; 
    move_uploaded_file($tmp_name,$path); 
} 

或者我想它是在這裏叫什麼名字?

$upphoto = copyServiceImage($path,$_FILES['uploadphoto']) ;}