你可以用$ _FILES [' name']而不是$ _FILES [' tmp_name']在php中执行move_uploaded_file

时间:2016-06-04 10:35:05

标签: php file-upload

我试图遍历一系列类型为' $ _ FILES'的变量。从表单发布并处理每个图像和存储目录

代码是

if(isset($_POST['travsubli'])){

$date=Date('Y-m-d');
$uname=substr($utravlastname,0,5).substr($utravphone,5,10);
$destin=str_replace(array(" ","/","."),"-",$_POST['Destination']);
if(!is_dir("images/userimages/travelphotos")) {mkdir("images/userimages/travelphotos");}
if(!is_dir("images/userimages/travelphotos/$uname")){mkdir("images/userimages/travelphotos/$uname");}
echo "Check : ".$_FILES['TourPhoto1']['name']."<br>";
$photosarray=array($_FILES['TourPhoto1']['name'],$_FILES['TourPhoto2']['name'],$_FILES['TourPhoto3']['name'],$_FILES['TourPhoto4']['name'],$_FILES['TourPhoto5']['name'],$_FILES['TourPhoto6']['name']);

$pai=1;
foreach($photosarray as $pha){
$ext=pathinfo($pha,PATHINFO_EXTENSION);
if(!empty($ext)){

$newphotoname="Travelogue-Photo-$destin-$pai.$ext";
$newphotopath="images/userimages/travelphotos/$uname/$newphotoname";

      if(move_uploaded_file($pha,$newphotopath)){
           echo "Photo Successfully Uploaded !<br>";
        }else{echo "Something went wrong with Photo Upload !<br>";     $e=error_get_last();echo $e['message']."<br>";}
       }    
$TourPhoto="TourPhoto$pai";
$$TourPhoto=$newphotoname;
echo $pha."->$TourPhoto->".$$TourPhoto.$pai."<br>";
$pai++;  
}

我的问题或更确切的问题是,

Move_uploaded_file不起作用。

我认为这是因为我使用$ _FILES [&#39;变量&#39;] [&#39;名称&#39;]而不是$ _FILES [&#39;变量&#39;] [&# 39; tmp_name&#39;]在move_uploaded_file命令中。

我的诊断是对的吗?如果是这样,该怎么办?因为,我已经指定了&#39; name&#39;要循环的数组中的参数。如何将其撤消到&#39; tmp_name&#39;在循环内?或者还有其他更好的选择吗?

1 个答案:

答案 0 :(得分:1)

我不知道真正的问题是什么,但你可以试试这个:

$photosarray = array( 
    array(
        'tmp_name' => $_FILES['TourPhoto1']['tmp_name'],
        'real_name' => $_FILES['TourPhoto1']['name'],
    ),
    array(
        'tmp_name' => $_FILES['TourPhoto2']['tmp_name'],
        'real_name' => $_FILES['TourPhoto2']['name'],
    ),
    array(
        'tmp_name' => $_FILES['TourPhoto3']['tmp_name'],
        'real_name' => $_FILES['TourPhoto3']['name'],
    ),
    // etc
);

foreach ($photosarray as $pha) {
    $ext = pathinfo($pha['real_name'],PATHINFO_EXTENSION);
    $path = ''; 
    // make your path here

    // copy file
    move_uploaded_file($pha['tmp_name'], $new_path);
    // do other stuff
}