2012-04-14 45 views
0

我想知道是否有人能夠幫助我請。使用字段值的文件路徑

我試圖加載具有以下文件路徑的XML文件:

/UploadedFiles/username/location/files.xml 

我一直在嘗試了一段時間做的,就是用兩種形式字段的值,「用戶名」和'位置'並將它們合併到文件路徑中,但我似乎無法得到正確的結果。

我只是想知道是否某個人可能會告訴我如何獲取這些值並在文件路徑中使用它們。

非常感謝

POST修訂

我以爲我可以通過簡單地改變文件的路徑,因此,我原來的職位解決我的問題,但我擔心我缺乏的PHP知識,讓我下來,我懷疑我的代碼中存在的問題更深。

請在下面找到更多的細節。

下面的腳本顯示了我最初如何保存圖像。

'upload.php的'

<?php 
require_once 'Includes/gallery_helper.php'; 
require_once 'ImageUploaderPHP/UploadHandler.class.php'; 
$galleryPath = 'UploadedFiles/'; 

function onFileUploaded($uploadedFile) { 
    global $galleryPath; 

    $packageFields = $uploadedFile->getPackage()->getPackageFields(); 
    $username=$packageFields["username"]; 
    $locationid=$packageFields["locationid"]; 
    $username = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['username']); 
    $location = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['locationid']); 
    $dirName = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $_POST['folder']); 

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


    if (!is_dir($absGalleryPath)) mkdir($absGalleryPath, 0777, true); 
    chmod($absGalleryPath, 0777); 
    if (!is_dir($absGalleryPath . $dirName)) mkdir($absGalleryPath . $dirName, 0777, true); 
    chmod($absGalleryPath . $dirName, 0777); 

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

    $originalFileName = $uploadedFile->getSourceName(); 
    $files = $uploadedFile->getConvertedFiles(); 
    $sourceFileName = getSafeFileName($absGalleryPath, $originalFileName); 
    $sourceFile = $files[0]; 

    if ($sourceFile) $sourceFile->moveTo($absGalleryPath . $sourceFileName); 
    $thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName); 
    $thumbnailFile = $files[1]; 
    if ($thumbnailFile) $thumbnailFile->moveTo($absThumbnailsPath . $thumbnailFileName); 

    $descriptions = new DOMDocument('1.0', 'utf-8'); 
    $descriptions->load($absGalleryPath . 'files.xml'); 

    $xmlFile = $descriptions->createElement('file'); 
    // <-- please check the following line 
    $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()); 
    $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(); 
?> 

該腳本產生以下文件結構:

