简体   繁体   English

创建文件夹并在不存在时插入

[英]Create Folder and Insert if Not Present

I wonder whether someone may be able to help me please.我想知道是否有人可以帮助我。

I'm using Aurigma's Image uploader software to allow users to upload images for locations they visit.我正在使用 Aurigma 的图像上传器软件来允许用户上传他们访问过的位置的图像。 The information is saved via the script shown below:信息通过如下所示的脚本保存:

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

In additon to the original script I've added code that creates a folder, with it's name based on the current 'locationid'.除了原始脚本之外,我还添加了创建文件夹的代码,其名称基于当前的“locationid”。 This is shown below.如下所示。

  $locationfolder = $_POST['locationid'];
  $locationfolder = preg_replace('/[^a-z0-9_\-\.()\[\]{}]/i', '_', $locationfolder);
  if (!is_dir($absGalleryPath . $locationfolder)) {
    mkdir($absGalleryPath . $locationfolder, 0777);
  }

What I like to incorporate, is a check that looks to see whether there is a folder already setup with the current 'locationid' value, if not create the folder.我想合并的是一项检查,查看是否已使用当前“locationid”值设置了一个文件夹,如果没有则创建该文件夹。 I'm ceratianly no expert in PHP, but I know that to check to see whether a file exists, the if(file exists....) can be used, but I just wondered whether someone could tell me please how I can implement this check for the folder name?我确实不是 PHP 的专家,但我知道要检查文件是否存在,可以使用if(file exists....) ,但我只是想知道是否有人可以告诉我如何实现这检查文件夹名称?

Many thanks非常感谢

Chris克里斯

I think is_dir() is what you are looking for.我认为is_dir()是你要找的。

UPDATE:更新:

The code you have:您拥有的代码:

if (!is_dir($absGalleryPath . $locationfolder)) {
   mkdir($absGalleryPath . $locationfolder, 0777);
}

Does exactly what you want.做你想要的。 It checks for the folder and if it does not exist then it creates one for you (with CHMOD 777).它会检查该文件夹,如果它不存在,则会为您创建一个(使用 CHMOD 777)。 Don't see what your question is then...看不出你的问题是什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM