Insert Folders & Files In PHP
<p>I wonder whether someone may be able to help me please.</p>

<p>Using Aurigma image Uploader I've put together the code below which allows users to upload location based images.</p>

<pre><code>&lt;?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-&gt;getPackage()-&gt;getPackageFields();
  $username=$packageFields["username"];
  $locationid=$packageFields["locationid"];

  global $galleryPath;

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

  if ($uploadedFile-&gt;getPackage()-&gt;getPackageIndex() == 0 &amp;&amp; $uploadedFile-&gt;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-&gt;getSourceName();

  $files = $uploadedFile-&gt;getConvertedFiles();

  // save converter 1

  $sourceFileName = getSafeFileName($absGalleryPath, $originalFileName);
  $sourceFile = $files[0];
  /* @var $sourceFile ConvertedFile */
  if ($sourceFile) {
    $sourceFile-&gt;moveTo($absGalleryPath . $sourceFileName);
  }

  // save converter 2   

  $thumbnailFileName = getSafeFileName($absThumbnailsPath, $originalFileName);
  $thumbnailFile = $files[1];
  /* @var $thumbnailFile ConvertedFile */
  if ($thumbnailFile) {
    $thumbnailFile-&gt;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-&gt;load($absGalleryPath . 'files.xml');

  //Save file info.
  $xmlFile = $descriptions-&gt;createElement('file');
  $xmlFile-&gt;setAttribute('name', $_POST['folder'] . '/' . $originalFileName);
  $xmlFile-&gt;setAttribute('source', $sourceFileName);
  $xmlFile-&gt;setAttribute('size', $uploadedFile-&gt;getSourceSize());
  $xmlFile-&gt;setAttribute('originalname', $originalFileName);
  $xmlFile-&gt;setAttribute('thumbnail', $thumbnailFileName);
  $xmlFile-&gt;setAttribute('description', $uploadedFile-&gt;getDescription());
  //Add additional fields
  $xmlFile-&gt;setAttribute('username', $username);
  $xmlFile-&gt;setAttribute('locationid', $locationid);
  $xmlFile-&gt;setAttribute('folder', $dirName);
  $descriptions-&gt;documentElement-&gt;appendChild($xmlFile);
  $descriptions-&gt;save($absGalleryPath . 'files.xml');
}

$uh = new UploadHandler();
$uh-&gt;setFileUploadedCallback('onFileUploaded');
$uh-&gt;processRequest();
?&gt;
</code></pre>

<p>This code, in addition to the original script creates a 'location' folder with it's name derived from the 'locationid' value.</p>

<p>I've now come across a problem which I just can't seem to find the solution to.</p>

<p>When the user adds an image, the original image, a file entitled 'files.xml' and a folder called Thumbnails containing a thumbnail version of the image are created and saved in a folder called 'UploadedFiles'. </p>

<p>What I'm trying to do is to move the afore mentioned files into my 'location' folder, so the structure would be:</p>

<p><strong>Uploaded Files (Folder)</strong></p>

<ul>
<li><strong>Location (subfolder)</strong>, containing original image, 'files.xml', Thumbnails folder containing and thumbnail image</li>
</ul>

<p>I'm certainly not an expert in PHP, but I've tried to follow the format of the existing code creating:  </p>

<pre><code>$locationpath = $locationfolder
$abslocationpath = realpath($locationpath) . DIRECTORY_SEPARATOR;
</code></pre>

<p>I then tried to point the various files and folders I want to move to <code>$abslocationpath</code> but this doesn't work. I'm sure that the answer lies within creating a new directory, but I'm at a loss about how to do this.</p>

<p>I just wondered whether someone could perhaps provide some guidance please on how I can create the relevant directory and move the files to the new folder structure I want to create.</p>

<p>Many thanks</p>
