php 多文件上传

68bkxrlz  于 2023-01-08  发布在  PHP
关注(0)|答案(2)|浏览(137)

我从文件上传中得到一个错误,我只有一个文件输入,我得到错误
发生错误。请与管理员联系。
我的代码:

//html your text <input type="file" name="the_file[]" multiple >

  //php $currentDirectory = getcwd();
   $uploadDirectory = "/uploads/";
   $fileExtensionsAllowed = ['image/png, image/jpeg']; // These will be 
   the only file extensions allowed 
   $fileName = $_FILES['the_file[]']['name'];
   $fileSize = $_FILES['the_file[]']['size'];
   $fileTmpName  = $_FILES['the_file[]']['tmp_name[]'];
   $fileName = $prenume . $nume  . $fileName . ".png";
   $fileExtension = strtolower(end(explode('.',$fileName)));
   $uploadPath = $currentDirectory . $uploadDirectory . $fileName; 
   if(isset($_POST['submit'])){
    //upload fisiere 

   if ( in_array($fileExtension,$fileExtensionsAllowed)) {
    $errors[] = "This file extension is not allowed. Please upload a JPEG or PNG file";
  }

  if ($fileSize > 400000000000) {
    $errors[] = "File exceeds maximum size (4MB)";
  }

  if (empty($errors)) {

    $didUpload = move_uploaded_file($fileTmpName, $uploadPath);
    if ($didUpload) {
      echo "Upload-ul fisierului  " . $fileName . " s-a efectuat cu succes <br>";
    } else {
      echo "An error occurred. Please contact the administrator.";
    }
  } else {
    foreach ($errors as $error) {
      echo $error . "These are the errors" . "\n";
    }
  }

我期望得到多个文件上传,如果我上传一个文件它的工作,它可能是因为名称或某事

h79rfbju

h79rfbju1#

显然问题出在这句台词上:第一个月
它应该是:$fileTmpName = $_FILES['the_file']['tmp_name'];
还应将文件名更新为:$fileName = $prenume . $nume . $fileName;
最后,您应该处理用户选择多个文件进行上传的情况,您可以通过循环$_FILES数组来完成此操作:

foreach ($_FILES['the_file']['name'] as $i => $name) {
  if (strlen($_FILES['the_file']['name'][$i]) > 1) {
      if (move_uploaded_file($_FILES['the_file']['tmp_name'][$i], $uploadDirectory.$name)) {
          echo 'File was uploaded successfully';
      }
  }
}
kq0g1dla

kq0g1dla2#

最终代码:

$currentDirectory = getcwd();
       $uploadDirectory = "/uploads/";

       $errors = []; // Store errors here
           $fileExtensionsAllowed = ['image/png, image/jpeg']; // These will be the                                  only file extensions allowed 
       $fileName = $_FILES['the_file']['name'];
       $fileSize = $_FILES['the_file']['size'];
       $fileTmpName  = $_FILES['the_file']['tmp_name'];
       $fileExtension = strtolower(end(explode('.',$fileName)));
       $fileName = $prenume . $nume . $fileName;

       $uploadPath = $currentDirectory . $uploadDirectory . $fileName; 

       //

       if ( in_array($fileExtension,$fileExtensionsAllowed)) {
         $errors[] = "This file extension is not allowed. Please upload a JPEG                                  or PNG file";
       }

       if ($fileSize > 400000000000) {
         $errors[] = "File exceeds maximum size (4MB)";
       }
       foreach ($_FILES['the_file']['name'] as $i => $name) {
         if (strlen($_FILES['the_file']['name'][$i]) > 1) {
             if (move_uploaded_file($_FILES['the_file']['tmp_name'][$i],            $uploadPath. $name .$filename)) {
                 echo 'File was uploaded successfully';
             }
         }
       }

相关问题