UPLOADEDFILES(預先存在的文件夾)

  • '用戶名'(子文件夾包含'位置'文件夾)
  • 'location'(包含原始圖像的子文件夾'文件s.xml」和 '縮略圖' 文件夾中)
  • '包含原始圖像的縮略圖的縮略圖'(子文件夾)

NB。'用戶名'和'位置文件夾名稱是從當前用戶和位置值派生而來的。

然後我來創建圖像庫來解決我的問題。

下面的代碼是我最初的'gallery.php'腳本。我需要改變腳本的頂部PHP部分到新的文件夾結構相匹配,即UploadedFiles/'username'/'location'/ThumbnailsUploadedFiles/'username'/'location'.files.xml

「gallery.php」

<?php 

    $galleryPath = 'UploadedFiles/'; 

    $thumbnailsPath = $galleryPath . 'Thumbnails/'; 

    $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR; 

    $descriptions = new DOMDocument('1.0'); 
    $descriptions->load($absGalleryPath . 'files.xml'); 
?> 
<head> 
    <title>Gallery</title> 
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
    <link href="Libraries/fancybox/jquery.fancybox-1.3.1.css" rel="stylesheet" type="text/css" /> 
    <link href="Styles/style.css" rel="stylesheet" type="text/css" /> 
    <!--[if IE]> 
    <link href="Styles/ie.css" rel="stylesheet" type="text/css" /> 
    <![endif]--> 
    <script src="Libraries/jquery/jquery-1.4.3.min.js" type="text/javascript"></script> 
    <script src="Libraries/fancybox/jquery.fancybox-1.3.1.pack.js" type="text/javascript"></script> 
    <script type="text/javascript"> 

    $(function() { $('a.fancybox').fancybox(); }); 

    </script> 
    <style type="text/css"> 
<!-- 
.style1 { 
    font-size: 14px; 
    margin-right: 110px; 
} 
.style4 {font-size: 12px} 
--> 
    </style> 
</head> 
<body style="font-family: Calibri; color: #505050; font-size: 9px; border-bottom-width: thin; margin-top: 5px; margin-left: -476px; margin-right: 1px; margin-bottom: -10px;"> 
<div align="right" class="style1"> <a href = "imagefolders.php" /> View Uploaded Images In Folder Structure <a/> &larr; View All Uploaded Images </div> 
    <form id="gallery" class="page"> 
    <div id="container"> 
    <div id="center"> 
     <div class="aB"> 
     <div class="aB-B"> 
      <?php if ('Uploaded files' != $current['title']) :?> 
      <?php endif;?> 
      <div class="demo"> 
      <input name="username" type="text" id="username" value="IRHM73" /> 
      <input name="locationid" type="text" id="locationid" value="1" /> 
      <div class="inner"> 
       <div class="container"> 
       <div class="gallery"> 
        <ul class="gallery-image-list"> 
        <?php for ($i = 0; $i < $descriptions->documentElement->childNodes->length; $i++) : 
          $xmlFile = $descriptions->documentElement->childNodes->item($i); 
          $name = htmlentities($xmlFile->getAttribute('originalname'), ENT_COMPAT, 'UTF-8'); 
          $description = htmlentities($xmlFile->getAttribute('description'), ENT_COMPAT, 'UTF-8'); 
          $folder = htmlentities($xmlFile->getAttribute('folder'), ENT_COMPAT, 'UTF-8'); 
          $source = $galleryPath . rawurlencode($xmlFile->getAttribute('source')); 
          $thumbnail = $thumbnailsPath . rawurlencode($xmlFile->getAttribute('thumbnail')); 
        ?> 
        <li class="item"> 
         <a class="fancybox" target="_blank" rel="original" href="<?php echo $source; ?>"><img class="preview" 
         alt="<?php echo $name; ?>" src="<?php echo $thumbnail; ?>" /></a></li> 
         <p><span class="style4"><b>Image Description:</b> <?php echo htmlentities($xmlFile->getAttribute('description'));?> <br /> 
          <b>Contained in folder:</b> <?php echo htmlentities($xmlFile->getAttribute('folder'));?> </span><br /> 
          <?php endfor; ?> 
          </li> 
         </p> 
        </ul> 
       </div> 
       </div> 
      </div> 
      </div> 
     </div> 
     </div> 
    </div> 
    </div> 
     <div class="aB-a">  </div> 
     </div> 
    </div> 
    </div> 
    </form> 
</body> 
</html> 

我承認自己在如何解決這個真正的損失,我不知道是我的'gallery.php'腳本需要更改還是我的'upload.php'

任何幫助都會非常感激地收到。

親切的問候

+0

如果我正確理解你內,普通的舊串聯會的工作。你有什麼嘗試? – Rick 2012-04-14 15:43:14

+0

您可以編輯此問題以添加更多詳細信息。當然不改變話題。 – Starx 2012-04-15 13:27:48

+0

Hi @Starx,只是爲了讓你知道我已更新我的文章。親切的問候 – IRHM 2012-04-15 14:43:27

回答

1

OK,讓我們說你有這樣的價值觀:usernamelocation

然後,(如果我得到你的問題右)響應的腳本(一個接收表單的動作),試試這個:

<?php 

    $username = $_GET['username']; 
    $location = $_GET['location']; 

    $filepath = "/UploadedFiles/$username/$location/files.xml"; 

?> 

提示(連接字符串的2種方式)

$filepath = "/UploadedFiles/$username/$location/files.xml"; 

OR

$filepath = "/UploadedFiles/".$username."/".$location."/files.xml"; 
+0

嗨,非常感謝。不幸的是我無法得到這個工作。但是,通過我的原始文章,我沒有提供足夠的細節,我爲此道歉。我現在增加了更多細節。親切的問候 – IRHM 2012-04-15 14:11:01

1

無論你在表單中使用post還是get方法,這都可以工作。只要確保您爲輸入文本html元素選擇字段名稱用戶名和位置。

$filepath = "/UploadedFiles/{$_REQUEST['username']}/{$_REQUEST['location']}/files.xml" 
+0

嗨,非常感謝。不幸的是,雖然我沒有接受任何錯誤,但我無法得到這個工作。不過,我認爲這是我的腳本和我缺乏PHP知識而不是您的代碼,我也覺得我原來的帖子沒有包含足夠的細節,對此我表示歉意。我現在加入了這個。親切的問候 – IRHM 2012-04-15 14:11:38

1

你必須將其定義爲變量,包括字符串

$username = "user1"; 
$location = "userlocation1"; 

$url = "/UploadedFiles/$username/$location/files.xml"; 

//Next you might want to verify the location 

if(is_file($url)) { 
    //do your operation.... 
} 
+0

嗨,非常感謝。不幸的是,我無法實現這個目標,但是通過我的原始文章,我沒有提供足夠的細節,我爲此抱歉,因此我提出了一個新帖子。親切的問候 – IRHM 2012-04-15 13:21:44

+0

@IRHM,哪一個是你的帖子? – Starx 2012-04-15 13:27:09

+0

非常感謝您的回覆,我會在幾分鐘內修改該帖子。親切的問候 – IRHM 2012-04-15 13:57:26