Current location: Hot Scripts Forums » Programming Languages » PHP » Help with PHP uploading


Help with PHP uploading

Reply
  #1 (permalink)  
Old 01-28-04, 12:19 PM
MGCJerry's Avatar
MGCJerry MGCJerry is offline
Newbie Coder
 
Join Date: Jun 2003
Location: FL
Posts: 75
Thanks: 0
Thanked 1 Time in 1 Post
Question Help with PHP uploading

I posted on another forum 12 days ago with this same problem, and I cant get it working...

Anyways, I'm working on a simple script to allow specific users to upload files but I cant seem to get the uploads to work. Ive tried taking code from other scripts on hotscripts, but nothing works, or they just have waaaaaaay to much bloat for me to sort through.

Here's my code so far, I already have my authentication system in the is_admin() function...

Thanks for any help...

PHP Code:

<?php


#######################################################
$dir "files";   // Directory that files are stored in
$max "8388608";   // Maximum File size in bytes
#######################################################

require_once("mainfile.php");
if(!
is_admin($admin)) {
    
header("Location: index.php");
    exit;
}

include(
"header.php");
$admin base64_decode($admin);
$admin explode(":"$admin);
$adname "$admin[0]";

global 
$filemsg;

switch(
$action) {

    default:
    
title("$sitename Files Page");
    
OpenTable();
    if (!
is_dir($dir) OR !$dir) {
        echo 
"<p><font color=\"#FFFF00\"><b>ERROR:</b><br>"
        
."The $dir directory doesn't exist or is invalid.</font></p>"
        
."<p>You must correct this error before you can use this think.";
    } else {
        echo 
"<p>Hello $adname,</p>"
        
."<p>Choose a function below to manage the files.</p>"
        
."<p align=\"center\">"
        
."[ <a href=\"$PHP_SELF?action=upload\">Upload</a> "
        
."| <a href=\"$PHP_SELF?action=list\">View Files</a> ]</p>";
    }
    
CloseTable();
    break;

    case 
"upload":
        
title("$sitename Files Upload");
        
OpenTable();
    if (
$uploaded == "Send Files") {
        
header("Location: $PHP_SELF?action=list");
//
// UPLOAD SCRIPT
//

// Nothing works :s

//    copy($_FILES['userfile']['tmp_name'], "/$dir");

//    move_uploaded_file($_FILES['userfile']['tmp_name'], "/$dir");



// END
    
} else {
        echo 
"<form action=\"$PHP_SELF?action=upload\" method=\"post\" enctype=\"multipart/form-data\">
      <input type=\"hidden\" name=\"MAX_FILE_SIZE\" value=\"
$max\">
      <b>Files to be Uploaded:</b><br><br>
      <input name=\"userfile[]\" type=\"file\">&nbsp;&nbsp;<input name=\"userfile[]\" type=\"file\"><br>
      <input name=\"userfile[]\" type=\"file\">&nbsp;&nbsp;<input name=\"userfile[]\" type=\"file\"><br>
      <input name=\"userfile[]\" type=\"file\">&nbsp;&nbsp;<input name=\"userfile[]\" type=\"file\"><br>
      <input name=\"userfile[]\" type=\"file\">&nbsp;&nbsp;<input name=\"userfile[]\" type=\"file\">
      <br><center>
      <input type=\"submit\" name=\"uploaded\" value=\"Send Files\">
      <input type=\"button\" value=\"Cancel\" onclick=\"javascript:history.go(-1)\">
      <input type=\"reset\" value=\"Reset Fields\">
      </center>
    </form>"
;
    }
    
    
CloseTable();
    break;

//Delete files
    
case "delete":
    
unlink("$dir/$file");
    
header("Location: $PHP_SELF?action=list");
    break;

    case 
"list":
    if (!
is_dir($dir) OR !$dir) {
        
header("Location: $PHP_SELF");
    }
    
title("$sitename Files List");
    
OpenTable();
    echo 
"<p>Here is a current list of files and directories in the &quot;$dir&quot; directory.</p>$filemsg";
    
$handle opendir($dir);
    
$count ="0";
    while (
$file readdir($handle)) {
        if (
$file != "." && $file != "..") {
            
$size filesize("$dir/$file");
            
$count++;
            if (
$size >= "1099511627776"){
                
$finalsize sprintf ("%01.7f",$size/"1099511627776") . " TB";
            } elseif (
$size >= "1073741824") {
                
$finalsize sprintf ("%01.5f",$size/"1073741824") . " GB";
            } elseif (
$size >= "1048576") {
                
$finalsize sprintf ("%01.3f",$size/"1048576") . " MB";
            } elseif (
$size >= "1024") {
                
$finalsize sprintf ("%01.3f",$size/"1024") . " KB";
            } else{
                
$finalsize $size " Bytes";
            }

            if (
$finalsize == "0 Bytes") {
                
$finalsize "<font color=\"#FFFF00\">Directory or 0 byte file.</b></font>";
            }
            echo 
"<b><a href=\"$dir/$file\">$file</a></b> : <a href=\"$PHP_SELF?action=delete&file=$file\">Delete File</a><br>
            
$finalsize<br><br>";
        }
    }
    
closedir($handle);
    echo 
"<p>$count files are present in this directory.</p>
    <center>[ <a href=\"
$PHP_SELF\">Go Back</a> ]</center><br>";
    
CloseTable();
    break;
}

echo 
"<br>";
OpenTable();
echo 
"<div class=\"tiny\" align=\"right\">Nuke Admin File Manager 1B<br>
Copyright &copy; 2004 MGCJerry<br>
<a href=\"http://www.2thextreme.org\">2 The Xtreme RPG</a><div>"
;
CloseTable();
echo 
"<br>";
include(
"footer.php");

?>
__________________
Nothing witty...
Reply With Quote
  #2 (permalink)  
Old 01-28-04, 05:39 PM
alfreds's Avatar
alfreds alfreds is offline
Newbie Coder
 
Join Date: Jun 2003
Posts: 38
Thanks: 0
Thanked 0 Times in 0 Posts
Hi,

I have written several upload scripts in this forum, they all worked for me and the response from others reported success. Maybe you can take a look at these scripts, go thourgh them and see the main things there.

This is the link on this forum:
http://www.programmingtalk.com/showt...?t=2382&page=2

Sincerely,
Alfreds
Reply With Quote
  #3 (permalink)  
Old 01-29-04, 03:30 PM
MGCJerry's Avatar
MGCJerry MGCJerry is offline
Newbie Coder
 
Join Date: Jun 2003
Location: FL
Posts: 75
Thanks: 0
Thanked 1 Time in 1 Post
Thanks for the link alfreds.

I managed to get it working. I used this code:

PHP Code:

        @copy($_FILES['userfile']['tmp_name'][0], "./$dir/".$_FILES['userfile']['name'][0]."");

        @
copy($_FILES['userfile']['tmp_name'][1], "./$dir/".$_FILES['userfile']['name'][1]."");
        @
copy($_FILES['userfile']['tmp_name'][2], "./$dir/".$_FILES['userfile']['name'][2]."");
        @
copy($_FILES['userfile']['tmp_name'][3], "./$dir/".$_FILES['userfile']['name'][3]."");
        @
copy($_FILES['userfile']['tmp_name'][4], "./$dir/".$_FILES['userfile']['name'][4]."");
        @
copy($_FILES['userfile']['tmp_name'][5], "./$dir/".$_FILES['userfile']['name'][5]."");
        @
copy($_FILES['userfile']['tmp_name'][6], "./$dir/".$_FILES['userfile']['name'][6]."");
        @
copy($_FILES['userfile']['tmp_name'][7], "./$dir/".$_FILES['userfile']['name'][7].""); 
While I'm sure this isnt the most efficient way of doing this, this seemed to be the most effective way of doing this for my purposes. Someone gave me the following snippet, but it would not work properly if someone didnt use the upload fields in the proper order.

PHP Code:

for ($x 0$x <= sizeof($_FILES); $x++) 


   @
copy($_FILES['userfile']['tmp_name'][$x], "./$dir/".$_FILES['userfile']['name'][$x].""); 

Thanks again.
__________________
Nothing witty...
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
PHP Dudes - Hi All stuart New Members & Introductions 4 05-03-04 01:22 PM
100 Web Templates & 10 PHP Scripts for sale! HostersUK.co.uk General Advertisements 0 01-10-04 12:31 AM
Realtime PHP Code Obfuscation barrywien General Advertisements 1 11-16-03 06:50 PM
How to convert php generated pages to SE friendly HTML or PHP(SE friendly) -CLASS 101 crippled PHP 1 11-16-03 06:37 AM


All times are GMT -5. The time now is 08:04 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.