Closed Thread Icon

Topic awaiting preservation: how to upload file by php? (Page 1 of 1) Pages that link to <a href="https://ozoneasylum.com/backlink?for=12808" title="Pages that link to Topic awaiting preservation: how to upload file by php? (Page 1 of 1)" rel="nofollow" >Topic awaiting preservation: how to upload file by php? <span class="small">(Page 1 of 1)</span>\

 
sweetii
Nervous Wreck (II) Inmate

From:
Insane since: Jul 2003

posted posted 07-04-2003 17:40

I'm a beginer and I don't know much about php but I'd like to know how to upload file using php. Can anyone show me how? Thanks

butcher
Paranoid (IV) Inmate

From: New Jersey, USA
Insane since: Oct 2000

posted posted 07-04-2003 18:16

The PHP manual is always a great place to start.

-Butcher-

tgullett
Obsessive-Compulsive (I) Inmate

From: Lexington, Kentucky, USA
Insane since: Jun 2003

posted posted 07-04-2003 18:31

<CODE>
class UploadFile
{

function UploadFile()
{
$this->UpPath = "/home/virtual/site165/fst/var/www/uploads";
$this->MaxSize = 24576;
$this->MaxWidth = 170;
$this->MaxHeight = 220;

$this->RegTypes = array(
"image/gif" => ".gif",
"image/pjpeg" => ".jpg, .jpeg",
"image/jpeg" => ".jpg, .jpeg",
"image/tif" => ".tif, .tiff",
"image/png" => ".png",
);

$this->AllowedTypes = array("image/gif","image/pjpeg","image/jpeg","image/tif","image/png");
}

// The params can be reset and if they are NULL,
// the defaults will be kept for that param.
function NewParams($up_path,$max_size,$max_width,$max_height,$reg_types,$allowed_types)
{
if($up_path) {$this->UpPath = $up_path;}
if($max_size) {$this->MaxSize = $max_size;}
if($max_width) {$this->MaxWidth = $max_width;}
if($max_height) {$this->MaxHeight = $max_height;}

if($reg_types) {$this->RegTypes = $reg_types;}
if($allowed_types) {$this->AllowedTypes = $allowed_types;}
}


function ValidateFile($file_name)
{

global $_FILES;

$this->start_error = "\n<b>Error:</b>\n<ul>";

// Was there a file sent?
if (!$file_name) {

$this->error .= "\n<li>Please select a file to upload</li>";

// Check to see if the file is able to be uploaded given our perameters.
} else {

// If the file size is too big...
if ($_FILES["$file_name"]["error"] == 2) {
$this->error .= "\n<li>Your image size can be no larger than " . $this->MaxSize/1024 . "KB</li>";
} else {

// If the file type is NOT allowed...
if (!in_array($_FILES["$file_name"]["type"],$this->AllowedTypes)) {
$this->error .= "\n<li>The file that you uploaded: <b>".$_FILES["$file_name"]["name"]."</b> was of a type that is not allowed, you are only allowed to upload files of the types:\n<ul>";
while ($this->type = current($this->AllowedTypes)) {
$this->error .= "\n<li>" . $this->RegTypes[$type] . " (" . $this->type . ")</li>";
next($this->AllowedTypes);
}
$this->error .= "\n</ul>";
}

}

// If the file type is allowed AND the file size is not too big...
if (in_array($_FILES["$file_name"]["error"],$this->AllowedTypes) && $_FILES["$file_name"]["error"] != 2) {

$this->size = GetImageSize($_FILES["$file_name"]["tmp_name"]);
list($foo,$this->width,$bar,$this->height) = explode("\"",$this->size[3]);

if ($this->width > $this->MaxWidth) {
$this->error .= "\n<li>Your image should be no wider than " . $this->MaxWidth . " Pixels</li>";
}

if ($this->height > $this->MaxHeight) {
$this->error .= "\n<li>Your image should be no higher than " . $this->MaxHeight . " Pixels</li>";
}

}

if ($this->error) {
$this->error = $this->start_error . $this->error . "\n</ul>";
return false;
} else {
return true;
}
}
}

// Return the name of the file that was uploaded
function ShowFile()
{
return $this->the_file_name;
}

// Upload the file.
function Upload($file_name)
{

// Make sure we can upload the file
if ($this->ValidateFile($file_name)) {
global $LOGIN_INFO,$_FILES;

$SQLJunk = new MyDatabase;
$SQLJunk->Connect();

$this->x = false;

while ($this->x == false) {

// Make up a file name between 7 and 11 characters.
$this->the_file_name = PassGen(7,11);

// Get the extension of the file (ie. GIF or JPG) and add it to the file name.
$this->ext = explode("/",$_FILES["$file_name"]["type"]);

if($this->ext[1] == "pjpeg") {
$this->ext[1] = "jpg";
}

$this->the_new_file_name = $this->the_file_name.".".$this->ext[1];

$this->result = @mysql_query("SELECT image_name FROM images WHERE image_name='".$this->the_new_file_name."'");

if (!$this->row = mysql_fetch_array($this->result)) {
mysql_query("INSERT INTO images (login_id,image_name) VALUES ('".$LOGIN_INFO["login_id"]."','".$this->the_new_file_name."')") or die("Could not add pic name ($this->the_file_name) to DB<br><br>".mysql_error());
$this->x = true;
}
}

if (!@copy($_FILES["$file_name"]["tmp_name"], $this->UpPath . "/" . $this->the_new_file_name)) {
$this->error = "ERROR: An error has taken place while trying to upload the file. Please email E-link Design at info@elinkdesign.com and say that the file that could not be uploaded is: ".$this->the_file_name;
return false;
} else {
return true;
}

$SQLJunk->Disconnect();
} else {
return false;
}
}

// Return any Errors
function ReturnErrors()
{
return $this->error;
}

// Return MaxSize of file
function GetSize()
{
return $this->MaxSize;
}

}
</CODE>

My Buddy Sam wrote this for one of our projects.

/TCG

Thomas Chase Gullett
tgullett@elinkdesign.com

Emperor
Maniac (V) Mad Scientist with Finglongers

From: Cell 53, East Wing
Insane since: Jul 2001

posted posted 07-04-2003 18:59

:FAQ: How do I upload files using PHP?

___________________
Emps

FAQs: Emperor

« BackwardsOnwards »

Show Forum Drop Down Menu