1

I have an uploader that make me upload files to the server.

I would like that when I upload an image, this image is automatically visible in a page of the site. Eg, if I have a site of a music band, if I upload from an administrator password protected page the poster of the next concert, and automatically in the page "events" I can see that poster.

In the same way if I upload (from another uploader) the text for that concert, this text appears in the event page in a "description of the concert" div.

Is it possible? thank you!

Luigi
  • 151
  • 1
  • 11

1 Answers1

1

If you need to do that, then using the database and store the uploaded items in its coloumn is the better way..

But to answer your question

You can do something like this.

Step 1 :

Read all the files in your directory

$dir    = 'up/';
$files = scandir($dir);

Step 2 :

Do a foreach and print it in your file (If you need to display an image it can be possible and you can have links over there )

$i = 1;
foreach ($files as $key) 
{
    echo $i.' - '.$key.'<hr>';
    echo "<a href='up/".$key."'><img src ='up/".$key."'>".$key."</a>";
    $i++;
}

So you can have something like this..

<?php
$dir    = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key) 
{
    echo $i.' - '.$key.'<hr>';
    echo "<a href='up/".$key."'><img src ='up/".$key."'>".$key."</a>";
    $i++;
}
echo 'End of Files';

?>

Note :

I am using the directory name as up you can have it any name of your own.

If you have this file as list.php then it should have a sub folder named as up and you should have files inside it.. You can have image or text file as you need.

Update :

If you just want to list the files, then you just need to echo it

<?php
$dir    = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key) 
{
    echo $i.' - '.$key.'<hr>';
    $i++;
}
echo 'End of Files';

?>

Here's the eval link

Update 2 :

As the OP needs to have an anchor tag and wants to remove the directory level Here's the updated code

<?php
$dir    = 'up/';
$files = scandir($dir);
echo '<h1>List of Uploaded Files</h1> <br><hr>';
$i = 1;
foreach ($files as $key) 
{
    if ($i>3) 
    {
    $j = $i-3;
    echo $j."<a href='up/".$key."'>".$key."</a><hr>";
    }
    $i++;

}
echo 'End of Files';

?>

Here's the Eval

Sulthan Allaudeen
  • 11,330
  • 12
  • 48
  • 63
  • Thank you very much! I tried and it works, this is not exactly what I thought but it could work! I removed because I need only a list without the images. there is some disorder in the list because of links not ordered http://valsileprova.altervista.org/upload_list.php – Luigi May 31 '15 at 15:02
  • Yes, I would like on the one hand have a clickable list of the uploaded files and on the other know if I can recall the uploaded images into an existing html page – Luigi May 31 '15 at 15:13
  • You mean in the listed flies ? – Sulthan Allaudeen May 31 '15 at 15:17
  • no, sorry, it's a quite difficult idea to explain and also to make, i think. The idea is to make an uploader that when I upload an image automatically there is a new image ina new div in the page "events" – Luigi May 31 '15 at 15:25
  • That's ok .. You mean to display the image instantly after upload ? – Sulthan Allaudeen May 31 '15 at 15:31
  • yes! (could you please also help me fixing the problem with the code above so that I resolve that problem and I can focus on this new, bigger problem?) I'm sorry if i bother you! – Luigi May 31 '15 at 15:35
  • Cool, you have to add only one line to that code i given `echo "";` Here's the eval. https://eval.in/373341 – Sulthan Allaudeen May 31 '15 at 15:42
  • I added the code in the update, it works. but now i don't have the links in the file names and in the list the first 2 file names are respectively only one and two dots – Luigi May 31 '15 at 15:59
  • Do you want to have links in the name ? – Sulthan Allaudeen May 31 '15 at 16:00
  • Those above two dots are directory structure . Do you want to remove it ? – Sulthan Allaudeen May 31 '15 at 16:01
  • I'm really impressed by your kind patience, thank you @Sulthan Allaudeen – Luigi May 31 '15 at 16:01
  • 1
    You're welcome :) Here's the Eval that satisfy your two needs :) https://eval.in/373345 – Sulthan Allaudeen May 31 '15 at 16:04
  • How can I set css for the uploaded image (like size and placement)? Is it possible too add a way to remove the uploaded file if the user accidentally uploads a wrong file? – Luigi May 31 '15 at 17:14
  • In your file that you upload you can add `.css` and `.js` manually, And to remove you can use `unlink` but i recommend to use a `database` for such things – Sulthan Allaudeen May 31 '15 at 17:17
  • Welcome. You should start with Creating Database, Tables with columns and once you're done, you should do connectivity so that you can do all the operations easily – Sulthan Allaudeen May 31 '15 at 17:25
  • ouch, it seems quite difficult.. and is with unlink easier? – Luigi May 31 '15 at 17:36
  • Ahmm Though it seems difficult it was quite interesting :) And unlink is easier enough .. – Sulthan Allaudeen May 31 '15 at 17:42
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/79262/discussion-between-sulthan-allaudeen-and-luigi). – Sulthan Allaudeen May 31 '15 at 17:44
  • How can I list the files uploaded not in alphabetical order but in order of upload? – Luigi Jun 01 '15 at 22:13
  • can you please help me in this issue? Find someone able to solve this problem seems really hard! http://stackoverflow.com/q/30562945/4842333 – Luigi Jun 13 '15 at 11:20