PHP创建父/子文件夹时遇数组转字符串错误求助
Hey there! Let's break down what's causing that error and fix it step by step.
What's the Issue?
The Array to string conversion error pops up because your $check[$x] value is either an array itself, or the $check variable wasn't properly defined in the first place. Looking at your code, you never pull the check data from the form submission—this is almost certainly the root problem.
Step-by-Step Fixes
1. Properly Retrieve Checkbox Data from the Form
First, make sure your HTML form's checkboxes have a name attribute set to check[] (the square brackets tell PHP to treat the input as an array). Then, in your PHP code, safely fetch this data to avoid undefined variable warnings:
// Get checkbox values from POST, default to empty array if not set $check = isset($_POST['check']) ? $_POST['check'] : [];
2. Use Dynamic Loop Logic (Instead of Hardcoding)
You hardcoded $checked_count1=5, which will cause issues if fewer (or more) checkboxes are selected. Instead, use the actual count of selected checkboxes:
$checked_count1 = count($check);
Even cleaner: use a foreach loop to iterate directly over the $check array—it eliminates off-by-one errors and makes the code easier to read:
foreach ($check as $subFolderName) { // Folder creation logic here }
3. Fix the Folder Path for Reliability
Your current subfolder path uses a relative path, which can lead to unexpected behavior depending on where your script runs. Instead, build the subfolder path using the absolute parent folder path you already defined:
$sub = $structure . DIRECTORY_SEPARATOR . $subFolderName;
This ensures the subfolder is always created inside the parent folder you made earlier.
4. Full Corrected Code
Here's the complete, fixed version of your code with all these changes, plus safer permissions and basic sanitization:
// Create parent folder $foldername = isset($_POST['foldername']) ? trim($_POST['foldername']) : ''; if (empty($foldername)) { die('Please enter a parent folder name.'); } $structure = __DIR__ . DIRECTORY_SEPARATOR . $foldername; // Use 0755 for production (safer than 0777, which gives full access to everyone) if (!mkdir($structure, 0755, true)) { die('Failed to create parent folder...'); } // Get checkbox values for subfolders $check = isset($_POST['check']) ? $_POST['check'] : []; // Create subfolders foreach ($check as $subFolderName) { // Sanitize folder name to avoid empty/invalid entries $subFolderName = trim($subFolderName); if (empty($subFolderName)) { continue; // Skip empty subfolder names } $sub = $structure . DIRECTORY_SEPARATOR . $subFolderName; if (!file_exists($sub)) { if (!mkdir($sub, 0755)) { echo "Failed to create subfolder: $subFolderName<br>"; } } }
Extra Notes
- Permissions:
0777is convenient for testing but a security risk in production. Stick with0755(read/write/execute for the owner, read/execute for others) instead. - Sanitization: The
trim()removes extra spaces, but you might want to add more checks to block invalid characters (like/,\,:) depending on your operating system.
内容的提问来源于stack exchange,提问作者Mrudul Bose

