Help with upload script!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pimpinjuice
Forum Newbie
Posts: 2
Joined: Mon Jul 04, 2005 8:03 pm

Help with upload script!

Post by pimpinjuice »

Hello fellow coders :D
I am making a system where people who register can upload files and what not. I found a script that works perfect but wont upload files over 2 mb. Even if I change the file limit, which is 190 mb. Here is the script:

Code: Select all

<?php

require 'db_connect.php';

// require our database connection
// which also contains the check_login.php
// script. We have $logged_in for use.

if ($logged_in == 0) {
	die('<style>body {background-image:url(http://gmuploads.servegame.com/background.png);font-family: Courier New;font-size: 12px;color: FFFFFF;}</style>Welcome to the Garrys Mod Upload website. We provide free hosting for all your GM files.Sorry, but before you can start uploading files you need to <a href=login.php>login</a>, you can <a href=Register.php>register</a> free.');
}


// show content

$db_object->disconnect();
// when you are done.

?>
<?php
//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//   You may change maxsize, and allowable upload file types.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
//Mmaximum file size. You may increase or decrease.
$MAX_SIZE = 200000000;
                            
//Allowable file Mime Types. Add more mime types if you want
$FILE_MIMES = array('image/jpeg','image/jpg','image/gif'
                   ,'image/png','application/msword');

//Allowable file ext. names. you may add more extension names.            
$FILE_EXTS  = array('.zip','.jpg','.png','.gif','.rar','.sav','.dem','.avi','.bsp'); 

//Allow file delete? no, if only allow upload only
$DELETABLE  = false;                               


//vvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvvv
//   Do not touch the below if you are not confident.
//^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
/************************************************************
 *     Setup variables
 ************************************************************/
$site_name = $_SERVER['HTTP_HOST'];
$url_dir = "http://".$_SERVER['HTTP_HOST'].dirname($_SERVER['PHP_SELF'].$_SESSION['username']);
$url_this =  "http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'];

$upload_dir = $_SESSION['username'].'/';
$upload_url = $url_dir.'/'.$upload_dir;
$message ="";

/************************************************************
 *     Create Upload Directory
 ************************************************************/
if (!is_dir($_SESSION['username'])) {
  if (!mkdir($upload_dir))
  	die ("upload_files directory doesn't exist and creation failed");
  if (!chmod($upload_dir,0755))
  	die ("change permission to 755 failed.");
}

/************************************************************
 *     Process User's Request
 ************************************************************/
if ($_REQUEST[del] && $DELETABLE)  {
  $resource = fopen("log.txt","a");
  fwrite($resource,date("Ymd h:i:s")."DELETE - $_SERVER[REMOTE_ADDR]"."$_REQUEST[del]\n");
  fclose($resource);
  
  if (strpos($_REQUEST[del],"/.")>0);                  //possible hacking
  else if (strpos($_REQUEST[del],$upload_dir) === false); //possible hacking
  else if (substr($_REQUEST[del],0,6)==$upload_dir) {
    unlink($_REQUEST[del]);
    print "<script>window.location.href='$url_this?message=Deleted successfully.'</script>";
  }
}
else if ($_FILES['userfile']) {
  $resource = fopen("log.txt","a");
  fwrite($resource,date("Ymd h:i:s")."UPLOAD - $_SERVER[REMOTE_ADDR]"
            .$_FILES['userfile']['name']." "
            .$_FILES['userfile']['type']."\n");
  fclose($resource);

	$file_type = $_FILES['userfile']['type']; 
  $file_name = $_FILES['userfile']['name'];
  $file_ext = strtolower(substr($file_name,strrpos($file_name,".")));

  //File Size Check
  if ( $_FILES['userfile']['size'] > $MAX_SIZE) 
     $message = "The file size is over 190MB.";
  //File Type/Extension Check
  else if (!in_array($file_type, $FILE_MIMES) 
          && !in_array($file_ext, $FILE_EXTS) )
     $message = "Sorry, $file_name($file_type) is not allowed to be uploaded.";
  else
     $message = do_upload($upload_dir, $upload_url);
  
  print "<script>window.location.href='$url_this?message=$message'</script>";
}
else if (!$_FILES['userfile']);
else 
	$message = "Invalid File Specified.";

