简体   繁体   English

如何检查symfony2中是否已经存在某个文件?

[英]How to check if a certain file already exists in symfony2?

I am working on symfony2 and currently I want to check if a file does exist. 我正在symfony2工作,目前我想检查文件是否确实存在。 My scenario look something like this,, I have two files excel file and the docx file. 我的情况如下所示,我有两个文件excel文件和docx文件。 If a person have an excel file for her/his transaction then it is automatically that the docx file is not applicable for her or him.And vice versa. 如果某人拥有一个用于他/他的交易的excel文件,那么docx文件自动不适用于他/他,反之亦然。 How can I possibly do it in symfony2 ? 我怎么可能在symfony2symfony2呢? A lot in the net but I don't know how to do it in my case,, thanks a lot :) 网上有很多东西,但是我不知道该怎么做,非常感谢:)

UPDATE Controller: UPDATE控制器:

public function documentType ($doc_type) {
   $em = $this->getDoctrine()->getEntityManager();
   $result = $em->getRepository('SupplierBundle:SupplierTransactionDetails')->findDocumentType('xlsx');
   $result1 = $em->getRepository('SupplierBundle:SupplierTransactionDetails')->findDocumentType('docx');
   // statement that will know if the file exists for that certain user if so the other document type will be not applicable for that user. Example: that user have docx file already then the xlsx will be N/A and vice versa

} 

Repository : 仓库:

public function findDocumentType ($doc_type) {
   $em = $this->getEntityManager();
   $query = $em->createQuery(
       'SELECT a, b, c FROM SupplierBundle:SupplierTransaction Details a
        JOIN a.supplierTransaction b
        JOIN b.supplierDocType c
        WHERE c.docType LIKE :doc_type'
   )->setParameter('doc_type', $doc_type);

   return $query->getResult();
}

You can just use plain PHP for this. 您可以为此使用纯PHP。 For example: 例如:

$xls = 'sheet.xlsx';
$doc = 'document.docx';

if (file_exists($xls)) {
    // User has an XLSX file
    echo 'Download your sheet <a href="sheet.xlsx">here</a>.';
} elseif (file_exists($doc)) {
    // User has a DOCX file
    echo 'Download your document <a href="document.docx">here</a>.';
}

If you want to do it the Symfony2 way, you can use the Filesystem class, it has an exists() function. 如果您想以Symfony2的方式进行操作,则可以使用Filesystem类,它具有一个exists()函数。 In a controller an instance is automatically available in the service container: 在控制器中,实例将在服务容器中自动可用:

$em = $this->getDoctrine()->getManager();
$xlsx = $em->getRepository('SupplierBundle:SupplierTransactionDetail')->findDocumentType‌('xlsx');
$docx = $em->getRepository('SupplierBundle:SupplierTransactionDetail')->findDocumnetType‌('docx');

return [
        'xlsx' => $xlsx instanceof SupplierTransactionDetail && $this->get('filesystem')->exists($xlsx->getFile()) ? $xlsx->getFile() : null,
        'docx' => $docx instanceof SupplierTransactionDetail && $this->get('filesystem')->exists($docx->getFile()) ? $docx->getFile() : null,
    ];

In your twig template: 在您的树枝模板中:

{% if xlsx is not empty %}
    {{ xlsx }}
{% endif %}

{% if docx is not empty %}
    {{ docx }}
{% endif %}

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

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