[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 308
  • Last Modified:

let user choose upload dir

hi

i'm using a php script to let users upload some pictures.

how can i let them upload the pictures to a chosen directory from a drop-down-list that shows the folders in the main upload folder?

thanks a lot!
0
peps03
Asked:
peps03
  • 13
  • 11
  • 2
1 Solution
 
ambientguitarCommented:
Check this out may be solution for you!

http://www.tizag.com/phpT/fileupload.php
0
 
peps03Author Commented:
i cant see where it lets users choose their upload-directory..?

or where it generates a drop-down-list with the folders in the directory where the users can choose from to upload to...
0
 
ambientguitarCommented:
Apologies thought you might be able to tweak some of teh code there , it may be worth reading this before you  start implementing.
http://www.scanit.be/uploads/php-file-upload.pdf
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
peps03Author Commented:
dude, i don't need a teaching and already have a good solid upload-script!

what does all this have to do with my question?
0
 
Amar BardoliwalaCommented:
Hello peps03,

for your questions.. I have following solutions..
for each directroy name in drop down you should have its folder name..

suppose you have 3 directories name dir1,dir2 and dir3 and you default upload folder path is like "C:\upload\"

you just need to store path upto main upload directory in some variable say

$dir_path = "C:\upload\";

Now in your code you will get user selection dir1 or dir2 or dir3..

so in your upload script you just need to provide your path like  $dir_path."dir1" or  $dir_path."dir2"
or  $dir_path."dir3" depending on user selection.

Hope this will help you.

Thanks.
 
0
 
Amar BardoliwalaCommented:
Hello peps03,

may be I replied half answer.

Following is the php code which will give you array of all directories.. you just need to pass root directory that is your upload directory.

Using that array you can create drop down list

Following is the code

<?php

function directoryToArray($directory, $recursive) {
      $array_items = array();
      if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
                        if (is_dir($directory. "/" . $file)) {
                              if($recursive) {
                                    $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
                              }
                              $file = $directory . "/" . $file;
                              $array_items[] = preg_replace("/\/\//si", "/", $file);
                        }
                  }
            }
            closedir($handle);
      }
      return $array_items;
}

$files = directoryToArray("./", false);

echo '<ul>';

for ($i = 0; $i <= count($files); $i++) {
      echo '<li>' . $files[$i] . '</li>';
}

echo '</ul>';

?>

Thanks.
0
 
peps03Author Commented:
Thanks amar,

i made it like this:

[code]
<?php

function directoryToArray($directory, $recursive) {
      $array_items = array();
      if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
                        if (is_dir($directory. "/" . $file)) {
                              if($recursive) {
                                    $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
                              }
                              $file = $directory . "/" . $file;
                              $array_items[] = preg_replace("/\/\//si", "/", $file);
                        }
                  }
            }
            closedir($handle);
      }
      return $array_items;
}

$files = directoryToArray("uploaded/", false);

echo "<select name='test' value='test'>";

for ($i = 0; $i <= count($files); $i++) {
      echo '<option value='. $files[$i] .'>' . $files[$i] . '</option>';
}

echo '</select>';

?>[/code]

how do i now implement this into an upload script like this:

