Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
how do I limit file type upload and size in this script?
Old 11-18-2010, 03:00 AM how do I limit file type upload and size in this script?
Junior Talker

Posts: 3
Trades: 0
below is php script that has to do with uploading files, I want only images to be uploaded and should not be more than 350kb in size


<?php
/**
* Elgg file browser uploader/edit action
*
* @package ElggFile
* @author Curverider Ltd
* @copyright Curverider Ltd 2008-2010
* @link http://elgg.com/
*/

global $CONFIG;

gatekeeper();

// Get variables
$title = get_input("title");
$desc = get_input("description");
$access_id = (int) get_input("access_id");
$container_guid = (int) get_input('container_guid', 0);
if ($container_guid == 0) {
$container_guid = get_loggedin_userid();
}
$guid = (int) get_input('file_guid');
$tags = get_input("tags");

// check whether this is a new file or an edit
$new_file = true;
if ($guid > 0) {
$new_file = false;
}

if ($new_file) {
// must have a file if a new file upload
if (empty($_FILES['upload']['name'])) {
// cache information in session
$_SESSION['uploadtitle'] = $title;
$_SESSION['uploaddesc'] = $desc;
$_SESSION['uploadtags'] = $tags;
$_SESSION['uploadaccessid'] = $access_id;

register_error(elgg_echo('file:nofile'));
forward($_SERVER['HTTP_REFERER']);
}

$file = new FilePluginFile();
$file->subtype = "file";

// if no title on new upload, grab filename
if (empty($title)) {
$title = $_FILES['upload']['name'];
}

} else {
// load original file object
$file = get_entity($guid);
if (!$file) {
register_error(elgg_echo('file:cannotload'));
forward($_SERVER['HTTP_REFERER']);
}

// user must be able to edit file
if (!$file->canEdit()) {
register_error(elgg_echo('file:noaccess'));
forward($_SERVER['HTTP_REFERER']);
}
}

$file->title = $title;
$file->description = $desc;
$file->access_id = $access_id;
$file->container_guid = $container_guid;

$tags = explode(",", $tags);
$file->tags = $tags;

// we have a file upload, so process it
if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) {

$prefix = "file/";

// if previous file, delete it
if ($new_file == false) {
$filename = $file->getFilenameOnFilestore();
if (file_exists($filename)) {
unlink($filename);
}

// use same filename on the disk - ensures thumbnails are overwritten
$filestorename = $file->getFilename();
$filestorename = elgg_substr($filestorename, elgg_strlen($prefix));
} else {
$filestorename = elgg_strtolower(time().$_FILES['upload']['name']);
}

$file->setFilename($prefix.$filestorename);
$file->setMimeType($_FILES['upload']['type']);
$file->originalfilename = $_FILES['upload']['name'];
$file->simpletype = get_general_file_type($_FILES['upload']['type']);

$file->open("write");
$file->write(get_uploaded_file('upload'));
$file->close();

$guid = $file->save();

// if image, we need to create thumbnails (this should be moved into a function)
if ($guid && $file->simpletype == "image") {
$thumbnail = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),60,60, true);
if ($thumbnail) {
$thumb = new ElggFile();
$thumb->setMimeType($_FILES['upload']['type']);

$thumb->setFilename($prefix."thumb".$filestorename);
$thumb->open("write");
$thumb->write($thumbnail);
$thumb->close();

$file->thumbnail = $prefix."thumb".$filestorename;
unset($thumbnail);
}

$thumbsmall = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),153,153, true);
if ($thumbsmall) {
$thumb->setFilename($prefix."smallthumb".$filestorename );
$thumb->open("write");
$thumb->write($thumbsmall);
$thumb->close();
$file->smallthumb = $prefix."smallthumb".$filestorename;
unset($thumbsmall);
}

$thumblarge = get_resized_image_from_existing_file($file->getFilenameOnFilestore(),600,600, false);
if ($thumblarge) {
$thumb->setFilename($prefix."largethumb".$filestorename );
$thumb->open("write");
$thumb->write($thumblarge);
$thumb->close();
$file->largethumb = $prefix."largethumb".$filestorename;
unset($thumblarge);
}
}
} else {
// not saving a file but still need to save the entity to push attributes to database
$file->save();
}

// make sure session cache is cleared
unset($_SESSION['uploadtitle']);
unset($_SESSION['uploaddesc']);
unset($_SESSION['uploadtags']);
unset($_SESSION['uploadaccessid']);

// handle results differently for new files and file updates
if ($new_file) {
if ($guid) {
system_message(elgg_echo("file:saved"));
add_to_river('river/object/file/create', 'create', get_loggedin_userid(), $file->guid);
} else {
// failed to save file object - nothing we can do about this
register_error(elgg_echo("file:uploadfailed"));
}

$container_user = get_entity($container_guid);
forward($CONFIG->wwwroot . "pg/file/" . $container_user->username);

} else {
if ($guid) {
system_message(elgg_echo("file:saved"));
} else {
register_error(elgg_echo("file:uploadfailed"));
}

forward($file->getURL());
}
jekwumoore is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-18-2010, 06:19 AM Re: how do I limit file type upload and size in this script?
Banned

Posts: 408
Name: mushget
Trades: 0
first you can use php function to read the bytes (binary data) into memory ,then you can get the file size.



2) I got some asp code like this,.You can change it to php
sFileName = oFile.FileName

extension=mid(sFileName,instrrev(sFileName,"."))
if extension <> "exe" or extension <> "zip" or extension <> "txt" then
mushget is offline
Reply With Quote
View Public Profile Visit mushget's homepage!
 
Old 11-18-2010, 09:43 AM Re: how do I limit file type upload and size in this script?
Junior Talker

Posts: 3
Trades: 0
I am a novice , I do not know much about coding.

what I need is for someone to look carefully into the php script I posted I find where put codes that will restricts uploads to only images
jekwumoore is offline
Reply With Quote
View Public Profile
 
Old 11-19-2010, 01:39 AM Re: how do I limit file type upload and size in this script?
Backslider's Avatar
Experienced Talker

Posts: 40
Trades: 0
This should work ok:

Code:
  $filetypes =  "/^\.(jpg|jpeg|gif|png){1}$/i";
  if(!$match = preg_match($filetypes, strrchr($_FILES['upload']['name'], '.'))) {
    register_error(elgg_echo('file:cannotload'));
    forward($_SERVER['HTTP_REFERER']);
  }

  if($_FILES['upload']['size'] / 1024 > 350) {
    register_error(elgg_echo('file:cannotload'));
    forward($_SERVER['HTTP_REFERER']);
  }
put it just above:

Code:
$file = new FilePluginFile();

Last edited by Backslider; 11-19-2010 at 01:52 AM..
Backslider is offline
Reply With Quote
View Public Profile
 
Old 11-19-2010, 07:17 AM Re: how do I limit file type upload and size in this script?
Junior Talker

Posts: 3
Trades: 0
Thanks man, you did a great job!! it worked!!
jekwumoore is offline
Reply With Quote
View Public Profile
 
Old 11-19-2010, 11:35 AM Re: how do I limit file type upload and size in this script?
Backslider's Avatar
Experienced Talker

Posts: 40
Trades: 0
Quote:
Originally Posted by jekwumoore View Post
Thanks man, you did a great job!! it worked!!
No problem

I'm not sure where or how exactly your error messages such as 'cannotload' are defined, so I just used one of those you posted. You may be able to find these and create your own custom error messages.
Backslider is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to how do I limit file type upload and size in this script?
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.19562 seconds with 12 queries