php - Getting Array to string conversion error at the line : $sub= $foldername.'/'.$check[$x];. Please advice

386

I am new to PHP. Here i am trying to create Parent Folder and Sub folder based on the input from a form. I am Getting Array to string conversion error at the line : $sub= $foldername.'/'.$check[$x];. Please advice.

//Creating the parent and sub folder
$checked_count1=5;
$foldername = $_POST['foldername'];
       $structure = dirname(__FILE__).DIRECTORY_SEPARATOR.$foldername;

      if (!mkdir($structure, 0777, true)) {
        die('Failed to create folders...');
      }
   //Subfolder
   for($x=0;$x<$checked_count1;$x++)
   {
   $sub= $foldername.'/'.$check[$x];
   if(!file_exists($sub)){
   mkdir($sub, 0777);
   }
   }
619

Answer

Solution:

Even though $check is not set in the given code, php prpbably thinks the value of $check[$x] is an array. Also you're concatenating this array to a string, hence the array to string conversion error. Only strings can be concatenated. When possible, php converts other type values to a string first. This is not possible for an array.

People are also looking for solutions to the problem: php - how to get nested category parents and siblings of parents?

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.