2012-04-10 75 views
0

我想知道是否有人可以幫助我。創建文件夾並插入,如果不存在

我使用Aurigma的圖片上傳軟件,允許用戶上傳他們訪問的地點的圖片。該信息通過如下所示的腳本保存:

<?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(); 
    $username=$packageFields["username"]; 
    $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); 
    } 

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

    $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('username', $username); 
    $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(); 
?> 

在additon到我補充說,根據目前的「locationid」創建一個文件夾,它的名字代碼原始腳本。如下所示。

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

我喜歡什麼納入,是一個檢查,看起來,看看是否有一個文件夾中已經設置與當前'locationid的價值,如果無法創建文件夾。我天真地沒有在PHP的專家,但我知道,要檢查是否存在一個文件,if(file exists....)可以使用,但我只是想知道是否有人可以告訴我,請問我可以如何執行此檢查的文件夾名稱?

非常感謝

克里斯

回答

1

我覺得is_dir()是你在找什麼。

UPDATE:

的代碼,你有:

if (!is_dir($absGalleryPath . $locationfolder)) { 
    mkdir($absGalleryPath . $locationfolder, 0777); 
} 

不正是你想要的。它檢查文件夾,如果它不存在,它會爲你創建一個文件夾(使用CHMOD 777)。沒有看到你的問題是...

+1

非常感謝您的澄清。我不得不承認,我並不是100%確定代碼的全部內容。因爲我對此比較陌生,所以我做了一個「複製和粘貼」,只是改變了我想要的東西。但從知識豐富的人那裏得到一些指導是很好的。問候 – IRHM 2012-04-10 13:44:48

+0

你總是歡迎,很高興我一直在幫助 – Michal 2012-04-10 13:53:14

相關問題