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,但这些都没有奏效,所以我不太确定我需要更改什么。

我只是想知道是否有人可以看看这个,让我知道我哪里出错了。

非常感谢

4

1 回答 1

0

'$username.xml' 将被解释为 $username.xml,你需要使用 "$username.xml"。单引号“禁用”在字符串中使用的变量。

您正在尝试的可能是一个坏主意,因为您正在制作用户名,因此用户名不能包含“/”之类的“特殊字符”。如果您已经有一条规则阻止“/”成为用户名的一部分,也许这不是问题。

于 2012-03-29T15:36:10.887 回答