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 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