在PHP中插入文件夹和文件


Insert Folders & Files In PHP

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

使用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();
?>

除了原始脚本之外,此代码还创建了一个"位置"文件夹,其名称派生自"locationid"值。

我现在遇到了一个问题,我似乎找不到解决方案。

当用户添加图像时,将创建原始图像、名为"files.xml"的文件和一个名为"缩略图"的文件夹,其中包含图像的缩略图版本,并将其保存在名为"UploadedFiles"的文件夹中。

我正在尝试做的是将上述文件移动到我的"位置"文件夹中,因此结构将是:

上传的文件(文件夹)

  • 位置(子文件夹),包含原始图像、"文件.xml"、包含缩略图的缩略图文件夹

我当然不是PHP专家,但我尝试遵循现有代码创建的格式:

$locationpath = $locationfolder
$abslocationpath = realpath($locationpath) . DIRECTORY_SEPARATOR;

然后,我尝试将要移动到$abslocationpath的各种文件和文件夹指向,但这不起作用。我确信答案在于创建一个新目录,但我对如何做到这一点感到茫然。

只是想知道是否有人可以提供一些指导,说明我如何创建相关目录并将文件移动到我要创建的新文件夹结构。

非常感谢

您的代码看起来不错,但是,我没有看到您在哪里进行任何实际将任何内容移动到位置路径的调用。

我稍微修改了你的代码。请先浏览一下,注意注释的行并做出相应的调整,让我知道它是否产生了预期的结果:

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"];
  $locationfolder = preg_replace('/[^a-z0-9_'-'.()'[']{}]/i', '_', $_POST['locationid']);
  $dirName = preg_replace('/[^a-z0-9_'-'.()'[']{}]/i', '_', $_POST['folder']);
  $absGalleryPath = realpath($galleryPath) . DIRECTORY_SEPARATOR . $locationfolder . 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);
  $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 = new DOMDocument('1.0', 'utf-8');
  $descriptions->load($absGalleryPath . 'files.xml');
  $descriptions->documentElement->appendChild($xmlFile);
  $descriptions->save($absGalleryPath . 'files.xml');
}
$uh = new UploadHandler();
$uh->setFileUploadedCallback('onFileUploaded');
$uh->processRequest();