在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);

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

我在控制器中进行多个文件上传但文件没有上传 控制器代码:用于上传 p>

  $ images = $ _FILES ['evidence']; 
 $ success = null; 
 
 $ paths = ['uploads']; 
 
 / n  / get file names 
 $ filenames = $ images ['name']; 
 
 
 //循环和处理文件
for($ i = 0; $ i&lt; 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; 
} 
 //基于成功状态检查和处理
if($ success === true){
 $ evidence = new 证据(); 
 $ evidence-&gt; case_ref = $ id; 
 $ evidence-&gt; saved_on = date(“Ymd”); 
 $ evidence-&gt; save(); 
 
 $ output =  []; 
} elseif($ success === false){
 $ output = ['error'=&gt;'上传图片时出错。 联系系统管理员']; 
 
foreach($ paths as $ file){
 unlink($ file); 
} 
} else {
 $ output = ['error'=&gt;'没有文件 经过处理。']; 
} 
 
 //返回一个json编码的响应,插件成功处理
echo json_encode($ output); 
  code>  pre> 
 
 

我试过var_dump($ images ['name'],一切似乎没关系,移动文件没有上传文件 p> div>

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

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?

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

Have you added enctype attribute to form tag?

For example:

 <form action="demo_post_enctype.asp" method="post" enctype="multipart/form-data">
  First name: <input type="text" name="fname"><br>
  Last name: <input type="text" name="lname"><br>
  <input type="submit" value="Submit">
</form>