Upload mehrerer Datein in einem Script

Diese Seite verwendet Cookies. Durch die Nutzung unserer Seite erklären Sie sich damit einverstanden, dass wir Cookies setzen. Weitere Informationen

  • Upload mehrerer Datein in einem Script

    HiHo,

    Ich versuche gerade ein Script zu Basteln das es mir ermöglicht mehrere Files per PHP hochzu laden.

    Bei einer Datei läuft es ohne Probleme, bei 2 oder mehr schlägt es fehl.
    Habt ihr da einen Tip?

    Quellcode

    1. if ( isset($_FILES['file']) && !empty($_FILES['file']['name']) )
    2. {
    3. if (!empty($_FILES['file_thumb']['name']) )
    4. {
    5. $extension = $_FILES['file_thumb']['name'];
    6. $thumbname = 'img_' . time() . '_' . rand(0,9999) . '.' . $extension;
    7. $bdirl = dirname($Core->get(GETSERV, 'SCRIPT_FILENAME')) . '/modules/videos/images/';
    8. @move_uploaded_file($_FILES['file']['tmp_name'], $bdirl . $thumbname);
    9. @chmod($bdirl . $thumbname, 0777);
    10. }
    11. else
    12. {
    13. $thumbname = 'default.png';
    14. }
    15. $rand_name = 'vid_' . time() . '_' . rand(0,9999) . '.flv';
    16. $bdir = dirname($Core->get(GETSERV, 'SCRIPT_FILENAME')) . '/modules/videos/';
    17. @move_uploaded_file($_FILES['file']['tmp_name'], $bdir . $rand_name);
    18. @chmod($bdir . $rand_name, 0777);
    19. }
    Alles anzeigen


    Quellcode

    1. <form enctype="multipart/form-data" action="index.php?mode=video_gallery&sub=upload&id={$CAT_ID}" method="post">
    2. <div class="ucp_container">
    3. <table width="100%" border="0" cellspacing="1" cellpadding="1">
    4. <tr>
    5. <td><strong>{$LANG.AUSWAHL}:</strong></td>
    6. <td><input id="gfuploader" type="file" name="file" /></td>
    7. </tr>
    8. <tr>
    9. <td><strong>{$LANG.AUSWAHL}:</strong></td>
    10. <td><input id="gfuploader1" type="file" name="file_thumb" /></td>
    11. </tr>
    12. <tr>
    13. <td><strong>{$LANG.TITLE}:</strong></td>
    14. <td><input type="text" name="title" class="field_fs" value="" /></td>
    15. <tr>
    16. <td><strong>{$LANG.DESC}:</strong></td>
    17. <td><input type="text" name="desc" class="field_fs" value="" /> </td>
    18. </tr>
    19. <tr>
    20. <td class="row1" colspan="2" align="right"><input type="submit" name="save" value="{$LANG.SEND}" class="button_green" /></td>
    21. </tr>
    22. </table>
    23. <div class="ucp_clear"></div>
    24. </div>
    25. </form>
    Alles anzeigen
  • Hi,


    du benutzt einmal "gfuploader" & "gfuploader1". Im Script aber sprichst du nur $_FILES['file'] an !



    Quellcode

    1. <td><strong>{$LANG.AUSWAHL}:</strong></td>
    2. <td><input id="gfuploader[]" type="file" name="file" /></td>
    3. </tr>
    4. <tr>
    5. <td><strong>{$LANG.AUSWAHL}:</strong></td>
    6. <td><input id="gfuploader[]" type="file" name="file_thumb" /></td>
    7. </tr>


    Schau dir das $_FILES Array mittels var_dump() an.
  • äh, ich hab ausversehen auf die ID und nicht auf den Namen geschaut :wacko:

    so sollte es heissen:

    Quellcode

    1. <td><strong>{$LANG.AUSWAHL}:</strong></td>
    2. <td><input id="gfuploader" type="file" name="file[]" /></td>
    3. </tr>
    4. <tr>
    5. <td><strong>{$LANG.AUSWAHL}:</strong></td>
    6. <td><input id="gfuploader1" type="file" name="file[]" /></td>
    7. </tr>


    dann ist das Filesarray mehrdimensional so, dass du alles über eine Schleife abhandeln kannst und du nicht alles doppeln musst.


    Quellcode

    1. foreach($_FILES as $file) {
    2. if (!empty($file['name']) )
    3. {
    4. $extension = $file['name'];
    5. $thumbname = 'img_' . time() . '_' . rand(0,9999) . '.' . $extension;
    6. $bdirl = dirname($Core->get(GETSERV, 'SCRIPT_FILENAME')) . '/modules/videos/images/';
    7. @move_uploaded_file($file['tmp_name'], $bdirl . $thumbname);
    8. @chmod($bdirl . $thumbname, 0777);
    9. }
    10. else
    11. {
    12. $thumbname = 'default.png';
    13. }
    14. }
    Alles anzeigen





    und wenn du Bilder hochlädst solltest du immer mit getimagesize prüfen ob es wirklich ein Bild ist.
  • ich weiss grad nicht wie du das Video auf Gültigkeit prüfen kannst, aber wenn getimagesize() !=== false wahr ist, dann ist es ein Bild.

    Das kann man ja ganz einfach mit einer IF-Konstruktion abfragen.

    Quellcode

    1. // ungetestet !
    2. foreach($_FILES as $file) {
    3. if(getimagesize($file['file']['tmp_name']) !== false) {
    4. $pinfo = pathinfo($file['file']['tmp_name']);
    5. $extension = '.' . $pinfo['extension'];
    6. $newName = 'img_' . time() . '_' . rand(0,9999) . '.' . $extension;
    7. $uDir = '/modules/videos/images/';
    8. } else {
    9. $newName = 'vid_' . time() . '_' . rand(0,9999) . '.flv';
    10. $uDir = '/modules/videos/';
    11. }
    12. $bdir = dirname($Core->get(GETSERV, 'SCRIPT_FILENAME')) . $uDir;
    13. @move_uploaded_file($file['file']['tmp_name'], $bdirl . $newName);
    14. @chmod($bdirl . $thumbname, 0777);
    15. }
    Alles anzeigen
  • Entweder habe ich gerade einen hänger oder den Überblick verloren.

    Dein Code prüft ob es ein img ist, wenn ja wird es als img gespeichert, wenn nicht als video. Sehe ich das Richtig?

    Dann müsste ich praktisch deine foreach für das Pic nutzen und mein ersten Code für das Video da ich 2 pic und video gleichzeitig hochladen will

    EDIT; Ah ich verstehe, Filesarray ist ja mehrdimensional :pinch:
  • Fragz schrieb:

    Dein Code prüft ob es ein img ist, wenn ja wird es als img gespeichert, wenn nicht als video. Sehe ich das Richtig?
    EDIT; Ah ich verstehe, Filesarray ist ja mehrdimensional :pinch:



    genau ... somit brauchst du das ganze nur einmal, egal wieviele Bilder und Videos du hochlädst.

    Sicherheitstechnisch ist das ganze eher Problematisch, vorallem da du den hochgeladenen Dateien einen chmod von 0777 gibst ! warum ?
    So kann jeder irgendetwas hochladen und hat dann sogar die Rechte zum ausführen, das ist schon fatal !!

    Du könntest nach Dateiendungen prüfen. Problem hier ist, das ich ein "virus.sh" einfach in "one-night-in-easy-coding.mpeg" umbenennen kann und die Abfrage nach Dateiendungen sinnlos wäre.

    Du bräuchtest ein sicheres Verfahren ein Video auf Gültig zu prüfen.
    Wenn du eine Datei "blubb.exe" in "yeah.png" umbenennst, dann gibt getimagesize false zurück, da es kein gültiges Bild ist .... sowas wäre für Videos auch von Vorteil.