Pregunta

I have written a small php script which reads file names from a directory, uses explode to get rid of the extension and then displays the file name on a php page.

But for some reason, whenever the file name consists of more than one word, it only shows the first word of the file name.

Here is the code,

while  ($name = readdir ($handle)) 
        {
            if ($name == '.' || $name == '..')
            continue; /* don't echo anything, skip to next read */
            $i++;
            echo '<td >';
            echo '<form name="form" method="get" action="download.php">';
            echo '<input type="hidden" name="file" value='.$name.' />';
            $parts=explode(".",$name);
            $name=$parts[0];
            echo '<input type="submit" name="submit" value='.$name.' class="subbutton" /> </br></br>';
            echo '</form>';
            echo '</td>';
            if($i==5)
            {
            echo '</tr>';
            echo '<tr>';
            $i=0;
            }
        }
¿Fue útil?

Solución

Change this line:

echo '<input type="submit" name="submit" value='.$name.' class="subbutton" /> </br></br>';

to this:

echo '<input type="submit" name="submit" value="'.$name.'" class="subbutton" /> </br></br>';

That way you're quoting your attribute and the HTML parser can't get confused by spaces in your filenames.

Otros consejos

You will want to make sure you get everything to the left of the last period in the file name, since file names can include more than one. This makes explode a less-than-ideal solution. The following code will give you the name of the file, minus the file extension:

$file_name_without_extension = substr($full_file_name, 0, strrpos($full_file_name, "."));
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top