Rename uploaded files

南笙酒味 提交于 2020-01-07 07:07:11

问题


I am uploading files via php. The amount of files can be uploaded is determined by a value which is stored in database. So let's say, this value is 3, than my code show three html file input. This is working. I could also rename one file too. working extension filter, and size restrictions.. But when I tried to do this with more than one, I stacked... The new name should be the 'number of list element'-'base filename'. Example:

1-thisisanimage.jpg
2-anotherimage.jpg
3-andthisalso.jpg

I tried with foreach but I think I over-complicated. I also tried different scripts from the internet but none of them really what I just cannot do this. Anybody? :)

EDIT: added the part of my code

if (!file_exists("uploads/".$date."-".$email."-vid-".$videoID)) {
    mkdir("uploads/".$date."-".$email."-vid-".$videoID."/", 0777, true);
    }

    $imagecounter = 0;
        $_FILES["file"] = array();
    foreach ($_FILES["file"] as $file) {
        $imagecounter++;


        $filename = $_FILES["file"]["name"];
        $file_basename = substr($filename, 0, strripos($filename, '.')); // get file extention
        $file_ext = substr($filename, strripos($filename, '.')); // get file name
        $filesize = $_FILES["file"]["size"];
        $allowed_file_types = array('.jpg','.jpeg','.png','.gif');  

        if (in_array($file_ext,$allowed_file_types) && ($filesize < 200000))
        {   
            // Rename file
            $newfilename = "$imagecounter-".$file_basename . $file_ext;
            if (file_exists("uploads/".$date."-".$email."-vid-".$videoID."/" . $newfilename))
            {
                // file already exists error
                echo "You have already uploaded this file.";
            }
            else
            {       
                move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/".$date."-".$email."-vid-".$videoID."/" . $newfilename);
                echo "File uploaded successfully.";     
            }
        }

回答1:


When you have files in array:

$i = 1;
foreach($_FILES['file'] as $file){
    move_uploaded_file($file['tmp_name'], $i.'-'.$file['name']);
    ++$i;
}



回答2:


your problem is that the $imagecounter is allways 0, so when you upload a new file it gets the value 1 (one file in this request) instant of the number of files that are allready uploaded.

so count the files that are allready uploaded in your destination folder and increase this value +1 to get your list number work.

$uploadDir = "uploads/".$date."-".$email."-vid-".$videoID."/";
$fileInterator = new FilesystemIterator($uploadDir, FilesystemIterator::SKIP_DOTS);
$imagecounter = iterator_count($fileInterator);

but attention when delete files - eg. you have these files in the folder:

  • 1-test.png
  • 2-hello.png
  • 3-wow.png

and delete 2-hello.png when only count the number of files (wich is now 2) then the next uploaded file get the name 3-new.png unnecessary this number is allready taken by 3-wow.png



来源:https://stackoverflow.com/questions/24802686/rename-uploaded-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!