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 关于#python#的问题,请各位专家解答!
  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备