PHP 注意:未定义的偏移错误


PHP Notice: Undefined offset error

这是我在压缩器中的代码.php:

<?php
$filename = "isnotconverted_*";
$files = glob($filename);
$filefound = $files[0];
$new_filefound = str_replace( "isnotconverted_", '', $filefound );
$command = 'ffmpeg -i '.$filefound.' -b 64k -vf "movie=watermark.png [watermark]; [in][watermark] overlay=main_w-overlay_w-10:0 [out]" '.$new_filefound;
exec( $command );
unlink( $filefound );

当我直接打开compresser.php时没有错误,一切正常,但是当我使用 putty 并键入 /usr/local/bin/php /home/dltvbourse/public_html/compresser.php 时,我收到此错误:

PHP Notice:  Undefined offset: 0 in /home/dltvbourse/domains/dl.tvbourse.ir/public_html/compresser.php on line 5
ffmpeg version 0.7.11, Copyright (c) 2000-2011 the FFmpeg developers
  built on Mar  1 2015 14:55:21 with gcc 4.1.2 20080704 (Red Hat 4.1.2-55)
  configuration: --enable-gpl --enable-version3 --enable-nonfree --enable-shared --enable-libmp3lame --enable-libx264 --enable-libfaac --enable-libvorbis --enable-libopencore-amrnb --enable-libopencore-amrwb --enable-libvpx
  libavutil    50. 43. 0 / 50. 43. 0
  libavcodec   52.123. 0 / 52.123. 0
  libavformat  52.111. 0 / 52.111. 0
  libavdevice  52.  5. 0 / 52.  5. 0
  libavfilter   1. 80. 0 /  1. 80. 0
  libswscale    0. 14. 1 /  0. 14. 1
  libpostproc  51.  2. 0 / 51.  2. 0
-b: No such file or directory
PHP Warning:  unlink(): No such file or directory in /home/dltvbourse/domains/dl.tvbourse.ir/public_html/compresser.php on line 12

我该怎么办?

您应该在正确的文件夹中运行php文件。所以做一个:

cd /home/dltvbourse/public_html/
/usr/local/bin/php compresser.php

(在putty或您正在使用的任何SSH客户端中)。

失败的原因是glob()使用当前目录作为偏移目录。由于该模式的文件可能没有放在初始目录中,因此它不起作用。

此外,稍微

重写一下程序可能是安全的。毕竟,总是有可能出错,并且目录中没有放置这样的文件。在这种情况下,您可能最好检查数组的大小并抛出错误消息。

这将删除您的 PHP 注意:未定义的偏移量:0 错误

$filefound = (!empty($files[0])

) ? $files[0] : 假;