dtkp51520 2016-06-04 10:35
浏览 239
已采纳

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

I am trying to loop through an array of variables of type '$_FILES' posted from a form and process each image and store directories

The code is

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++;  
}

My question or rather propblem is,

Move_uploaded_file does not work.

I presume it is because I am using $_FILES['variable']['name'] instead of $_FILES['variable']['tmp_name'] in the move_uploaded_file command.

Am I right in my diagnosis ? If so, what should be done ? Because, I have assigned 'name' parameter in the array to loop. How do I undo it to 'tmp_name' inside the loop ? Or is there any other better alternative ?

  • 写回答

1条回答 默认 最新

  • douhoujun9304 2016-06-04 11:54
    关注

    I don't know what real problem is but you can try this:

    $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
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机