2

我在将多个文件上传到磁盘时遇到问题。这是我的代码。

我有一个发送到上传功能的 2 张图片的请求。2张图片在一个名为$multiUpload

$folderPath = '/var/www/';
if (is_array($multiUpload)){
            $file = array();
            $filename = array();

            foreach($multiUpload as $key=>$val){
                // get the file extension
                $file[] = explode('.',$val);

                // create custom file name
                $filename[] = time().'.'.$file[$key][1];

                //send to the upload function
                $this->uploadToDisk($folderPath, $filename[$key]);

                // sleep 1 sec so that the pic names will be different
                sleep(1);
            }
                 return $filename;

        }


public function uploadToDisk($folderPath, $filename)
{

    $adapter = new Zend_File_Transfer_Adapter_Http();
    $adapter->setDestination($folderPath);
    $adapter->addFilter( 'Rename',array(
            'target' => $folderPath."/".$filename,
            'overwrite' => true
            ) );
    if ($adapter->receive()) {
        $message = "success";
    } else {
        $message = "fail";
    }

    return $message;
}

这将返回

Array
(
    [0] => Array
        (
            [0] => 1332977938.jpg
            [1] => 1332977939.jpg
        )

)

array[0][0] or 1332977938.jpg实际上只会保存到磁盘。

为什么他们现在都得救了?有线

有任何想法吗?

4

1 回答 1

3

我怀疑第二次调用uploadToDisk正在返回fail,因为您只能Zend_File_Transfer_Adapter_Http::receive()为每个文件调用一次。由于您在调用时没有指定文件receive,因此它会在您第一次调用时接收所有文件uploadToDisk,随后因File Upload Attack错误而失败。

这是您可以尝试的一些代码。这会尝试单独接收每个文件,然后在每次调用uploadToDisk.

关于代码的几点说明:

  • ($val)的第一个参数uploadToDisk可能需要更改,因为我不确定原始值是什么。它应该对应于用于文件上传的元素名称之一(请参阅Zend_File_Transfer_Adapter_Http::getFileInfo()文件列表)。
  • 我更改了生成唯一文件名的方法,因此您不必sleep(1)
  • Zend_File_Transfer_Adapter_Abstract::setDestination()已弃用并将在未来消失。相反,只需使用Rename过滤器。使用时RenamesetDestination()没有效果。

在这里...

<?php

$folderPath = '/var/www/';

if (is_array($multiUpload)){
    $filenames = array();

    foreach($multiUpload as $key => $val){
        // get the file extension
        $ext = explode('.', $val);
        $ext = $ext[sizeof($ext) - 1];

        // create custom file name
        do {
            $filename = uniqid(time()) . '.' . $ext;
            $diskPath = $folderPath . $filename;
        } while (file_exists($diskPath));

        $filenames[$key] = $filename;

        //send to the upload function
        // $val is the file to receive, $diskPath is where it will be moved to
        $this->uploadToDisk($val, $diskPath);
    }

    return $filename;
}


public function uploadToDisk($file, $filename)
{
    // create the transfer adapter
    // note that setDestination is deprecated, instead use the Rename filter
    $adapter = new Zend_File_Transfer_Adapter_Http();
    $adapter->addFilter('Rename', array(
            'target'    => $filename,
            'overwrite' => true
    ));

    // try to receive one file
    if ($adapter->receive($file)) {
        $message = "success";
    } else {
        $message = "fail";
    }

    return $message;
}
于 2012-03-29T00:39:13.397 回答