dounai6613 2015-06-08 15:49
浏览 59
已采纳

在上传复制图像并移动到其他文件夹

I am currently using the following to upload an image into a folder but I am finding that for my specific purpose I need to copy the image as it is uploaded and add it to a folder a little higher up in the directory.

Example: The image file gets uploaded to /folder/folder/images and this is good. I need it there. However, I also need a copy with the same file with the same filename here: /folder/images

Here is the PHP I am using to upload the image to begin with. How can I mdify it to also copy and add to the other folder as a new image is uploaded?

<?php
  // A list of permitted file extensions
  $allowed = array('png', 'jpg', 'gif','zip');

  if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){

  $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

  if(!in_array(strtolower($extension), $allowed)){
  echo '{"status":"error"}';
  exit;
}

  if(move_uploaded_file($_FILES['file']['tmp_name'],
  'images/'.$_FILES['file']['name'])){
  $tmp='images/'.$_FILES['file']['name'];
  echo 'images/'.$_FILES['file']['name'];
  //echo '{"status":"success"}';
  exit;
  }
}

echo '{"status":"error"}';
exit;
?>

UPDATE:

Here is the code that got things working for me:

<?php
  // A list of permitted file extensions
  $allowed = array('png', 'jpg', 'gif','zip');

  if(isset($_FILES['file']) && $_FILES['file']['error'] == 0){

  $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);

  if(!in_array(strtolower($extension), $allowed)){
  echo '{"status":"error"}';
  exit;
 }

  if(move_uploaded_file($_FILES['file']['tmp_name'],'images/'.$_FILES['file']['name'])){
   $tmp='images/'.$_FILES['file']['name'];
   $new = '../images/'.$_FILES['file']['name']; //adapt path to your needs;
   if(copy($tmp,$new)){
     echo 'images/'.$_FILES['file']['name'];
   //echo '{"status":"success"}';
  }
  exit;
 }
}

echo '{"status":"error"}';
exit;
?>
  • 写回答

3条回答 默认 最新

  • doujiacai4986 2015-06-08 16:17
    关注

    Based on the comments on your question:

    if(move_uploaded_file($_FILES['file']['tmp_name'],'images/'.$_FILES['file']['name'])){
        $tmp='images/'.$_FILES['file']['name'];
        $new = 'newFolder/'.$_FILES['file']['name']; //adapt path to your needs;
        if(copy($tmp,$new)){
            echo 'images/'.$_FILES['file']['name'];
            //echo '{"status":"success"}';
        }
        exit;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Opencv配置出错
  • ¥15 模电中二极管,三极管和电容的应用
  • ¥15 关于模型导入UNITY的.FBX: Check external application preferences.警告。
  • ¥15 气象网格数据与卫星轨道数据如何匹配
  • ¥100 java ee ssm项目 悬赏,感兴趣直接联系我
  • ¥15 微软账户问题不小心注销了好像
  • ¥15 x264库中预测模式字IPM、运动向量差MVD、量化后的DCT系数的位置
  • ¥15 curl 命令调用正常,程序调用报 java.net.ConnectException: connection refused
  • ¥20 关于web前端如何播放二次加密m3u8视频的问题
  • ¥15 使用百度地图api 位置函数报错?