duanping2695 2016-05-11 09:07
浏览 24
已采纳

Php自定义名称到文件

How can I make a custom name to the pictures they/I upload, preferably I want them to get numbers as names, and the next picture get the number of the last name +1.

<?php
    $target_dir = "../image/DBFilmCover/";

    $target_file = $target_dir . basename($_FILES["fileToUpload"]["name"]);
    $uploadOk = 1;
    imageFileType = pathinfo($target_file, PATHINFO_EXTENSION);

    // Check if image file is a actual image or fake image
    if (isset($_POST["submit"])) {
        $check = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
        if ($check !== false) {
            echo "File is an image - " . $check["mime"] . ".";
            $uploadOk = 1;
        } else {
            echo "File is not an image.";
            $uploadOk = 0;
        }
    } else {

        if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
            echo "The file " . basename($_FILES["fileToUpload"]["name"]) . " has been      uploaded.";
        } else {
            echo "Sorry, there was an error uploading your file.";
        }
    }
?>

I appreciate all the help I can get, if I didn't give enough information, just ask

  • 写回答

4条回答 默认 最新

  • doulu1945 2016-05-11 09:11
    关注

    Yes you can. Just change target_file according to your need:

    E.g:

    $target_file = $target_dir . "custom name";
    

    For counting number of files in your directory simply do:

    $files = scandir($target_dir);
    $files_count = count($files)-2; // -2 for ignoring . and ..
    

    Now filename would be:

    $path = $_FILES['fileToUpload']['name'];
    $ext = pathinfo($path, PATHINFO_EXTENSION);
    $files_count++;
    $filename = $files_count.".".$ext;
    $target_file = $target_dir . $filename;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源