2013-03-13 72 views
1

我有一个形式的输入与此类似:

<p><label>Image 1: </label><input type="file" name="candy"></p> 
<p><label>Image 2: </label><input type="file" name="apple"></p> 
<p><label>Image 3: </label><input type="file" name="red"></p> 

使用到$ _Files一个循环,我想知道是否有可能获得输入字段名(不是值)的上传文件的输入元素?

foreach($_FILES as $file) { 
    /* 
    do image processing here, but would like to save as 
    'candy.jpg' rather than $file['name']. 
    */ 
} 

所以,我希望能够得到字段名“糖果”或“苹果”不知道它是什么。到目前为止,我所见过的所有事情都假定在处理文件时已经知道'糖果'。

换句话说,我想从上传的文件后退到它上传的元素。这可能与PHP?

更新了解决方案:

foreach($_FILES as $key=>$file) { 
    $image = new Imagick($file['tmp_name']); 
    // do some processing 
    $image->writeImage($key.'.jpg'); 
} 

会给 'candy.jpg' 或者根据输入字段名 'apple.jpg'。

回答

0

您可以要求在foreach声明密钥的姓名(或名称)的数组。

foreach($_FILES as $key=>$file) { 

} 
+0

太简单了,谢谢! – Dodo 2013-03-13 06:26:59

1

尝试

foreach($_FILES as $nameOfInputFile => $file) { 

    // your stuff 

} 

$_FILES是有作为指标的输入(文件类型)