2012-06-21 67 views
0

我需要一些關於PHP類的幫助。我對它的工作原理知之甚少,並試圖發現更多。同時,我遇到了一個問題,我需要在通過類中的這些函數運行之後獲取變量。PHP類 - 將變量發送到插入查詢

protected function upcount_name_callback($matches) { 
     $index = isset($matches[1]) ? intval($matches[1]) + 1 : 1; 
     $ext = isset($matches[2]) ? $matches[2] : ''; 
     return ' ('.$index.')'.$ext; 
    } 

    protected function upcount_name($name) { 
     return preg_replace_callback(
      '/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/', 
      array($this, 'upcount_name_callback'), 
      $name, 
      1 
     ); 
    } 

我需要在下面的JS語句中檢索這個變量併發送到我的INSERT php文件。

$('#albumBack.fileupload').bind('fileuploaddone',function(e,data) { 

    //Loop through each page and return object and write to DB 
    $.each(data.files, function (index, file) { 
     var filename = file.name; 
     $.ajax({ 
      type: "POST", 
      url: "../albumUploader/queries/albumPages.php", 
      data: {file: filename} 
     }); 
    }); 
}); 

我目前得到的文件名是原始名稱,而不是附加名稱。 感謝您的任何幫助。

albumPages.php

//Variables for gallerimage table 
    $originalName = $_POST['file']; 
    $clientRef = $_SESSION['clientRef']; 
    $galleryID = $_SESSION['newGalleryId']; 
    $galleryLayout = $_SESSION['layoutID']; 
    $imageID = rand(); 

    //Find the sort# for the gallery 
    $qSortOrder = mysql_query("SELECT MAX(sort) AS sortOrder 
            ,id 
            ,clientRef 
            ,galleryId 
            FROM galleryimage 
            WHERE galleryId='{$galleryID}' 
            AND clientRef= '{$clientRef}' 
            "); 
    $fsortOrder = mysql_fetch_array($qSortOrder); 
    //Latest revision 
    $orderNumber = $fsortOrder['sortOrder']; 
    $order = $orderNumber + 1; 

    $query = "INSERT INTO galleryimage 
      (
       galleryId 
       ,image 
       ,OrgImageName 
       ,clientRef 
       ,sort 
       ,layout 
      ) VALUES (
       '{$galleryID}' 
       ,'{$imageID}' 
       ,'{$originalName}' 
       ,'{$clientRef}' 
       ,'{$order}' 
       ,'{$galleryLayout}' 
      )"; 
    $return = mysql_query($query); 

$ ORIGINALNAME是$變量,我需要將其定義爲img.jpg,$ IMG(1).JPG等我只是發表文件,它最終成爲了來自輸入的原始選定文件。

這是選擇文件的格式。

<form class="fileupload" id="albumBack" action="../js/jQuery-file-upload/server/php/" method="POST" enctype="multipart/form-data" > 
       <!-- The fileupload-buttonbar contains buttons to add/delete files and start/cancel the upload --> 
       <div class="row fileupload-buttonbar"> 
         <span class="btn btn-success fileinput-button"> 
          <span>Choose Back</span> 
          <input type="file" name="files[]"> 
         </span> 
        </div> 
+0

這是你正在使用的整個腳本? –

+0

js還是PHP類?還有更多的PHP類。至於js,是的,這是所有的腳本,我將編輯完整的 – Bungdaddy

+0

作爲一邊;你應該真的使用PHP PDO:http://php.net/manual/en/book.pdo.php你目前很容易受到SQL注入攻擊。 –

回答

1

不知道albumBack.php的內容,很難確切地確定你會看到的行爲。但讓我們假設albumBack.php在某個時候調用upcount_name($name)

然後會發生的事情是調用preg_replace_callback。這個函數特別需要一個字符串和一個正則表達式,以及一個額外的「回調」函數。它在字符串上運行正則表達式,並獲取一組匹配,然後傳遞給回調函數。回調允許您以靈活的方式「注入」行爲:在這種情況下,回調是定義替換字符串應該用於每個正則表達式匹配的一種方式。

upcount_name_callback函數就是這裏調用的函數。要理解它的作用,你必須理解正則表達式。這有點複雜,但實際上它正在尋找一些數字([\d]+部分),.後跟行結束。 upcount_name_callback將此作爲一個數字,它將遞增並添加一個擴展,如果它存在,它將按原樣返回,如果不存在,則爲空字符串。

結果是,這似乎將'blah.jpg'更改爲'blah1.jpg'和'blah1.jpg'更改爲'blah2.jpg'。然而,它看起來像你回來的名字是一樣的,它並沒有被實際執行。您需要發佈albumBack.php中的代碼調用行來確認。

+0

編輯我的代碼,謝謝@Nathaniel Ford - 我會仔細看看你的答案。 – Bungdaddy

+0

此外,正在寫入服務器的文件被寫爲blah(1).jpg,blah(2).jpg等....我只需要能夠檢索該名稱並將其發送到albumPages。 php – Bungdaddy

+0

是的;我掩飾了正則表達式中的括號,因爲重點是增加數字。當你說'找回這個名字並將它發送到albumPages.php'時,你的含義並不清楚。 albumPages.php中的代碼段不會與您發佈的其他代碼進行互動? –