[code]
<?php
echo "<form method='post' enctype='multipart/form-data'><input type='file' name='file' width='300'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='upload1' value='Upload'> </form>";
$map = "audio/";  //map waar die in komt (wel chmod permision 777 geven
if(isset($_POST['upload1'])) {
  if($_FILES['file']['size'] <= 1024*10000) {  
    // Controle of het bestand groter is als 10 mb (tussen de [''] van $_FILES vul je de naam van het veld in)
    $extension = array();
    $extension[] = "audio/mp3";  // zet de extensions (vb: .jpg) in de array..  
    $extension[] = "audio/mpeg";  // altijd met audio/ ervoor en zonder punt
    // Let op .jpg kan hij verschillend zien (bv: ook als .pjpg) zoek dus op internet een lijst anders krijg je vaak errors..
    if(in_array($_FILES['file']['type'], $extension)) {
    // Controle of hij een bestand met de juiste extension heeft geupload
      if(is_uploaded_file($_FILES['file']['tmp_name'])) {  //Contorleren of hij is geupload
        move_uploaded_file($_FILES['file']['tmp_name'], $map.$_FILES['file']['name']);
        // eerst geef je de file aan met de tmp naam waar die tijdelijk staat op geslagen, daarna de mapnaam en de filenaam met een punt er tussen  
        echo "<p><span style=\"color:#F00\">Bestand succesvol geupload..</span></p>";
      } else {
        echo "<p><span style=\"color:#F00\">Er is iets mis gegaan bij het uploaden, probeer het nogmaals</span></p>";
      }
    } else {
      echo "<p><span style=\"color:#F00\">Het bestand heeft een verkeerde extensie</span></p>";
    }
  } else {
    echo "<p><span style=\"color:#F00\">Het bestand moet kleiner zijn dan 10 mb</span></p>";
  }
} else {
// Hier je formulier (deze is zichtbaar wanneer er niet op submit is gedrukt..)
}
?>[/code]

where the above script must change "$map = "audio/";

thanks
0
 
Amar BardoliwalaCommented:
Hello peps03,

in your script you just need to do following..

$map = "uploaded/" . $_POST["test"];

and it should work.

Thanks.
0
 
peps03Author Commented:
Hi amar,

i tried this below and it isn't working... strange!


<? ////////////////////////////////////////////////////////////////////AUDIO/////////////////////////////////////////////?>
<?php

function directoryToArray($directory, $recursive) {
      $array_items = array();
      if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
                        if (is_dir($directory. "/" . $file)) {
                              if($recursive) {
                                    $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
                              }
                              $file = $directory . "/" . $file;
                              $array_items[] = preg_replace("/\/\//si", "/", $file);
                        } 
                  }
            }
            closedir($handle);
      }
      return $array_items;
}
$files = directoryToArray("uploaded/", false);
echo "<select name='folder' value='folder'>";
for ($i = 0; $i <= count($files); $i++) {
      echo '<option value='. $files[$i] .'>' . $files[$i] . '</option>';
}
echo '</select>';
?>

<hr width="<?PHP echo HRWIDTH ?>" align="left">
<h2>Beheer audio</h2>
<p>Hieronder kan een mp3 bestand geselecteerd worden om te uploaden, max 10mb.<br /><br /></p>
<?php
echo "<form method='post' enctype='multipart/form-data'><input type='file' name='file' width='300'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='upload1' value='Upload'> </form>";
$map = "uploaded/" . $_POST["folder"];  //map waar die in komt (wel chmod permision 777 geven 
if(isset($_POST['upload1'])) { 
  if($_FILES['file']['size'] <= 1024*10000) {  
    // Controle of het bestand groter is als 10 mb (tussen de [''] van $_FILES vul je de naam van het veld in) 
    $extension = array(); 
    $extension[] = "audio/mp3";  // zet de extensions (vb: .jpg) in de array..  
    $extension[] = "audio/mpeg";  // altijd met audio/ ervoor en zonder punt 
    // Let op .jpg kan hij verschillend zien (bv: ook als .pjpg) zoek dus op internet een lijst anders krijg je vaak errors.. 
    if(in_array($_FILES['file']['type'], $extension)) { 
    // Controle of hij een bestand met de juiste extension heeft geupload 
      if(is_uploaded_file($_FILES['file']['tmp_name'])) {  //Contorleren of hij is geupload 
        move_uploaded_file($_FILES['file']['tmp_name'], $map.$_FILES['file']['name']); 
        // eerst geef je de file aan met de tmp naam waar die tijdelijk staat op geslagen, daarna de mapnaam en de filenaam met een punt er tussen  
        echo "<p><span style=\"color:#F00\">Bestand succesvol geupload..</span></p>"; 
      } else { 
        echo "<p><span style=\"color:#F00\">Er is iets mis gegaan bij het uploaden, probeer het nogmaals</span></p>"; 
      } 
    } else { 
      echo "<p><span style=\"color:#F00\">Het bestand heeft een verkeerde extensie</span></p>"; 
    } 
  } else { 
    echo "<p><span style=\"color:#F00\">Het bestand moet kleiner zijn dan 10 mb</span></p>"; 
  } 
} else { 
// Hier je formulier (deze is zichtbaar wanneer er niet op submit is gedrukt..) 
} 
?>

