It appears you have not yet registered with DEVPPL. To register please click here... (it's fast, easy and free!)

Forum

Log In Sponsors
Board index Programming PHP and MySQL Forum

yet another code

Moderator: Malcolm

yet another code

Postby Phate on Wed Mar 09, 2005 12:50 am

Malcom!!!(or anyone) I need your help again!

I need of another code....

I want for the people that visit site to be able to upload a file, add a description of the file, and browse all of the files that have been uploaded.

Is there a way for me to do that?
Web-Developing since '03
Image
User avatar
Phate
500+ Club
 
Posts: 826
Joined: Sun Nov 21, 2004 4:12 am
Location: 127.0.0.1

Postby Malcolm on Wed Mar 09, 2005 3:50 am

Here's your quick code, note that most PHP/Apache configs only allow 2mb files to be uploaded and with this script people can upload anything to pretty much anywhere if they know how to mess with file names:

Code: Select all
Upload a file!<br />
<form method='post' action='?post=TRUE' enctype='multipart/form-data'>
<input type="file" name="file" /><br />
<input type='submit' />
</form>
<br />
<hr />

<?php
$dir = "uploads";
function read_dir($dir)
{
   $list = array();
   if (is_dir($dir)) {
      if ($handle = opendir($dir)) {
         while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                  $list[] = $dir."/".$file;
            }
         }
      }
      closedir($handle);
   }

   return $list;
}

if(isset($_GET['post'])){
   if(copy($_FILES['file']['tmp_name'], $dir."/".$_FILES['file']['name'])){
      echo "Upload Success!<hr /><br />";
   }else{
      echo "Error on upload!<hr /><br />";   
   }
}

?>
Files:<br />
<?php
$list = read_dir($dir);
foreach($list as $key => $val){
   echo $key.": <a href='".$val."'>".$val."</a><br />\n";
}
?>
Image
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby Phate on Wed Mar 09, 2005 7:39 pm

1 more thing....
Is there any way to limit the upload to a certian extension? (like . exe)
Web-Developing since '03
Image
User avatar
Phate
500+ Club
 
Posts: 826
Joined: Sun Nov 21, 2004 4:12 am
Location: 127.0.0.1

Postby Malcolm on Thu Mar 10, 2005 3:26 am

replace:
Code: Select all
if(isset($_GET['post'])){
   if(copy($_FILES['file']['tmp_name'], $dir."/".$_FILES['file']['name'])){
      echo "Upload Success!<hr /><br />";
   }else{
      echo "Error on upload!<hr /><br />";   
   }
}

with this:
Code: Select all
$exts = array('mp3', 'txt', 'mpeg');
if(isset($_GET['post'])){
   $f_ext = split(".", $_FILES['file']['tmp_name']);
   $f_ext = $f_ext[sizeof($f_ext)-1];
   if(in_array($f_ext, $exts)){
      if(copy($_FILES['file']['tmp_name'], $dir."/".$_FILES['file']['name'])){
         echo "Upload Success!<hr /><br />";
      }else{
         echo "Error on upload!<hr /><br />";   
      }
   }else{
      echo "Invalid file.<br />";
   }
}


:)
Image
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby Phate on Thu Mar 10, 2005 3:43 am

awesome!!
thanks man!


edit:::

once i made it and uploaded it, i get a 500 error when i try and view it. Any suggestions?
it is located at http://zbrooks.com/upload.php
Web-Developing since '03
Image
User avatar
Phate
500+ Club
 
Posts: 826
Joined: Sun Nov 21, 2004 4:12 am
Location: 127.0.0.1

Postby webmaster on Thu Mar 10, 2005 7:12 am

Try another server if you can.
Make sure to check out our TNX Review and Link Vault Review
User avatar
webmaster
Site Admin
 
Posts: 2695
Joined: Tue Aug 17, 2004 1:07 pm
Location: Sweden

Postby Malcolm on Thu Mar 10, 2005 4:48 pm

Looks like the PHP install you have on there may have a problem. The code was tested on my home server (Linux Apache 1.3/PHP 4) and worked fine, though looks very ugly ;)
Image
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby Phate on Fri Mar 11, 2005 12:38 am

I am a big noob with PHP... is there any more code i need to add for it to work?
Web-Developing since '03
Image
User avatar
Phate
500+ Club
 
Posts: 826
Joined: Sun Nov 21, 2004 4:12 am
Location: 127.0.0.1

Postby Malcolm on Fri Mar 11, 2005 3:21 am

My bad, there were some logic errors in there:
Code: Select all
Upload a file!<br />
<form method='post' action='?post=TRUE' enctype='multipart/form-data'>
<input type="file" name="file" /><br />
<input type='submit' />
</form>
<br />
<hr />

<?php
$dir = "uploads";
function read_dir($dir)
{
   $list = array();
   if (is_dir($dir)) {
      if ($handle = opendir($dir)) {
         while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != "..") {
                  $list[] = $dir."/".$file;
            }
         }
      }
      closedir($handle);
   }

   return $list;
}

$exts = array('mp3', 'txt', 'mpeg');
if(isset($_GET['post'])){
   $f_ext = split("\.", $_FILES['file']['name']);
   $f_ext = $f_ext[sizeof($f_ext)-1];
   echo "<hr />* ".$f_ext."*<hr />";
   if(in_array($f_ext, $exts)){
      if(copy($_FILES['file']['tmp_name'], $dir."/".$_FILES['file']['name'])){
         echo "Upload Success!<hr /><br />";
      }else{
         echo "Error on upload!<hr /><br />";   
      }
   }else{
      echo "Invalid file.<br />";
   }
}

?>
Files:<br />
<?php
$list = read_dir($dir);
foreach($list as $key => $val){
   echo $key.": <a href='".$val."'>".$val."</a><br />\n";
}
?>
Image
User avatar
Malcolm
100+ Club
 
Posts: 198
Joined: Thu Oct 07, 2004 9:53 pm
Location: Ontario, Canada

Postby Phate on Fri Mar 11, 2005 11:11 pm

I am still gettting a 500 error.... any other possibilities of what could be going wrong?
Web-Developing since '03
Image
User avatar
Phate
500+ Club
 
Posts: 826
Joined: Sun Nov 21, 2004 4:12 am
Location: 127.0.0.1

Next

Who is online

Users browsing this forum: No registered users and 2 guests