yii2 move_upload 函数上传文件出错 [英] Error in uploading files in yii2 move_upload function

查看:24
本文介绍了yii2 move_upload 函数上传文件出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在控制器中上传多个文件,但文件没有上传控制器代码:用于上传

Am doing multiple file upload in the controller but the file doesn't get uploaded controller code: for the upload

$images = $_FILES['evidence'];
$success = null;

$paths= ['uploads'];

// get file names
$filenames = $images['name'];


// loop and process files
for($i=0; $i < count($filenames); $i++){
//$ext = explode('.', basename($filenames[$i]));
$target = "uploads/cases/evidence".DIRECTORY_SEPARATOR . md5(uniqid()); //. "." . array_pop($ext);
if(move_uploaded_file($images['name'], $target)) {
    $success = true;
    $paths[] = $target;
} else {
    $success = false;

    break;
}

echo $success;
}
// check and process based on successful status 
if ($success === true) {
        $evidence = new Evidence();
        $evidence->case_ref=$id;
        $evidence->saved_on=date("Y-m-d");
        $evidence->save();

$output = [];
} elseif ($success === false) {
$output = ['error'=>'Error while uploading images. Contact the system administrator'];

foreach ($paths as $file) {
    unlink($file);
}
} else {
$output = ['error'=>'No files were processed.'];
}

//  return a json encoded response for plugin to process successfully
echo json_encode($output);

我已经尝试过 var_dump($images['name'] 并且一切似乎没问题移动文件没有上传文件

I have tried var_dump($images['name'] and everything seems okay the move file does not upload the file

推荐答案

检查您在 $_FILES 和 $_POST 中获得的内容,并根据这些结果评估您的逻辑...

Check what you obtain in $_FILES and in $_POST and evaluate your logic by these result...

PHP 手册说这个函数在检查文件名时返回 false 以确保文件名指定的文件不是有效的文件名,或者由于某种原因可以移动文件.. 你确定生成的文件名是有效的吗?/或者可以移动到目的地?

The PHP manual say this function return false when the filename is checked to ensure that the file designated by filename and is not a valid filename or the file can be moved for some reason.. Are you sure the filename generated is valid and/or can be mooved to destination?

这是相关的php man php.net/manual/en/function.move-uploaded-file.php

this is the related php man php.net/manual/en/function.move-uploaded-file.php

这篇关于yii2 move_upload 函数上传文件出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