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.

Coding Forum


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



Reply
Remove Form From Upload Script
Old 11-23-2009, 11:30 AM Remove Form From Upload Script
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
I have an upload script where I want to remove part of the code and make the rest of it still work, meaning make it dynamic. Can someone have a look and see if the upload part at the bottom could be removed but still have it do exactly it's function w/out the upload form.

Code:
<?php

if(isset($_POST['submit']))

{		

//make sure this directory is writable!

$path_thumbs = "../scripts/machform/data/form_1/files/";		

//the new width of the resized image, in pixels.

$img_thumb_width = 500; // 

$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)

//List of allowed extensions if extlimit = yes

$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");		

//the image -> variables

$file_type = $_FILES['vImage']['type'];

$file_name = $_FILES['vImage']['name'];

$file_size = $_FILES['vImage']['size'];

$file_tmp = $_FILES['vImage']['tmp_name'];

//check if you have selected a file.

if(!is_uploaded_file($file_tmp)){

echo "Error: Please select a file to upload!. <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

exit(); //exit the script and don't process the rest of it!

}

//check the file's extension

$ext = strrchr($file_name,'.');

$ext = strtolower($ext);

//uh-oh! the file extension is not allowed!

if (($extlimit == "yes") && (!in_array($ext,$limitedext))) {

echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";

exit();

}

//so, whats the file's extension?

$getExt = explode ('.', $file_name);

$file_ext = $getExt[count($getExt)-1];

//create a random file name

$rand_name = md5(time());

$rand_name= rand(0,999999999);

//the new width variable

$ThumbWidth = $img_thumb_width;



/////////////////////////////////

// CREATE THE THUMBNAIL //

////////////////////////////////



//keep image type

if($file_size){

if($file_type == "image/pjpeg" || $file_type == "image/jpeg"){

$new_img = imagecreatefromjpeg($file_tmp);

}elseif($file_type == "image/x-png" || $file_type == "image/png"){

$new_img = imagecreatefrompng($file_tmp);

}elseif($file_type == "image/gif"){

$new_img = imagecreatefromgif($file_tmp);

}

//list the width and height and keep the height ratio.

list($width, $height) = getimagesize($file_tmp);

//calculate the image ratio

$imgratio=$width/$height;

if ($imgratio>1){

$newwidth = $ThumbWidth;

$newheight = $ThumbWidth/$imgratio;

}else{

$newheight = $ThumbWidth;

$newwidth = $ThumbWidth*$imgratio;

}

//function for resize image.

if (function_exists(imagecreatetruecolor)){

$resized_img = imagecreatetruecolor($newwidth,$newheight);

}else{

die("Error: Please make sure you have GD library ver 2+");

}

//the resizing is going on here!

imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//finally, save the image

ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");

ImageDestroy ($resized_img);

ImageDestroy ($new_img);





}



//ok copy the finished file to the thumbnail directory

move_uploaded_file ($file_tmp, "$path_big/$rand_name.$file_ext");

/*

Don't want to copy it to a separate directory?

Want to just display the image to the user?

Follow the following steps:



2. Uncomment this code:

/*

/* UNCOMMENT THIS IF YOU WANT */

//echo "IMG:<img src=\"$path_big/$rand_name.$file_ext\" />";

//exit();

//*/



//and you should be set!



//success message, redirect to main page.		

$msg = urlencode("Image was successfully uploaded! <a href=\"resize.php\">Upload More?</a>");

header("Location: resize.php?msg=$msg");

exit();





}else{



//if there is a message, display it

if(isset($_GET['msg']))

{

//but decode it first!

echo "<p>".urldecode($_GET['msg'])."</p>";

}

//the upload form

echo "

<form action=\"$_SERVER[PHP_SELF]\" method=\"post\"enctype=\"multipart/form-data\">\n

<p>File:<input type=\"file\" name=\"vImage\" /></p>\n

<p><input type=\"submit\" name=\"submit\" value=\"Submit\" /></p>";

}

?>
Thanks in advance
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 11-23-2009, 02:37 PM Re: Remove Form From Upload Script
Defies a Status

Posts: 1,606
Trades: 0
How will you upload the file without the form?

Easy way to test it simply comment that line out and see if it works. Do not comment out the closing }


PHP Code:
//echo "<form action=\"$_SERVER[PHP_SELF]\" method=\"post\"enctype=\"multipart/form-data\">\n
<p>File:<input type=\"file\" name=\"vImage\" /></p>\n
<p><input type=\"submit\" name=\"submit\" value=\"Submit\" /></p>"

For some reason VB is treating it like seperate lines of code when it is all one big echo statement.
__________________
Colbyt

Please login or register to view this content. Registration is FREE
colbyt is offline
Reply With Quote
View Public Profile
 
Old 11-24-2009, 10:57 AM Re: Remove Form From Upload Script
Brian07002's Avatar
Defies a Status

Posts: 2,162
Name: ...
Location: ...
Trades: 0
The purpose of this would be to resize the images that already exist in a folder on my server instead of using the upload form to get the images. This script is *PERFECT* for that, but it contains an upload form that I don't need because I am already using a form to upload the images, and figured to do dynamic resizing from this script I posted above.
__________________
Made2Own

Please login or register to view this content. Registration is FREE
Brian07002 is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to Remove Form From Upload 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.21184 seconds with 12 queries