Open in new window

0
 
Amar BardoliwalaCommented:
Hello peps03,

Please use following code.. I made some minor changes after looking at your code.

Following code should work fine for you.

<?php

function directoryToArray($directory, $recursive) {
      $array_items = array();
      if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
                        if (is_dir($directory. "/" . $file)) {
                              if($recursive) {
                                    $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
                              }
                              $file = $directory . "/" . $file;
                              $array_items[] = preg_replace("/\/\//si", "/", $file);
                        }
                  }
            }
            closedir($handle);
      }
      return $array_items;
}
$files = directoryToArray("upload/", false);
echo "<select name='folder' value='folder'>";
for ($i = 0; $i < count($files); $i++) {
      echo '<option value="'. $files[$i] .'">' . $files[$i] . '</option>';
}
echo '</select>';
?>

<hr width="<?PHP echo HRWIDTH ?>" align="left">
<h2>Beheer audio</h2>
<p>Hieronder kan een mp3 bestand geselecteerd worden om te uploaden, max 10mb.<br /><br /></p>
<?php
echo "<form method='post' enctype='multipart/form-data'><input type='file' name='file' width='300'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='upload1' value='Upload'> </form>";
$map = $_POST["folder"];  //map waar die in komt (wel chmod permision 777 geven
if(isset($_POST['upload1'])) {
  if($_FILES['file']['size'] <= 1024*10000) {  
    // Controle of het bestand groter is als 10 mb (tussen de [''] van $_FILES vul je de naam van het veld in)
    $extension = array();
    $extension[] = "audio/mp3";  // zet de extensions (vb: .jpg) in de array..  
     $extension[] = "audio/mpeg"; // altijd met audio/ ervoor en zonder punt
    // Let op .jpg kan hij verschillend zien (bv: ook als .pjpg) zoek dus op internet een lijst anders krijg je vaak errors..
    if(in_array($_FILES['file']['type'], $extension)) {
    // Controle of hij een bestand met de juiste extension heeft geupload
      if(is_uploaded_file($_FILES['file']['tmp_name'])) {  //Contorleren of hij is geupload
        move_uploaded_file($_FILES['file']['tmp_name'], $map.$_FILES['file']['name']);
        // eerst geef je de file aan met de tmp naam waar die tijdelijk staat op geslagen, daarna de mapnaam en de filenaam met een punt er tussen  
        echo "<p><span style=\"color:#F00\">Bestand succesvol geupload..</span></p>";
      } else {
        echo "<p><span style=\"color:#F00\">Er is iets mis gegaan bij het uploaden, probeer het nogmaals</span></p>";
      }
    } else {
      echo "<p><span style=\"color:#F00\">Het bestand heeft een verkeerde extensie</span></p>";
    }
  } else {
    echo "<p><span style=\"color:#F00\">Het bestand moet kleiner zijn dan 10 mb</span></p>";
  }
} else {
// Hier je formulier (deze is zichtbaar wanneer er niet op submit is gedrukt..)
}
?>


Thanks.
0
 
peps03Author Commented:
Thanks Amar,

The script is uploading, but to the same directory it is in. It isn't uploading to the selected dir from the dropdown list.. you know what could be the problem?
0
 
peps03Author Commented:
Amar,

