簡體   English   中英

PHP MySQL:使用Uploadify時將文件詳細信息寫入數據庫

[英]PHP MySQL: Writing file details to DB while using Uploadify

我正在使用Uploadify將文件(圖像)上傳到我的文件結構中,然后捕獲每個文件,並給它一個隨機數作為文件名,然后使用出色的SimpleImage腳本將其大小調整為大,中和小尺寸,然后將其保存到目錄,丟棄原始圖像,並將文件詳細信息(隨機數,原始名稱,專輯ID等)寫入數據庫,以供以后訪問。

所有這些工作都順利進行,除了寫入數據庫部分。 我發現只有第一個文件的信息會傳遞到數據庫,並且文件名的數字不正確。

簡而言之,當在多個圖像上使用Uploadify時,如何將每個文件的信息正確地寫入數據庫?

我當前的腳本:

*注:AlbumID由用戶動態設置,並作為文件數據發布到uploadify。

require_once '../../functions.php';
require_once '../../conn.php';

//defaults
$uploadify_path = '/contents/uploads/gallery/';
$albumID = $_POST['AlbumID'];

//Define a destination
$targetPath = $_SERVER['DOCUMENT_ROOT'] . $_POST['path']; // Relative to the root

if (!empty($_FILES)) {
$tempFile = $_FILES['Filedata']['tmp_name'];
$targetFile = $targetPath . $_FILES['Filedata']['name'];
include($_SERVER['DOCUMENT_ROOT'].'/includes/SimpleImage.php');

// Validate the file type
$fileTypes = array('jpg','jpeg','gif','png'); // File extensions
$fileParts = pathinfo($_FILES['Filedata']['name']);

if (in_array($fileParts['extension'],$fileTypes)) {
    $ran = $albumID.RandNumber(10);
    $location=$targetPath.$ran;

    $LegacyName = $fileParts['filename'];
    $FileExt = $fileParts['extension'];

    if(is_numeric($albumID)) {
        $q = "INSERT INTO gallery_meta (AlbumID, FileName, LegacyName, FileExt, IsDefault, Public)
                VALUES ('$albumID','$ran','$LegacyName','$FileExt','0','1')";
        $r= mysql_query($q);
        mysql_free_result($r);
    }

    move_uploaded_file($tempFile,$targetFile);

    list($width, $height, $type, $attr) = getimagesize($targetFile);

    $image = new SimpleImage();
    $image->load($targetFile);

    if( $height >= 901 ) {
        $image->resizeToHeight(900);
        $image->save($location.'-lrg.'.$FileExt);
        $image->resizeToHeight(550);
        $image->save($location.'-med.'.$FileExt);
        $image->resizeToHeight(200);
        $image->save($location.'-sm.'.$FileExt);
        unlink($targetFile);
    }
    elseif(( $height  >= 551 )&&( $height <= 900 )) {
        $image->save($location.'-lrg.'.$FileExt);
        $image->resizeToHeight(550);
        $image->save($location.'-med.'.$FileExt);
        $image->resizeToHeight(200);
        $image->save($location.'-sm.'.$FileExt);
        unlink($targetFile);
    }
    elseif(( $height >= 201 )&&( $height <= 550 )) {
        $image->save($location.'-lrg.'.$FileExt);
        $image->save($location.'-med.'.$FileExt);
        $image->resizeToHeight(200);
        $image->save($location.'-sm.'.$FileExt);
        unlink($targetFile);
    }
    if( $height  <= 200 ) {
        $image->save($location.'-lrg.'.$FileExt);
        $image->save($location.'-med.'.$FileExt);
        $image->save($location.'-sm.'.$FileExt);
        unlink($targetFile);
    }

    echo '1';
} else {
    echo 'Invalid file type.';
}
}

添加一個自動遞增鍵表,並更改ALBUMID成BIGINT如果你確定你的價值觀是不會溢出的大小BIGINT ,但如果是我建議你保存“ALBUMID”為VARCHAR(30) ,因為它比實際的數字鍵更像一個名稱,尤其是因為它看起來可以是任意長度。

我還認為,您應該使用time()附加當前時間戳,而不是最后使用隨機數,這樣就不會像具有隨機數和用戶輸入的系統那樣產生潛在的沖突。

順便說一句, $_POST['albumId'] ,也被用戶直接輸入..,使您的數據庫容易受到MySQL注入攻擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM