【问题标题】:Zip not uploading :- (Zip 未上传 :- (
【发布时间】:2011-07-18 19:38:22
【问题描述】:

由于某种原因,在尝试上传 zip 文件时,此函数总是返回 false。目录都设置为 0777 以获得权​​限。我不知道可能出了什么问题。

function uploadProof ( $file, $email )
{
    // Check or create for existing directory
    if ( !is_dir('client_files/'.$email))
    {
        mkdir('client_files/'.$email);
        if ( !is_dir('client_files/'.$email.'/proof/'))
        {
            mkdir('client_files/'.$email.'/proof/');
        }
    }
    // Target path
    $target_path = 'client_files/'.$email.'/proof/';

    // File information
    $filename = date('Y_M_D').$email.'.zip';
    $tmp_name = $file['tmp_name'];
    $filesize = $file['size'];

    // Blacklist and Max file info
    $max_allowed = (1024 * 1024) * 99; // 99 MB
    $blacklist = array(
        '.pl', '.php', '.phtml', '.php3', '.php4', '.php5'
    );

    // Check filename
    foreach ( $blacklist as $nope)
    {
        if ( preg_match("/$nope\$/i", $filename))
        {
            die("As previously stated, we do not allow php files of any type\n
                to be uploaded to our server.\n\n");
        }
    }

    // Check filesize
    if ( $filesize > $max_allowed)
    {
        die("File is too big, file needs to be less than <em>20MB</em> in size.");
    }
    else
    {
        $target = $target_path.$filename;

        if (move_uploaded_file($tmp_name, $target))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}

【问题讨论】:

  • 请张贴 print_r( $_FILES );
  • Array ( [name] => BLAHBLAHfilename.zip [type] => application/zip [tmp_name] => /Applications/MAMP/tmp/php/phpzDwFiL [error] => 0 [size] => 1358091 )

标签: php upload zip


【解决方案1】:

在继续执行可能完全无用的操作之前,您确实需要检查上传是否真的成功:

function uploadProof ( $file, $email ) {
   if ($file['error'] !== UPLOAD_ERR_OK) {
       die("Upload failed with error code " . $file['error']);
   }
   ...
}

错误代码定义为here

【讨论】:

  • 仅供参考:如果您传入递归标志,mkdir() 可以在一次调用中创建多个级别的目录:mkdir('/some/multi/level/dir', 0777, true);
猜你喜欢
  • 2014-04-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-13
  • 2018-10-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多