I maybe saw a "/" we forgot. i changed this: echo '<option value="'. $files[$i] .'">' . $files[$i] . '</option>';
to: echo '<option value="'. $files[$i] .'/">' . $files[$i] . '/</option>';

but it still doesn't work. it uploads, but not to the selected dir..

0
 
Amar BardoliwalaCommented:
Hello peps03,

Actual problem was with form tag.. In previous code form was not including combo box to select directories. so because of that we were not getting values of directory selected. Now I have changed code to include directory selection in <form> tag.

Please use following code. It should work fine now.


<?php

function directoryToArray($directory, $recursive) {
      $array_items = array();
      if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
                        if (is_dir($directory. "/" . $file)) {
                              if($recursive) {
                                    $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
                              }
                              $file = $directory . "/" . $file;
                              $array_items[] = preg_replace("/\/\//si", "/", $file);
                        }
                  }
            }
            closedir($handle);
      }
      return $array_items;
}
$files = directoryToArray("upload/", false);
echo "<form method='post' enctype='multipart/form-data'>";
echo "<select name='folder' value='folder'>";
for ($i = 0; $i < count($files); $i++) {
      echo '<option value="'. $files[$i] .'/">' . $files[$i] . '/</option>';
}
echo '</select>';
?>

<hr width="<?PHP echo HRWIDTH ?>" align="left">
<h2>Beheer audio</h2>
<p>Hieronder kan een mp3 bestand geselecteerd worden om te uploaden, max 10mb.<br /><br /></p>
<?php
echo "<input type='file' name='file' width='300'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='upload1' value='Upload'> </form>";
$map = $_POST["folder"];  //map waar die in komt (wel chmod permision 777 geven

if(isset($_POST['upload1'])) {
  if($_FILES['file']['size'] <= 1024*10000) {  
    // Controle of het bestand groter is als 10 mb (tussen de [''] van $_FILES vul je de naam van het veld in)
    $extension = array();
    $extension[] = "audio/mp3";  // zet de extensions (vb: .jpg) in de array..  
     $extension[] = "audio/mpeg"; // altijd met audio/ ervoor en zonder punt
    // Let op .jpg kan hij verschillend zien (bv: ook als .pjpg) zoek dus op internet een lijst anders krijg je vaak errors..
    if(in_array($_FILES['file']['type'], $extension)) {
      print $map.$_FILES['file']['name'];
    // Controle of hij een bestand met de juiste extension heeft geupload
      if(is_uploaded_file($_FILES['file']['tmp_name'])) {  //Contorleren of hij is geupload
        move_uploaded_file($_FILES['file']['tmp_name'], $map.$_FILES['file']['name']);
        // eerst geef je de file aan met de tmp naam waar die tijdelijk staat op geslagen, daarna de mapnaam en de filenaam met een punt er tussen  
        echo "<p><span style=\"color:#F00\">Bestand succesvol geupload..</span></p>";
      } else {
        echo "<p><span style=\"color:#F00\">Er is iets mis gegaan bij het uploaden, probeer het nogmaals</span></p>";
      }
    } else {
      echo "<p><span style=\"color:#F00\">Het bestand heeft een verkeerde extensie</span></p>";
    }
  } else {
    echo "<p><span style=\"color:#F00\">Het bestand moet kleiner zijn dan 10 mb</span></p>";
  }
} else {
// Hier je formulier (deze is zichtbaar wanneer er niet op submit is gedrukt..)
}
?>



Thanks.

Amar.
0
 
peps03Author Commented:
Thank you very much Amar!! its working great! Thanks a lot!

