【问题标题】:Increment count each time a file is called in PHP每次在 PHP 中调用文件时递增计数
【发布时间】:2015-07-24 21:04:32
【问题描述】:

我有一个上传按钮,当用户单击上传按钮时,用户选择的文件会上传到特定目录。上传按钮调用upload.php 文件。
我有一个页面,其中一个问题最多可以填写 5 个选项答案,同时创建测验并为输入的每个选项上传音频。


页面快照

上传.php

<?php
$count=0;

$count++;

$target_dir = "Uploads/Question".$count."/Options/";
$target_file = "Uploads/Question".$count."/Options/".$count.".mp3"; //renames file as 1, 2, 3 etc.
$uploadOk = 1;
$audioFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
    echo "<br/>Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 50000000) {
    echo "<br/>Sorry, your file is too large.";
    $uploadOk = 0;
}

//Allow certain file formats
if($audioFileType != "avi" && $audioFileType != "mp3" && $audioFileType != "mp4"
&& $audioFileType != "wma" ) {
    echo "<br/>Only AVI, mp3, mp4 & WMA files are allowed.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "<br/>Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "<br/>Sorry, there was an error uploading your file.";
    }
}
?>

这部分代码$target_file = "Uploads/Question".$count."/Options/".$count.".mp3"; 应该将文件保存为 1.mp3,我想每次将 1 递增为 1、2、3 等。如您所见,计数在文件本身的开头设置为 0。 我的问题是,如何在每次调用 upload.php 文件时增加计数?

有点长,很难解释。感谢您提供帮助。

【问题讨论】:

  • 您必须在数据库或平面文件中存储一些内容,并在递增到新值之前检索最后一个值。
  • 从研究中我发现最简单的方法是计算目录本身的文件数。有什么帮助吗?
  • 您的代码一次无法处理超过 1 个上传的文件?您是在问我如何处理一次执行中上传的多个文件???
  • 不,我只想在每次调用 upload.php 文件时增加重命名文件的计数。我找到了一个简单的方法。我会自己回答以帮助其他人。

标签: php file-upload


【解决方案1】:

增加计数的一种简单方法是计算该目录中已经存在的文件数。

更新了 upload.php

<?php
$count=0;

$count++;


$i = 1; 
$dir = "Uploads/Question".$count."/Options/"; //count files in that directory
if ($handle = opendir($dir)) {
    while (($file = readdir($handle)) !== false){
        if (!in_array($file, array('.', '..')) && !is_dir($dir.$file)) 
            $i++;
    }
}

$target_dir = "Uploads/Question".$count."/Options/";
$target_file = "Uploads/Question".$count."/Options/".$i.".mp3"; //renames file as 1, 2, 3 etc.
$uploadOk = 1;
$audioFileType = pathinfo($target_file,PATHINFO_EXTENSION);

// Check if file already exists
if (file_exists($target_file)) {
    echo "<br/>Sorry, file already exists.";
    $uploadOk = 0;
}
// Check file size
if ($_FILES["fileToUpload"]["size"] > 50000000) {
    echo "<br/>Sorry, your file is too large.";
    $uploadOk = 0;
}

//Allow certain file formats
if($audioFileType != "avi" && $audioFileType != "mp3" && $audioFileType != "mp4"
&& $audioFileType != "wma" ) {
    echo "<br/>Only AVI, mp3, mp4 & WMA files are allowed.";
    $uploadOk = 0;
}

// Check if $uploadOk is set to 0 by an error
if ($uploadOk == 0) {
    echo "<br/>Sorry, your file was not uploaded.";
// if everything is ok, try to upload file
} else {
    if (move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file)) {
        echo "The file ". basename( $_FILES["fileToUpload"]["name"]). " has been uploaded.";
    } else {
        echo "<br/>Sorry, there was an error uploading your file.";
    }
}
?>

文件夹:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-24
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-01
    相关资源
    最近更新 更多