条件语句的错误位置。由于循环一切都被破坏,使用多上传文件
Wrong location of conditional statements. Because of loop everything is destroyed, using multi upload files
每次上传新文件时,此应用程序都会创建一个新文件夹,并将这些文件插入到创建的文件夹中。
下面的代码工作正常,但我有点困惑。由于多次上传,我无法检查是否上传了某些内容
而且我失去了对创建文件夹的控制,因为即使我不上传文件(只提交表单)也会创建它。
为什么只提交表单时会创建文件夹?因为 "UPLOAD_ERR_OK" 在文件夹创建后检查表格(首先是 "mkdir" 和 "UPLOAD_ERR_OK" 之后(但它必须是这样,因为循环 ([$i]!) )).
我想做的是把 UPLOAD_ERR_OK 放在开头,但我不能,因为它必须包含一个数组 [$i]!在单次上传中 "if ($_FILES['img']['error'] == UPLOAD_ERR_OK)" 工作正常,但是
不在 multi 中,因为数组。
下面的脚本有阶段:
- 创建文件夹
- 如果文件没有错误
- 上传文件到文件夹
是错误的,因为即使我们上传空表单也是在开始时创建文件夹,然后检查表单,所以创建文件夹并出错"UPLOAD_ERR_NO_FILE"。但是不可能先检查错误再创建文件夹,因为错误是在循环后检查数组。
if (!file_exists($pre_path)) // if no folder
{
if (mkdir($pre_path, 0777)) // create folder
{
for ($i=0; $i < count($_FILES['img']['name']) $i++) //LOOP
{
if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK) //if no error
{
//MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
else { }//show error
}
}
}
我希望它是这样的:
1. 如果文件没有错误(不是 "UPLOAD_ERR_NO_FILE")
2.创建文件夹
3.上传文件到文件夹
if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)
{
if (mkdir($pre_path, 0777))
{
for ($i=0; $i < count($_FILES['img']['name']) $i++)
{
//MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
}
}
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
else { }//show error
如何把"if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)"放在[$i]数组的开头,勾选errros后创建文件夹?我们不能将 mkdir 放在循环中,因为循环会创建许多文件夹。我只需要1个文件夹,所以它必须在循环之前。 :/ 太疯狂了。
完整代码:
if (isset($_SESSION['admin'], $_POST['upload_images']))
{
$img_tmp_name = $_FILES['img']['tmp_name'];
$img_name = $_FILES['img']['name'];
$img_error = $_FILES['img']['error'];
$img_type = $_FILES['img']['type'];
$img_size = $_FILES['img']['size'];
$image_quantity = count($img_name);
$error_text = array (
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',
9 => 'file couldnt be moved!',
10 => 'file isnt uploaded',
11 => 'za duzy rozmiar pliku',
12 => 'obrazek musi byc w formacie JPEG',
13 => 'nie mozna stworzyc folderu',
14 => 'Query has not started, but folder has created with number: ',
15 => 'Unknown upload error'
);
$conn = lacz_bd();
if ($q = $conn->prepare('SELECT MAX(gallery_id) FROM galleries'))
{
$q->execute();
$q->bind_result($gallery_id);
$q->store_result();
while($q->fetch())
{
$improved_gallery_id = $gallery_id + 1;
}
$pre_path = ('./galerie/'.$improved_gallery_id);
//------------------------------- START UPLOAD ----------------------------------
if (!file_exists($pre_path))
{
if (mkdir($pre_path, 0777))
{
for ($i=0; $i < $image_quantity; $i++)
{
if ($img_error[$i] == UPLOAD_ERR_OK)
{
if ($img_type[$i] = 'image/jpeg') //to do: preg match instead
{
if ($img_size[$i] <= 2621440) //2,5MiB
{
if (is_uploaded_file($img_tmp_name[$i]))
{
$path = $pre_path.'/'.basename($img_name[$i]);
if (move_uploaded_file($img_tmp_name[$i], $path))
{
if ($q = $conn->prepare('INSERT INTO galleries (path, gallery_id) VALUES (?, ?)'))
{
$q->bind_param('si', $path, $improved_gallery_id);
$q->execute();
if ($q->affected_rows > 0)
{
image_compression($path, 800, 536);
echo '<a href="ustaw_miniature.php?m='.urlencode($img_name[$i]).'&gid='.urlencode($improved_gallery_id).'">';
echo '<img src="'. htmlspecialchars($path, ENT_QUOTES).'" width="80%" height="80%"></img></a><br />';
}
}
} else { echo $error_text[9]; }
} else { echo $error_text[10]; }
} else { echo $error_text[11]; }
} else { echo $error_text[12]; }
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { echo $error_text[1]; }
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) { echo $error_text[2]; }
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { echo $error_text[3]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) { echo $error_text[4]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { echo $error_text[6]; }
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { echo $error_text[7]; }
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { echo $error_text[8]; }
else { echo $error_text[15]; }
} // END FOR
}
} else { echo $error_text[14].' '.htmlspecialchars($improved_gallery_id, ENT_QUOTES);}
$q->free_result();
$q->close();
}
$conn->close();
}
我坐在电脑前等待帮助。谢谢。这对我很重要,因为我为开发人员制作了一个网站。
您可以使用函数重新排列代码。你的代码真的很难理解。
首先你可以上传文件到临时文件夹,然后调用函数检查它,如果一切正常,你可以调用一个函数来准备文件夹(如果不存在则创建)然后将文件从临时文件夹复制到新文件夹。
您可以重新组织条件,使其仅在第一个循环 $i == 0
上检查和创建文件夹。您将需要检查并可能更改以下示例中的错误消息 $error_text[14]
一种更改条件的方法。
if (isset($_SESSION['admin'], $_POST['upload_images']))
{
$img_tmp_name = $_FILES['img']['tmp_name'];
$img_name = $_FILES['img']['name'];
$img_error = $_FILES['img']['error'];
$img_type = $_FILES['img']['type'];
$img_size = $_FILES['img']['size'];
$image_quantity = count($img_name);
$error_text = array (
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',
9 => 'file couldnt be moved!',
10 => 'file isnt uploaded',
11 => 'za duzy rozmiar pliku',
12 => 'obrazek musi byc w formacie JPEG',
13 => 'nie mozna stworzyc folderu',
14 => 'Query has not started, but folder has created with number: ',
15 => 'Unknown upload error'
);
$conn = lacz_bd();
if ($q = $conn->prepare('SELECT MAX(gallery_id) FROM galleries'))
{
$q->execute();
$q->bind_result($gallery_id);
$q->store_result();
while($q->fetch())
{
$improved_gallery_id = $gallery_id + 1;
}
$pre_path = ('./galerie/'.$improved_gallery_id);
//------------------------------- START UPLOAD ----------------------------------
for ($i=0; $i < $image_quantity; $i++)
{
if ($img_error[$i] == UPLOAD_ERR_OK)
{
if ($img_type[$i] = 'image/jpeg') //to do: preg match instead
{
if ($img_size[$i] <= 2621440) //2,5MiB
{
if (is_uploaded_file($img_tmp_name[$i]))
{
if ($i == 0) // only the first loop create directory
{
if (!file_exists($pre_path) && !is_dir($pre_path))
{
if (mkdir($pre_path, 0777))
{
// todo: handle the error
exit();
}
}
}
if (is_dir($pre_path))
{
$path = $pre_path.'/'.basename($img_name[$i]);
if (move_uploaded_file($img_tmp_name[$i], $path))
{
if ($q = $conn->prepare('INSERT INTO galleries (path, gallery_id) VALUES (?, ?)'))
{
$q->bind_param('si', $path, $improved_gallery_id);
$q->execute();
if ($q->affected_rows > 0)
{
image_compression($path, 800, 536);
echo '<a href="ustaw_miniature.php?m='.urlencode($img_name[$i]).'&gid='.urlencode($improved_gallery_id).'">';
echo '<img src="'. htmlspecialchars($path, ENT_QUOTES).'" width="80%" height="80%"></img></a><br />';
} // end affected_rows
} // end prepare
} // end move_uploaded_file
else { echo $error_text[9]; }
} // end is_dir
else { echo $error_text[14].' '.htmlspecialchars($improved_gallery_id, ENT_QUOTES);}
} // end is_uploaded_file
else { echo $error_text[10]; }
} // end image size
else { echo $error_text[11]; }
} // end image type
else { echo $error_text[12]; }
} // end error check
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { echo $error_text[1]; }
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) { echo $error_text[2]; }
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { echo $error_text[3]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) { echo $error_text[4]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { echo $error_text[6]; }
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { echo $error_text[7]; }
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { echo $error_text[8]; }
else { echo $error_text[15]; }
} // end for loop
$q->free_result();
$q->close();
} // end prepare
$conn->close();
} // end isset
每次上传新文件时,此应用程序都会创建一个新文件夹,并将这些文件插入到创建的文件夹中。 下面的代码工作正常,但我有点困惑。由于多次上传,我无法检查是否上传了某些内容 而且我失去了对创建文件夹的控制,因为即使我不上传文件(只提交表单)也会创建它。 为什么只提交表单时会创建文件夹?因为 "UPLOAD_ERR_OK" 在文件夹创建后检查表格(首先是 "mkdir" 和 "UPLOAD_ERR_OK" 之后(但它必须是这样,因为循环 ([$i]!) )). 我想做的是把 UPLOAD_ERR_OK 放在开头,但我不能,因为它必须包含一个数组 [$i]!在单次上传中 "if ($_FILES['img']['error'] == UPLOAD_ERR_OK)" 工作正常,但是 不在 multi 中,因为数组。
下面的脚本有阶段:
- 创建文件夹
- 如果文件没有错误
- 上传文件到文件夹
是错误的,因为即使我们上传空表单也是在开始时创建文件夹,然后检查表单,所以创建文件夹并出错"UPLOAD_ERR_NO_FILE"。但是不可能先检查错误再创建文件夹,因为错误是在循环后检查数组。
if (!file_exists($pre_path)) // if no folder
{
if (mkdir($pre_path, 0777)) // create folder
{
for ($i=0; $i < count($_FILES['img']['name']) $i++) //LOOP
{
if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK) //if no error
{
//MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
else { }//show error
}
}
}
我希望它是这样的: 1. 如果文件没有错误(不是 "UPLOAD_ERR_NO_FILE") 2.创建文件夹 3.上传文件到文件夹
if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)
{
if (mkdir($pre_path, 0777))
{
for ($i=0; $i < count($_FILES['img']['name']) $i++)
{
//MOVE UPLOADED FILES[$i], QUERIES AND FUNCTIONS
}
}
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { }//show error
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) {} //show error
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { } //show error
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { }//show error
else { }//show error
如何把"if ($_FILES['img']['error'][$i] == UPLOAD_ERR_OK)"放在[$i]数组的开头,勾选errros后创建文件夹?我们不能将 mkdir 放在循环中,因为循环会创建许多文件夹。我只需要1个文件夹,所以它必须在循环之前。 :/ 太疯狂了。 完整代码:
if (isset($_SESSION['admin'], $_POST['upload_images']))
{
$img_tmp_name = $_FILES['img']['tmp_name'];
$img_name = $_FILES['img']['name'];
$img_error = $_FILES['img']['error'];
$img_type = $_FILES['img']['type'];
$img_size = $_FILES['img']['size'];
$image_quantity = count($img_name);
$error_text = array (
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',
9 => 'file couldnt be moved!',
10 => 'file isnt uploaded',
11 => 'za duzy rozmiar pliku',
12 => 'obrazek musi byc w formacie JPEG',
13 => 'nie mozna stworzyc folderu',
14 => 'Query has not started, but folder has created with number: ',
15 => 'Unknown upload error'
);
$conn = lacz_bd();
if ($q = $conn->prepare('SELECT MAX(gallery_id) FROM galleries'))
{
$q->execute();
$q->bind_result($gallery_id);
$q->store_result();
while($q->fetch())
{
$improved_gallery_id = $gallery_id + 1;
}
$pre_path = ('./galerie/'.$improved_gallery_id);
//------------------------------- START UPLOAD ----------------------------------
if (!file_exists($pre_path))
{
if (mkdir($pre_path, 0777))
{
for ($i=0; $i < $image_quantity; $i++)
{
if ($img_error[$i] == UPLOAD_ERR_OK)
{
if ($img_type[$i] = 'image/jpeg') //to do: preg match instead
{
if ($img_size[$i] <= 2621440) //2,5MiB
{
if (is_uploaded_file($img_tmp_name[$i]))
{
$path = $pre_path.'/'.basename($img_name[$i]);
if (move_uploaded_file($img_tmp_name[$i], $path))
{
if ($q = $conn->prepare('INSERT INTO galleries (path, gallery_id) VALUES (?, ?)'))
{
$q->bind_param('si', $path, $improved_gallery_id);
$q->execute();
if ($q->affected_rows > 0)
{
image_compression($path, 800, 536);
echo '<a href="ustaw_miniature.php?m='.urlencode($img_name[$i]).'&gid='.urlencode($improved_gallery_id).'">';
echo '<img src="'. htmlspecialchars($path, ENT_QUOTES).'" width="80%" height="80%"></img></a><br />';
}
}
} else { echo $error_text[9]; }
} else { echo $error_text[10]; }
} else { echo $error_text[11]; }
} else { echo $error_text[12]; }
}
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { echo $error_text[1]; }
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) { echo $error_text[2]; }
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { echo $error_text[3]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) { echo $error_text[4]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { echo $error_text[6]; }
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { echo $error_text[7]; }
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { echo $error_text[8]; }
else { echo $error_text[15]; }
} // END FOR
}
} else { echo $error_text[14].' '.htmlspecialchars($improved_gallery_id, ENT_QUOTES);}
$q->free_result();
$q->close();
}
$conn->close();
}
我坐在电脑前等待帮助。谢谢。这对我很重要,因为我为开发人员制作了一个网站。
您可以使用函数重新排列代码。你的代码真的很难理解。 首先你可以上传文件到临时文件夹,然后调用函数检查它,如果一切正常,你可以调用一个函数来准备文件夹(如果不存在则创建)然后将文件从临时文件夹复制到新文件夹。
您可以重新组织条件,使其仅在第一个循环 $i == 0
上检查和创建文件夹。您将需要检查并可能更改以下示例中的错误消息 $error_text[14]
一种更改条件的方法。
if (isset($_SESSION['admin'], $_POST['upload_images']))
{
$img_tmp_name = $_FILES['img']['tmp_name'];
$img_name = $_FILES['img']['name'];
$img_error = $_FILES['img']['error'];
$img_type = $_FILES['img']['type'];
$img_size = $_FILES['img']['size'];
$image_quantity = count($img_name);
$error_text = array (
1 => 'The uploaded file exceeds the upload_max_filesize directive in php.ini',
2 => 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form',
3 => 'The uploaded file was only partially uploaded',
4 => 'No file was uploaded',
6 => 'Missing a temporary folder',
7 => 'Failed to write file to disk.',
8 => 'A PHP extension stopped the file upload.',
9 => 'file couldnt be moved!',
10 => 'file isnt uploaded',
11 => 'za duzy rozmiar pliku',
12 => 'obrazek musi byc w formacie JPEG',
13 => 'nie mozna stworzyc folderu',
14 => 'Query has not started, but folder has created with number: ',
15 => 'Unknown upload error'
);
$conn = lacz_bd();
if ($q = $conn->prepare('SELECT MAX(gallery_id) FROM galleries'))
{
$q->execute();
$q->bind_result($gallery_id);
$q->store_result();
while($q->fetch())
{
$improved_gallery_id = $gallery_id + 1;
}
$pre_path = ('./galerie/'.$improved_gallery_id);
//------------------------------- START UPLOAD ----------------------------------
for ($i=0; $i < $image_quantity; $i++)
{
if ($img_error[$i] == UPLOAD_ERR_OK)
{
if ($img_type[$i] = 'image/jpeg') //to do: preg match instead
{
if ($img_size[$i] <= 2621440) //2,5MiB
{
if (is_uploaded_file($img_tmp_name[$i]))
{
if ($i == 0) // only the first loop create directory
{
if (!file_exists($pre_path) && !is_dir($pre_path))
{
if (mkdir($pre_path, 0777))
{
// todo: handle the error
exit();
}
}
}
if (is_dir($pre_path))
{
$path = $pre_path.'/'.basename($img_name[$i]);
if (move_uploaded_file($img_tmp_name[$i], $path))
{
if ($q = $conn->prepare('INSERT INTO galleries (path, gallery_id) VALUES (?, ?)'))
{
$q->bind_param('si', $path, $improved_gallery_id);
$q->execute();
if ($q->affected_rows > 0)
{
image_compression($path, 800, 536);
echo '<a href="ustaw_miniature.php?m='.urlencode($img_name[$i]).'&gid='.urlencode($improved_gallery_id).'">';
echo '<img src="'. htmlspecialchars($path, ENT_QUOTES).'" width="80%" height="80%"></img></a><br />';
} // end affected_rows
} // end prepare
} // end move_uploaded_file
else { echo $error_text[9]; }
} // end is_dir
else { echo $error_text[14].' '.htmlspecialchars($improved_gallery_id, ENT_QUOTES);}
} // end is_uploaded_file
else { echo $error_text[10]; }
} // end image size
else { echo $error_text[11]; }
} // end image type
else { echo $error_text[12]; }
} // end error check
elseif ( $img_error[$i] == UPLOAD_ERR_INI_SIZE) { echo $error_text[1]; }
elseif ( $img_error[$i] == UPLOAD_ERR_FORM_SIZE) { echo $error_text[2]; }
elseif ( $img_error[$i] == UPLOAD_ERR_PARTIAL) { echo $error_text[3]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_FILE) { echo $error_text[4]; }
elseif ( $img_error[$i] == UPLOAD_ERR_NO_TMP_DIR) { echo $error_text[6]; }
elseif ( $img_error[$i] == UPLOAD_ERR_CANT_WRITE) { echo $error_text[7]; }
elseif ( $img_error[$i] == UPLOAD_ERR_EXTENSION) { echo $error_text[8]; }
else { echo $error_text[15]; }
} // end for loop
$q->free_result();
$q->close();
} // end prepare
$conn->close();
} // end isset