duanlu4371
2015-11-22 20:30
浏览 127
已采纳

在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

图片转代码服务由CSDN问答提供 功能建议

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

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

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

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • dongmin1166 2015-11-22 20:32
    已采纳

    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

    点赞 评论
  • doufuxing8562 2015-11-22 21:06

    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> 
    
    点赞 评论

相关推荐 更多相似问题