0

我想知道是否有人可以帮助我。

我正在尝试使用以下文件路径加载 xml 文件:

/UploadedFiles/username/location/files.xml

一段时间以来,我一直在尝试做的是使用两个表单字段的值,“用户名”和“位置”并将它们合并到文件路径中,但我似乎无法做到这一点。

我只是想知道是否有人可以告诉我如何获取这些值并在文件路径中使用它们。

非常感谢

修改后

我认为我可以通过简单地更改文件路径来解决我的问题,因此我的原始帖子,但我担心我缺乏 PHP 知识让我失望,我怀疑我遇到的问题在我的代码中更深层次。

请在下面找到更多详细信息。

下面的脚本显示了我最初是如何保存图像的。

'上传.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(预先存在的文件夹)

  • “用户名”(包含“位置”文件夹的子文件夹)
  • “位置”(包含原始图像的子文件夹、“files.xml”和“缩略图”文件夹)
  • “缩略图”(包含原始图像缩略图的子文件夹)

注意。“用户名”和“位置”文件夹名称派生自当前用户和位置值。

然后我开始讨论创建图片库的问题。

下面的代码是我最初的“gallery.php”脚本。我需要更改脚本的顶部 PHP 部分以匹配新的文件夹结构UploadedFiles/'username'/'location'/Thumbnails,即UploadedFiles/'username'/'location'.files.xml

'画廊.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”

任何帮助都会非常感激。

亲切的问候

4

3 回答 3

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....
}
于 2012-04-14T15:53:09.960 回答
1

好的,假设你有这个值 : username, location.

然后,(如果我的问题正确)在响应脚本(接收表单操作的脚本)中,试试这个:

<?php

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

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

?>

提示(2 种连接字符串的方法)

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

或者

$filepath = "/UploadedFiles/".$username."/".$location."/files.xml";
于 2012-04-14T15:44:42.390 回答
1

无论您在表单中使用 post 或 get 方法,这都会起作用。只需确保为输入文本 html 元素选择字段名称用户名和位置。

$filepath = "/UploadedFiles/{$_REQUEST['username']}/{$_REQUEST['location']}/files.xml"
于 2012-04-14T15:49:20.173 回答