我对用PHP上传文件感到很困惑 [英] I am so confused with uploading files in PHP
本文介绍了我对用PHP上传文件感到很困惑的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试阅读多个教程和PHP文档,却不知道我在做什么.
I have tried reading multiple tutorials, the PHP documentation and have no idea what I am doing.
这是我的表格
<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>
现在,当我通过此处发送该邮件时:
Now when I send that through here:
<?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
我不太了解foreach,当我print_r
数组时,它一点也帮不了我.
I don't understand foreachs all too well, and when I print_r
the array it dosen't help me one bit.
有人会很乐于帮助我吗?
Would someone be so kind to help me out.
谢谢.
推荐答案
您最好遵循本教程:您的文件位于$ _FILES ['files']中,因此使用foreach时,您必须遍历每个元素/文件并获取其数据.
Your files are in $_FILES['files'] so using foreach you have to go over each element/file and take its data.
这篇关于我对用PHP上传文件感到很困惑的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文