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 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改