变量的数组值

时间:2016-04-08 14:03:14

标签: javascript php arrays

我有一个表单,用户可以上传多个文件。 我试图从文件[]获取文件并将值作为变量返回。这是我第一次处理图片上传,我几乎陷入困境。非常感谢您的帮助。此致,

以下是我的文件上传表单元素。

 <div class="section">
    <label for="file1" class="field-label"> 
        Upload image - <span class="small-text fine-grey"> (ONLY JPG : PNG : PDF) </span> 
    </label>
    <label class="field prepend-icon file">
        <span class="button btn-primary"> Choose File </span>
        <input type="file" class="gui-file" name="file[]" required id="file1" 
        onChange="document.getElementById('uploader1').value = this.value;">
        <input type="text" class="gui-input" id="uploader1" placeholder="no file selected" readonly>
        <span class="field-icon"><i class="fa fa-upload"></i></span>
    </label>
</div><!-- end  section -->

 <div class="section">
    <label for="file1" class="field-label"> 
        Upload another image - <span class="small-text fine-grey"> (ONLY JPG : PNG : PDF) </span> 
    </label>
    <label class="field prepend-icon file">
        <span class="button btn-primary"> Choose File </span>
        <input type="file" class="gui-file" name="file[]" required id="file2" 
        onChange="document.getElementById('uploader2').value = this.value;">
        <input type="text" class="gui-input" id="uploader2" placeholder="no file selected" readonly>
        <span class="field-icon"><i class="fa fa-upload"></i></span>
    </label>
</div><!-- end  section -->

到目前为止,我创建了以下内容,但我认为它不起作用。

我的表单已启用以下功能。 ENCTYPE =“多部分/格式数据” 以下是我的进程.php的一部分,我发送表单值。

  // Here is the updated code***
 $file = $_FILES['file']['name'];
 $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $folder="uploads/";

 $filecount = count($file);
  for ($f=0; $f < $filecount; $f++){
  $rand =  rand(1000,100000)."-";
   if (move_uploaded_file($file_loc[$f],$folder.$rand.$file[$f]) ){
    //I need maximum 2 files uploaded. I guess name should be something else instead of file[0,1] such as finalname[0];
   $img_url=$file[0];
   $img_urla=$file[1];
   }

1 个答案:

答案 0 :(得分:2)

您的process.php代码存在问题

move_uploaded_file($file_loc,$folder.$file)

在这里,您传递$file_loc$file,因为您正在处理multiple uploads,所以它们位于您的案例数组中。 这一行

$file = rand(1000,100000)."-".$_FILES['file']['name'];

不正确,因为$_FILES['file']['name']是一个无法附加到字符串的数组。

你必须循环遍历文件并将它们全部移动到那里:

$file =$_FILES['file']['name'];
$filecount = count($file);
for ($f=0; $f < $filecount; $f++){
    $rand =  rand(1000,100000)."-";
    if (move_uploaded_file($file_loc[$f],$folder.$rand.$file[$f]) ){
        //file uploaded
    }else{
        //error in file upload
    }
}

之后,您的文件应该成功上传。

对更新代码的评论

所以你想要前两个文件名和这些上传文件的位置。你现在做的是在变量$img_url$img_urla中添加第一个和第二个文件名,但这些只是文件名,而且你多次这样做,因为代码在循环中。
我建议首先将所有上传的文件名和路径存储在一个数组中,然后只显示前两个,如下所示:

 // Here is the updated code***
 $file = $_FILES['file']['name'];
 $file_loc = $_FILES['file']['tmp_name'];
 $file_size = $_FILES['file']['size'];
 $file_type = $_FILES['file']['type'];
 $folder="uploads/";
 $img_urls = array(); //<-- create an array of image urls

 $filecount = count($file);
 // so far so good
 for ($f=0; $f < $filecount; $f++){
   $rand =  rand(1000,100000)."-";
   if (move_uploaded_file($file_loc[$f],$folder.$rand.$file[$f]) ){
       $img_urls[] = $folder.$rand.$file[$f]; //add the file to the array.
   }
 }
 if (count($img_urls) > 1){ //check if there are at least 2 files
     echo "image 1: ".$img_urls[0]." and image 2: ". $img_urls[1];
 }