Page 1 of 1

Notice: Undefined index: i

Posted: Sun Dec 05, 2010 4:21 am
by FernandoBasso
I have this php code to delete images from a folder at the user will:

Code: Select all

    <?php
    $dir = 'dir_upload';
    $array_imgs = scandir($dir);
    for ($i = 0; $i < count($array_imgs); $i++) {
        $arq = $array_imgs[$i];
        if ($arq <> '.' && $arq <> '..') {
            $ext = explode('.', $arq);
            if (($ext[i] == 'gif') || ($ext[i] == 'jpg') || ($ext[i] == 'png')) {
                echo("<div class='del_img'>");
                echo("<a href='$dir/$arq' class='highslide' onclick='return hs.expand(this,
                {wrapperClassName: \"wide-border\", captionOverlay: {position: \"rightpanel\"}})'>");
                echo("<img src='$dir/$arq' height='200' width='200' alt='$arq'></a>"); 
                /* Este 'file[]' é o que cria o array com o arquivos que tiveram o checkbox
                 * marcado. Assim podemos deletar vários arquivos de uma só vez. */
                echo("<input type='checkbox' name='file[]' value='$arq'>");
                // Fecha a div del_img.
                echo("</div>\n");
            }
        }
    }
}

// Lógica para deletar a imagem. 
if (isset($_POST['file']) && is_array($_POST['file'])) {
    foreach ($_POST['file'] as $file) {
        if ($arq <> '.' && $arq <> '..') {
            unlink("$dir/$file") or die(mysql_error());
            /* redirect "'after'" deleting files so the user can refresh without that
             * resending post info message. */
            header("location: " . $_SERVER['REQUEST_URI']); 
        }
    }
}
?>
</form>
And I am getting this warning:
( ! ) Notice: Use of undefined constant i - assumed 'i' in /home/nando/Websites/romario.home/delete_images.php on line 30.

Here, in my code, it is the code on this line:

Code: Select all

            if (($ext[i] == 'gif') || ($ext[i] == 'jpg') || ($ext[i] == 'png')) {
Any help would be appreciated. Thanks in advance.

Re: Notice: Undefined index: i

Posted: Sun Dec 05, 2010 5:24 am
by Darhazer
Actually is should be:

Code: Select all

$ext = end(explode('.', $arq));
            if (($ext == 'gif') || ($ext == 'jpg') || ($ext == 'png')) {

Re: Notice: Undefined index: i

Posted: Sun Dec 05, 2010 6:12 am
by FernandoBasso
Darhazer wrote:Actually is should be:

Code: Select all

$ext = end(explode('.', $arq));
            if (($ext == 'gif') || ($ext == 'jpg') || ($ext == 'png')) {
Thanks. I'm reading the documentation for this 'end'.

However, I solved the problem in another way. I had a 'thumbs' folder inside
dir_upload/ that of course does not have a .extension part. That was causing the
message 'undefined offset'.

I just added one more condition to line 30:

Code: Select all

        if ($arq <> '.' && $arq <> '..' && $arq != 'thumbs') {

Re: Notice: Undefined index: i

Posted: Sun Dec 05, 2010 11:35 am
by Darhazer
FernandoBasso wrote:
Darhazer wrote:Actually is should be:
I just added one more condition to line 30:

Code: Select all

        if ($arq <> '.' && $arq <> '..' && $arq != 'thumbs') {
Maybe you have to check if $arq is file, and not directory, and in this way the code won't depend on any folder name:

Code: Select all

        if (!is_dir($dir_upload . '/' . $arq)) {