473,407 Members | 2,598 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

PHP file upload

30
Hi guys,

I have managed to code a file that uploads a file, resizes it to 100 x 100 px, and then generates a random number and renames the image (so i do not get images over writing each other), what i am stuck on is trying to block uploads that are over 200kb, and if there is no file to upload (as there will be no need sometimes to upload a file) to skip the code all together in order to process some more code i got going on after this piece, can anyone help! i have looked around various websites and am just absoulty stuck on this!

Any help is much appriciated

The Code:

<?php
if(isset($_POST['submit'])){
if (isset ($_FILES['file'])){
imagename = $_FILES['file']['name'];
$random_digit=rand(0000,9999);
$new_file_name=$random_digit.$imagename;

$source = $_FILES['file']['tmp_name'];
$target = "Articleimages/".$new_file_name;
move_uploaded_file($source, $target);


$imagepath = $new_file_name;
$save = "Articleimages/" . $imagepath;
$file = "Articleimages/" . $imagepath;

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

$modwidth = 100;

$diff = $width / $modwidth;

$modheight = 100;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;

$save = "Articleimages/Thumbs/" . $imagepath;
$file = "Articleimages/" . $imagepath;

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

$modwidth = 100;

$diff = $width / $modwidth;

$modheight = 100;
$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

imagejpeg($tn, $save, 100) ;

}
}
}
?>



Cheers

Jay!
Jun 22 '08 #1
3 1069
Markus
6,050 Expert 4TB
You can obtain the size of the file in the $_FILES array.

$_FILES['file']['size']

Then use an IF check to compare sizes.
Jun 22 '08 #2
move the code to resize and rename the image in a new function. something like this:

Expand|Select|Wrap|Line Numbers
  1. <?php
  2. /* function to resize and rename the uploaded image */
  3. function resize_and_rename($filename) {
  4.   /* ...some code here...*/
  5. }
  6.  
  7. /* process uploaded image */
  8. if (is_uploaded_file($_FILES['file']['tmp_name'])) {
  9.   /* check if uploaded image size < 200kb */
  10.   if ($_FILES['file']['size'] >= 200000) {
  11.     die("Image size should be < 200kb!");
  12.   } else {
  13.     /* resize and rename the uploaded image */
  14.     resize_and_rename($_FILES['file']['tmp_name']);
  15.   }
  16. }
  17. ?>
  18.  
Jun 24 '08 #3
Asprisa
30
Hi Guys,

Thank you very much for your replies, i managed to get the code working!

Jay!
Jul 3 '08 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tihon | last post by:
Hello! I again need your help, just can't understand whats going on. Got this upload pictures form and it's having problem handling large files (~1.5 - 2 MB). Everything works fine if i just...
15
by: Simon | last post by:
I would like to create a very basic file upload add image form to add to my web site and to keep them in a "tmp" directory within my web hosting file manager once uploaded. I understand the basic...
2
by: matt | last post by:
I have compiled some code, some written by me, some compiled from various sources online, and basically i've got a very simple flat file photo gallery. An upload form, to upload the photos and give...
13
by: Sky Sigal | last post by:
I have created an IHttpHandler that waits for uploads as attachments for a webmail interface, and saves it to a directory that is defined in config.xml. My question is the following: assuming...
2
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage()...
7
by: pbd22 | last post by:
hi. i am having probs understanding how to grab a file being uploaded from a remote client. i am using hidden input fields for upload such as: <input id="my_file_element" type="file"...
2
by: hotflash | last post by:
Hi All, I found the best pure ASP code to upload a file to either server and/or MS Access Database. It works fine for me however, there is one thing that I don't like and have tried to fix but...
1
by: chrisj | last post by:
I'm using freeASPupload and got some assistance integrating to a Member script. It works successfully. In this modified version there are two groups that use this upload script. Members of one...
6
Jacotheron
by: Jacotheron | last post by:
I need a PHP script that can upload music files (mp3). The script is for a home project I have started a while ago. I have a MySQL database of all the music that I have. Other computers on the...
7
Curtis Rutland
by: Curtis Rutland | last post by:
Building A Silverlight (2.0) Multi-File Uploader All source code is C#. VB.NET source is coming soon. Note: This project requires Visual Studio 2008 SP1 or Visual Web Developer 2008 SP1 and...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.