dongquan0024 2016-09-05 21:47
浏览 23
已采纳

尝试计算目录目录中的文件,但返回错误

I have an image uploading script that creates a new image and saves it to a directory. I want to count the number of images in that directory so I can add that total, plus one, to my new images name. The directory resides within a directory of public_html.

Also, I want to use a three digit numbering system so that if the number has say, only one digit than my switch adds two zeros to the front to make it a three digit number.

My PHP script:

<?php
$directory = "/galleries/painting_parties/" ;
$directory_list = scandir( $directory ) ;
$numb = count( $directory_list ) ;
++$numb ;
$numb .= "" ;
$numblen = strlen( $numb ) . "" ;

switch( $numblen )
   {
   case "1" : $n = "00" . $numb ; break ;
   case "2" : $n = "0" . $numb ; break ;
   case "3" :
   default : $n = $numb ;
   }

$new_image_name = $directory . "marias_art_" . $n . "_new_image.jpg" ;
?>
  • 写回答

1条回答 默认 最新

  • doushenken2833 2016-09-05 22:02
    关注

    If the directory is the issue try without the leading slash like this, as the leading slash says go to the root directory and then up from there

    $directory = "galleries/painting_parties/" ;
    

    Looks like quite a bit of unnecessary code as well

    Try

    <?php
    $directory = "/galleries/painting_parties/" ;
    $directory_list = scandir( $directory ) ;
    $numb = count( $directory_list ) ;
    // maybe subtract 2 for the . and .. directories, up to you
    // $numb -= 2;
    // make it a 4 char num with leading zeros
    $n = sprintf('%04d', ++$numb);
    $new_image_name = $directory . "marias_art_" . $n . "_new_image.jpg" ;
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站