问题描述
请宽容我,这是我第一次,显然我不知道我在做什么...
我使用下面的脚本,我发现在网上上传文件并写入上传信息到数据库中,我想收集$ FILE_NAME信息到另一个数组这个code座外被用于在爆功能。我已经搜查高和低,但在我的搜索没有成功。我想通了如何从if语句里面打印$ FILE_NAME(S),而不是外界的它和PHP块,但我无法弄清楚如何使之成为一个数组来代替。
任何帮助将大大AP preciated。
< PHP
如果(使用isset($ _ FILES ['文件'])){
$错误=阵列();
的foreach($ _ FILES ['文件'] ['tmp_name的值']为$关键=> $ tmp_name的值){
$ FILE_NAME = $关键$ _ FILES ['文件'] ['名'] [$关键]。
$ FILE_SIZE = $ _ FILES ['文件'] ['大小'] [$关键];
$ file_tmp = $ _ FILES ['文件'] ['tmp_name的值'] [$关键];
$ FILE_TYPE = $ _ FILES [文件] [型] [$关键];
$时间戳=日期('Y-M-D G:我:S');
如果($ FILE_SIZE> 2097152){
$错误[] ='文件大小必须小于2 MB';
}
$查询=INSERT INTO upload_data(FILE_NAME,FILE_SIZE,FILE_TYPE,时间戳)VALUES(:FILE_NAME,:FILE_SIZE,:FILE_TYPE,:时间戳);
$ desired_dir =文件;
如果(空($错误)==真){
如果(is_dir($ desired_dir)== FALSE){
MKDIR($ desired_dir,0700); //创建目录,如果它不存在
}
如果(is_dir($ desired_dir /\".$ FILE_NAME)== FALSE){
move_uploaded_file($ file_tmp,文件/\".$ FILE_NAME);
}其他{//如果另一个存在重命名文件
$ NEW_DIR =文件/\".$ file_name.time();
重命名($ file_tmp,$ NEW_DIR);
}
$ Q = $ dbo-> prepare($查询);
$q->execute(array(':file_name'=>$file_name,':file_size'=>$file_size,':file_type'=>$file_type, ':时间戳'=> $时间戳)); }其他{
的print_r($错误);
}
}
如果(空($错误)){
回声成功;
}
}?>
有关您的理解(如你所说,你是新)
在PHP 的
数组可以使用阵列()
语言结构来创建。它需要一定数量用逗号分隔的键=>值
对作为参数。
例#1一个简单数组< PHP
$阵列=阵列(
富=> 酒吧,
栏=> 富,
);//为PHP 5.4的
$阵列= [
富=> 酒吧,
栏=> 富,
];?>
现在你的关心,你只要把从foreach循环的所有斩出的数据到一个新的数组:
< PHP
如果(使用isset($ _ FILES ['文件'])){
$错误=阵列();
$ filename_array =阵列();
的foreach($ _ FILES ['文件'] ['tmp_name的值']为$关键=> $ tmp_name的值){
$ FILE_NAME = $关键$ _ FILES ['文件'] ['名'] [$关键]。
$ filename_array [] = $ FILE_NAME;
$ FILE_SIZE = $ _ FILES ['文件'] ['大小'] [$关键];
$ file_tmp = $ _ FILES ['文件'] ['tmp_name的值'] [$关键];
$ FILE_TYPE = $ _ FILES [文件] [型] [$关键];
$时间戳=日期('Y-M-D G:我:S');
如果($ FILE_SIZE> 2097152){
$错误[] ='文件大小必须小于2 MB';
}
$查询=INSERT INTO upload_data(FILE_NAME,FILE_SIZE,FILE_TYPE,时间戳)VALUES(:FILE_NAME,:FILE_SIZE,:FILE_TYPE,:时间戳);
$ desired_dir =文件;
如果(空($错误)==真){
如果(is_dir($ desired_dir)== FALSE){
MKDIR($ desired_dir,0700); //创建目录,如果它不存在
}
如果(is_dir($ desired_dir /\".$ FILE_NAME)== FALSE){
move_uploaded_file($ file_tmp,文件/\".$ FILE_NAME);
}其他{//如果另一个存在重命名文件
$ NEW_DIR =文件/\".$ file_name.time();
重命名($ file_tmp,$ NEW_DIR);
}
$ Q = $ dbo-> prepare($查询);
$q->execute(array(':file_name'=>$file_name,':file_size'=>$file_size,':file_type'=>$file_type, ':时间戳'=> $时间戳)); }其他{
的print_r($错误);
}
}
如果(空($错误)){
回声成功;
}
}?>
的现在在你的第二块获取文件名作为的
的foreach($ filename_array为$文件名){
回声$文件名;
}
请参阅了解
Please go easy on me, this is my first time and obviously I have no idea what I'm doing...
I am using the following script that I found online to upload files and write the upload information into a database, I would like to collect the $file_name information into another array to be used outside of this code block in an implode function. I've searched high and low but have been unsuccessful in my searching. I figured out how to print the $file_name(s) from inside the if statement but not outside of both it and the php block but I can't figure out how to make it into an array instead.
Any help would be greatly appreciated.
<?php
if(isset($_FILES['files'])){
$errors= array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
$timestamp= date('Y-m-d G:i:s');
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into upload_data (FILE_NAME, FILE_SIZE, FILE_TYPE, timestamp) VALUES(:file_name,:file_size,:file_type, :timestamp)";
$desired_dir="files";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"files/".$file_name);
}else{ //rename the file if another one exist
$new_dir="files/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$q = $dbo->prepare($query);
$q->execute(array(':file_name'=>$file_name,':file_size'=>$file_size,':file_type'=>$file_type, ':timestamp'=>$timestamp));
}else{
print_r($errors);
}
}
if(empty($error)){
echo "Success";
}
}
?>
For your understanding (as you said you are new)
In PHPAn array can be created using the array()
language construct. It takes any number of comma-separated key => value
pairs as arguments.
Example #1 A simple array
<?php
$array = array(
"foo" => "bar",
"bar" => "foo",
);
// as of PHP 5.4
$array = [
"foo" => "bar",
"bar" => "foo",
];
?>
Now for your concern you just have to put all chopped out data from the foreach loop into a new array as:
<?php
if(isset($_FILES['files'])){
$errors= array();
$filename_array = array();
foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){
$file_name = $key.$_FILES['files']['name'][$key];
$filename_array[] = $file_name;
$file_size =$_FILES['files']['size'][$key];
$file_tmp =$_FILES['files']['tmp_name'][$key];
$file_type=$_FILES['files']['type'][$key];
$timestamp= date('Y-m-d G:i:s');
if($file_size > 2097152){
$errors[]='File size must be less than 2 MB';
}
$query="INSERT into upload_data (FILE_NAME, FILE_SIZE, FILE_TYPE, timestamp) VALUES(:file_name,:file_size,:file_type, :timestamp)";
$desired_dir="files";
if(empty($errors)==true){
if(is_dir($desired_dir)==false){
mkdir("$desired_dir", 0700); // Create directory if it does not exist
}
if(is_dir("$desired_dir/".$file_name)==false){
move_uploaded_file($file_tmp,"files/".$file_name);
}else{ //rename the file if another one exist
$new_dir="files/".$file_name.time();
rename($file_tmp,$new_dir) ;
}
$q = $dbo->prepare($query);
$q->execute(array(':file_name'=>$file_name,':file_size'=>$file_size,':file_type'=>$file_type, ':timestamp'=>$timestamp));
}else{
print_r($errors);
}
}
if(empty($error)){
echo "Success";
}
}
?>
Now in your second block get the file names as
foreach ($filename_array as $filename) {
echo $filename;
}
Refer for understanding PHP Arrays
这篇关于提取数据从数组的foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!