dqxyh48864 2014-03-11 14:46
浏览 44

在上传时计算dir并重命名多个上传的文件。

I'm trying to upload multiple files (imgs) from a form and I want to get the count of the directory they will be uploaded to then rename them accordingly. 1.jpg, 2.jpg, 3.jpg and so on depending on how many are uploaded. The closest I've come is getting it to rename and upload first file.

<?php
$foldername = $_POST['stock'];

$path = 'vehicles/' . $foldername;
mkdir($path);
?>

<?php
$valid_formats = array("jpg", "png", "gif", "zip", "bmp");
$max_file_size = 10240*1000; //100 kb
$path = "vehicles/$stock/"; // Upload directory
$count = 0;

$directory = "vehicles/$stock/";
$filecount = 0;
$files = glob($directory . "jpg");
if ($files){
  $filecount = count($files);
}

if(isset($_POST) and $_SERVER['REQUEST_METHOD'] == "POST"){
  // Loop $_FILES to execute all files
  foreach ($_FILES['files']['name'] as $f => $name) {     
    if ($_FILES['files']['error'][$f] == 4) {
      continue; // Skip file if any error found
    }        
    if ($_FILES['files']['error'][$f] == 0) {            
      if ($_FILES['files']['size'][$f] > $max_file_size) {
        $message[] = "$name is too large!.";
        continue; // Skip large files
      }
      elseif( ! in_array(pathinfo($name, PATHINFO_EXTENSION),                   $valid_formats) ){
        $message[] = "$name is not a valid format";
        continue; // Skip invalid file formats
      }
      else{ 
        // No error found! Move uploaded files 
        $ext = pathinfo($_FILES['files']['name'][$f], PATHINFO_EXTENSION);
        $uniq_name = uniqid() . '.' .'jpg';
        move_uploaded_file($_FILES["files"]["tmp_name"][$f], $path .              $uniq_name);
        $count++; // Number of successfully uploaded file
      }
    }
  }
}
?>
  • 写回答

2条回答 默认 最新

  • duanlou2917 2014-03-11 15:04
    关注

    Your foreach is going through the first file. You want something that loops through all files.

    foreach ($_FILES as $file_input){
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制