2012-03-29 227 views
1

我想知道是否有人可以幫助我。將'用戶名'保存爲文件名

我使用Aurigma的Image Uploader,爲了保存上傳的圖像,我把這個腳本放在一起。

<?php 

//This variable specifies relative path to the folder, where the gallery with uploaded files is located. 
//Do not forget about the slash in the end of the folder name. 
$galleryPath = 'UploadedFiles/'; 

require_once 'Includes/gallery_helper.php'; 

require_once 'ImageUploaderPHP/UploadHandler.class.php'; 

/** 
* FileUploaded callback function 
* @param $uploadedFile UploadedFile 
*/ 
function onFileUploaded($uploadedFile) { 

    $packageFields = $uploadedFile->getPackage()->getPackageFields(); 
    $userid = $packageFields["userid"]; 
    $locationid= $packageFields["locationid"]; 

    global $galleryPath; 

    $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 
    $absThumbnailsPath = $absGalleryPath . 'Thumbnails' . DIRECTORY_SEPARATOR; 

    if ($uploadedFile->getPackage()->getPackageIndex() == 0 && $uploadedFile->getIndex() == 0) { 
    initGallery($absGalleryPath, $absThumbnailsPath, FALSE); 
    } 

    $dirName = $_POST['folder']; 
    $dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $dirName); 
    if (!is_dir($absGalleryPath . $dirName)) { 
    mkdir($absGalleryPath . $dirName, 0777); 
    } 

    $path = rtrim($dirName, '/\\') . '/'; 

    $originalFileName = $uploadedFile->getSourceName(); 

    $files = $uploadedFile->getConvertedFiles(); 

    // save converter 1 

    $sourceFileName = getSafeFileName($absGalleryPath, $originalFileName); 
    $sourceFile = $files[0]; 
    /* @var $sourceFile ConvertedFile */ 
    if ($sourceFile) { 
    $sourceFile->moveTo($absGalleryPath . $sourceFileName); 
    } 

    // save converter 2 

    $thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName); 
    $thumbnailFile = $files[1]; 
    /* @var $thumbnailFile ConvertedFile */ 
    if ($thumbnailFile) { 
    $thumbnailFile->moveTo($absThumbnailsPath . $thumbnailFileName); 
    } 

    //Load XML file which will keep information about files (image dimensions, description, etc). 
    //XML is used solely for brevity. In real-life application most likely you will use database instead. 
    $descriptions = new DOMDocument('1.0', 'utf-8'); 
    $descriptions->load($absGalleryPath . 'files.xml'); 

    //Save file info. 
    $xmlFile = $descriptions->createElement('file'); 
    $xmlFile->setAttribute('name', $_POST['folder'] . '/' . $originalFileName); 
    $xmlFile->setAttribute('source', $sourceFileName); 
    $xmlFile->setAttribute('size', $uploadedFile->getSourceSize()); 
    $xmlFile->setAttribute('originalname', $originalFileName); 
    $xmlFile->setAttribute('thumbnail', $thumbnailFileName); 
    $xmlFile->setAttribute('description', $uploadedFile->getDescription()); 
    //Add additional fields 
    $xmlFile->setAttribute('userid', $userid); 
    $xmlFile->setAttribute('locationid', $locationid); 
    $xmlFile->setAttribute('folder', $dirName); 
    $descriptions->documentElement->appendChild($xmlFile); 
    $descriptions->save($absGalleryPath . 'files.xml'); 
} 

$uh = new UploadHandler(); 
$uh->setFileUploadedCallback('onFileUploaded'); 
$uh->processRequest(); 
?> 

我想要做的是替換文件名的files元素,並與username替換它,所以每個保存文件夾和相關文件可以中鑑定到每個用戶。

我添加了一個用戶名文本字段到這個腳本保存來自

我覺得我說得很對,這是一個需要改變$descriptions->save($absGalleryPath . 'files.xml');線的形式。

因此,我嘗試將此更改爲$descriptions->save($absGalleryPath . '$username.xml$descriptions->save($absGalleryPath . $username '.xml,但這些都沒有奏效,因此我不太清楚需要更改哪些內容。

我只是想知道是否有人可以看看這個請讓我知道我要去哪裏錯了。

非常感謝

回答

0

'$ username.xml' 將被解釋爲$ username.xml,你需要使用 「$ username.xml」。單引號「禁用」字符串內部的變量使用。

你正在嘗試可能是一個壞主意,因爲你正在創建一個用戶名不能包含像「/」這樣的特殊字符。如果你有一條規則阻止「/」成爲用戶名的一部分,也許不是問題。

+0

非常感謝您花時間幫助我。親切的問候。 – IRHM 2012-03-29 16:49:26