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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里