One final question, according to the php.ini file of my hosting provider files to 12 mb can be uploaded.
so i changed: if($_FILES['file']['size'] <= 1024*10000) {  
to: if($_FILES['file']['size'] <= 1024*1024*12) {  

but when a file / mp3 is bigger than 12mb it doesn't give the error message:
echo "<p><span style=\"color:#F00\">Het bestand moet kleiner zijn dan 12 mb</span></p>";
it doesn't show anything, just a blank page..
how can this be fixed?
0
 
Amar BardoliwalaCommented:
Hello peps03,

You also need to check post_max_size and upload_max_filesize and both should have values of 12 MB.

Please contact your server administrator for that.

I check it on my local and conditions is working fine.

Thanks.

Amar.
0
 
peps03Author Commented:
post_max_size = 16
 and upload_max_filesize = 12 mb

but i'll check i out!

thanks for your great work! it really helped me out!
0
 
Amar BardoliwalaCommented:
Hello peps03,

one more thing you might need to consider is max_execution_time. Make sure that it is enough to upload this much big file..

It might also be affecting your execution of this page.

Thanks.

Amar.
0
 
peps03Author Commented:
Thanks! maybe that is the problem!

it is set to 30 in the php.ini.. is that 30sec?

and if so, it takes more than 30 sec, can i increase this?
0
 
Amar BardoliwalaCommented:
Hello peps03,

Yes. you can increase it to whatever value you want to.

setting its value to 0 will execute page for infinite time means it will execute page untill its execution finishes.

you can use ini_set('max_execution_time', 0); in your page.

Thanks.

Amar.
0
 
peps03Author Commented:
Hmm an upload of 13mb did exceed the 30sec, but changing 30 to 0 or 60 didn't do the trick

it still doesn't show the error message.

i also turned on the error reporting, and got this:
Notice: Undefined index: folder in /*******/audio_upload_form_2.php on line 49 =

$map = $_POST["folder"];  //map waar die in komt (wel chmod permision 777 geven

does this maybe have something to do with it?

i also tried this:
ini_set('upload_max_filesize', 1024*1024*16); so now the post_max_size & upload_max_filesize are both 16mb, and left our limit like this:
  if($_FILES['file']['size'] <= 1024*1024*12) {  

so we have a 4mb margin, still no error message...
0
 
Amar BardoliwalaCommented:
Hello peps03,

It is strange that you are not getting any error message.. Can you please post your current code? I would like to see it and test on my local to see if it works for me..

regarding the notice it seems to say that it is not getting $_POST["folder"];

you can check it using print_r($_POST);

Please check it to make sure you are getting selected folder value..

Thanks.

Amar.
0
 
peps03Author Commented:
Thanks for your extra work Amar!

It is uploading to the correct folder.

problems:
     - no error message if uploaded file is bigger tahn 12mb
     - the notice: Notice: Undefined index: folder in /***/audio_upload_form_2.php on line 50
<?php

ini_set('display_errors', 'On');
error_reporting(E_ALL);

ini_set('max_execution_time', 60);
ini_set('post_max_size', 1024*1024*16);
print_r($_POST);
function directoryToArray($directory, $recursive) {
      $array_items = array();
      if ($handle = opendir($directory)) {
            while (false !== ($file = readdir($handle))) {
                  if ($file != "." && $file != "..") {
                        if (is_dir($directory. "/" . $file)) {
                              if($recursive) {
                                    $array_items = array_merge($array_items, directoryToArray($directory. "/" . $file, $recursive));
                              }
                              $file = $directory . "/" . $file;
                              $array_items[] = preg_replace("/\/\//si", "/", $file);
                        } 
                  }
            }
            closedir($handle);
      }
	  sort ($array_items);
      return $array_items;
}
$files = directoryToArray("upload/", false);
echo "<form method='post' enctype='multipart/form-data'>";
echo "<select name='folder' value='folder'>";
for ($i = 0; $i < count($files); $i++) {
      echo '<option value="'. $files[$i] .'/">' . $files[$i] . '/</option>';
}
echo '</select>';
?>

<hr width="<?PHP echo HRWIDTH ?>" align="left">
<h2>Beheer audio</h2>
<p>Hieronder kan een mp3 bestand geselecteerd worden om te uploaden, max 12mb.<br /><br /></p>
<?php
echo "<input type='file' name='file' width='300'>&nbsp;&nbsp;&nbsp;&nbsp;<input type='submit' name='upload1' value='Upload'> </form>";
$map = $_POST["folder"]; // map waar de upload in komt, bepaald door het bovenstaande drop-down-menu
if(isset($_POST['upload1'])) { 
  if($_FILES['file']['size'] <= 1024*1024*12) {  
    // Controle of het bestand groter is als 10 mb (tussen de [''] van $_FILES vul je de naam van het veld in) 
    $extension = array(); 
    $extension[] = "audio/mp3";  // zet de extensions (vb: .jpg) in de array..  
     $extension[] = "audio/mpeg"; // altijd met audio/ ervoor en zonder punt 
    // Let op .jpg kan hij verschillend zien (bv: ook als .pjpg) zoek dus op internet een lijst anders krijg je vaak errors.. 
    if(in_array($_FILES['file']['type'], $extension)) { 
      print $map.$_FILES['file']['name'];
    // Controle of hij een bestand met de juiste extension heeft geupload 
      if(is_uploaded_file($_FILES['file']['tmp_name'])) {  //Contorleren of hij is geupload 
        move_uploaded_file($_FILES['file']['tmp_name'], $map.$_FILES['file']['name']); 
        // eerst geef je de file aan met de tmp naam waar die tijdelijk staat op geslagen, daarna de mapnaam en de filenaam met een punt er tussen  
        echo "<p><span style=\"color:#009900\">Bestand succesvol geupload..</span></p>"; 
      } else { 
        echo "<p><span style=\"color:#F00\">Er is iets mis gegaan bij het uploaden, probeer het nogmaals</span></p>"; 
      } 
    } else { 
      echo "<p><span style=\"color:#F00\">Het bestand heeft een verkeerde extensie</span></p>"; 
    } 
  } else { 
    echo "<p><span style=\"color:#F00\">Het bestand moet kleiner zijn dan 12 mb</span></p>"; 
  } 
} else { 
// Hier je formulier (deze is zichtbaar wanneer er niet op submit is gedrukt..) 
} 
?>

Open in new window

0
 
Amar BardoliwalaCommented:
Hello peps03,

replace following line

$map = $_POST["folder"]; // map waar de upload in komt, bepaald door het bovenstaande drop-down-menu

with following code to remove notice..

if(isset($_POST["folder"])){
$map = $_POST["folder"]; // map waar de upload in komt, bepaald door het bovenstaande drop-down-menu
}

Thanks.

Amar.
0
 
peps03Author Commented:
Thanks Amar! That did the trick! Nice!

But i still can't figure out why i don't get the error message if the mp3 is bigger than 12mb...

Are you getting the error message?
0
 
Amar BardoliwalaCommented:
Hello pes03,

I checked and executed your code on my local by uploading file of size 27 MB and it gives me error "Het bestand moet kleiner zijn dan 12 mb".

So it works perfectly fine on my local. So according to me everything is fine regarding the code.

you just need to find what is going wrong on server. Please wait for sometime as files above 10 MB might take some time.

According to me you can do following

1. change ini_set('max_execution_time', 60); to ini_set("max_execution_time", "0");
2. first check condition for files with lower size.. like 1 MB and see if it works fine for it.
3. if it works fine for lower size file than change codition again for 12 MB and wait for some time may be 5 - 10 minutes... to see if it works... because file uploading will depend on your internet connection.
you can check normal time of uploading 12MB file using ftp.. if on ftp it takes 5 min.. using our code it might take 5 - 10 minutes..

this it the kind of work that requires some patience..

Hopefully this will help you.


Thanks.

Amar.
0
 
peps03Author Commented:
Thanks again! i'll  give it a try!

.. I know i'm waiting long enough because google chrome shows the upload % in the left corner.

thanks a lot!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 13
  • 11
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now