/************************************************************
 *     List Files
 ************************************************************/
$handle=opendir($upload_dir);
$filelist = "";
while ($file = readdir($handle)) {
   if(!is_dir($file) && !is_link($file)) {
      $filelist .= "<a href='$upload_dir$file'>"."http://gmuploads.servegame.com/".$_SESSION['username']."/".$file."</a>";
      if ($DELETABLE)
        $filelist .= " <a href='?del=/$upload_dir$file' title='delete'>Delete File</a>";
      $filelist .= "<sub><small><small><font color=grey>  ".date("d-m H:i", filemtime($upload_dir.$file))
                   ."</font></small></small></sub>";
$urlp1='"[url=http://gmuploads.servegame.com/';
$urlp2=']http://gmuploads.servegame.com/';
$urlp3='[/url]"';
$urlp4=$upload_dir;
$urlp5=$file;
$imgp1='"[img]http://gmuploads.servegame.com/';
$imgp2='[/img]"';
$imgp3=$upload_dir;
$imgp4=$file;
$filelist .= " <a href='javascript:void(document.form1.code.value=".$urlp1.$urlp4.$urlp5.$urlp2.$urlp4.$urlp5.$urlp3.")'><img alt='Get Forum Link Code' border=0 src=link.png></a>";
$filelist .= " <a href='javascript:void(document.form1.code.value=".$imgp1.$imgp3.$imgp4.$imgp2.")'><img alt='Get Forum Image Code' border=0 src=image.png></a>";
      $filelist .="<br>";
   }
}

function do_upload($upload_dir, $upload_url) {

	$temp_name = $_FILES['userfile']['tmp_name'];
	$file_name = $_FILES['userfile']['name']; 
  $file_name = str_replace("\\","",$file_name);
  $file_name = str_replace("'","",$file_name);
	$file_path = $upload_dir.$file_name;

	//File Name Check
  if ( $file_name =="") { 
  	$message = "Invalid File Name Specified";
  	return $message;
  }

  $result  =  move_uploaded_file($temp_name, $file_path);
  if (!chmod($file_path,0777))
   	$message = "error chmoding 777.";
  else
    $message = ($result)?"$file_name was uploaded successfully." :
     	      "Something is wrong with uploading a file.";
  return $message;
}

?>
<style>body {background-image:url(http://gmuploads.servegame.com/background.png);font-family: Courier New;font-size: 12px;color: FFFFFF;}</style>
<center>
   <font color=red><?=$_REQUEST[message]?></font>
   <br>
   <form name="upload" id="upload" ENCTYPE="multipart/form-data" method="post">
     Upload File <input type="file" id="userfile" name="userfile">
     <input type="submit" name="upload" value="Upload">
   </form>
   
   <br><b>My Files</b>
   <hr width=70%>
   <?=$filelist?>
   <hr width=70%>
Code<br>
<form name=form1><textarea rows=20 cols=80 name=code>Coding is outputed here.</textarea></form>
<a href="logout.php">Logout</a>
</center>
I get the error message "error chmoding 777." whenever the file is over 2 mb. Please help!
Thanks in advance!

JCART | Please use

Code: Select all

tags when posting php code. Review [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
lostboy
Forum Contributor
Posts: 329
Joined: Mon Dec 30, 2002 8:12 pm
Location: toronto,canada

Post by lostboy »

The php.ini file has a default limit of 2Mb. That is the value you need to change. If you don't have access to the ini file, then try using the ini_set("upload_max_filesize", value); to increase it. Note that this method only works for the script and you may need to talk to your host about increase the size on amore permanent basis
pimpinjuice
Forum Newbie
Posts: 2
Joined: Mon Jul 04, 2005 8:03 pm

Post by pimpinjuice »

You totally rock! I am actually my host :P
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you might also need to up the post data size in your php.ini
Post Reply