我曾尝试阅读多个教程,PHP文档并且不知道我在做什么。
这是我的表格
<form action="beta_upload.php" enctype="multipart/form-data" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="20971520" /><!-- 20 Meg -->
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="file" name="file[]" />
<input type="submit" value="submit" name="submit" />
</form>
现在我通过这里发送信息:
<?php
$username = trim($_POST['username']);
$password = trim($_POST['password']);
$name = trim($_POST['name']);
$email = trim($_POST['email']);
$username = preg_replace('/[^(\x20-\x7F)]*/','', $username);
$password = preg_replace('/[^(\x20-\x7F)]*/','', $password);
$name = preg_replace('/[^(\x20-\x7F)]*/','', $name);
$email = preg_replace('/[^(\x20-\x7F)]*/','', $email);
$upload_dir = '/beta_images/';
print_r($_FILES);
foreach ($_FILES['files']['error'] as $key => $error) {
if($error == UPLOAD_ERR_OK) {
$check_name = $_FILES['files']['name'];
$filetype = checkfiletype($check_name, 'jpg,jpeg');
$temp_name = $_FILES['files']['tmp_name'][$key];
$image_name = 'image_' . $name . '1';
move_uploaded_file($tmp_name, $upload_dir . $image_name);
}
}
它带回来
Warning: Invalid argument supplied for foreach() in /blabla on line 18
我不太了解foreachs,当我print_r
数组时它不会帮助我一点。
有人会非常友好地帮助我。
感谢。
答案 0 :(得分:4)
您最好遵循本教程:http://www.w3schools.com/php/php_file_upload.asp
foreach ($_FILES['file'] as $file) {
if($file['error'] == UPLOAD_ERR_OK) {
$check_name = $file['name'];
// I assume you have to use the file type here, not name
$filetype = checkfiletype($file['type'], 'jpg,jpeg');
$temp_name = $file['tmp_name'];
$image_name = 'image_' . $file['name'] . '1';
move_uploaded_file($tmp_name, $upload_dir . $image_name);
}
}
您的文件位于$ _FILES ['files']中,因此使用foreach时您必须遍历每个元素/文件并获取其数据。