473,383 Members | 2,005 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,383 software developers and data experts.

how can i prevent blank upload

123 100+
the code below will upload image and insert the image name and other variable into the database but i dont want the upload feild to post a blank field and also not
to allow any image which is not gif, jpeg and png. the code that i have that can do the folowing are if($_FILES['images']['size'] == 0){ and if (!eregi("^([A-Za-z0-9]+)\.jpg|\.gif|\.png$", $logoname))
but the problem is i dont know where to insert it in the code below (upload.php) i try inserting it but did not working

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.         if (isset($_POST['submit'])) {
  4.           If(empty($_POST['fristname']) OR empty($_POST['emaill'])OR empty($_POST['category'])){
  5.               echo '<font size="2" color="#FF0000">Please fill in all the required fields!</font>';
  6.          }else{
  7.  
  8.                     $logoname = $_FILES['images']['name'];
  9.                     $tmpname  = $_FILES['images']['tmp_name'];
  10.                     $logosize = $_FILES['images']['size'];
  11.                        $logotype = $_FILES['images']['type'];
  12.  
  13.  
  14.  
  15.              while(list($key,$value) = each($_FILES['images']['name'])){
  16.              if(!empty($value)) {
  17.                  $logoname = $value;
  18.                      $logoname = str_replace(" ","_",$logoname);// Add _ inplace of blank space in file name, you can remove this line
  19.  
  20.                      $add = "./company_logo/$logoname";
  21.                         echo "<font size='2' color='#FF0000'>your company is added successfully</font>";
  22.                   echo "<br>";
  23.  
  24.                      copy($_FILES['images']['tmp_name'][$key], $add);
  25.                      chmod("$add",0777);
  26.             }
  27.  
  28.                      $image = new SimpleImage();
  29.                      $image->load("$add");
  30.                      $image->resize(100,100);
  31.                      $image->save("$add"); 
  32.  
  33.             $fristname = ($_POST['fristname']);
  34.             $email = ($_POST['email']);
  35.             $category = ($_POST['category']);
  36.  
  37.                 mysql_query("INSERT INTO companylogourl (fristname, imagename, email, category) 
  38.     VALUES('$fristname', '$logoname', '$email', '$category')") or die(mysql_error());
  39.          } 
  40.         }
  41.     }
  42.  
  43.         ?>
  44.  
May 10 '09 #1
3 2231
Markus
6,050 Expert 4TB
You want to do it as early as possible to avoid doing stuff that isn't necessary if the file isn't correct. So, I would suggest add your conditional statements immediately after you create the image information variables (lines 8 - 11).
May 10 '09 #2
simon2x1
123 100+
i have insert the conditional statement between line 8 and 11 but is still giving me error when i try to post a blank field i will appreciate if you help me adjust my code


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3.         if (isset($_POST['submit'])) {
  4.           If(empty($_POST['fristname']) OR empty($_POST['emaill'])OR empty($_POST['category'])){
  5.               echo '<font size="2" color="#FF0000">Please fill in all the required fields!</font>';
  6.          }else{
  7.  
  8.             if($_FILES['images']['size'] == 0){ 
  9.         echo '<font size="2" color="#FF0000">Please upload your company logo</font>';
  10.         }
  11.         if (!eregi("^([A-Za-z0-9]+)\.jpg|\.gif|\.png$", $logoname)){
  12.         echo '<font size="2" color="#FF0000">invalid image format</font>';
  13.             }
  14.                     $logoname = $_FILES['images']['name'];
  15.                     $tmpname  = $_FILES['images']['tmp_name'];
  16.                     $logosize = $_FILES['images']['size'];
  17.                        $logotype = $_FILES['images']['type'];
  18.  
  19.  
  20.  
  21.              while(list($key,$value) = each($_FILES['images']['name'])){
  22.              if(!empty($value)) {
  23.                  $logoname = $value;
  24.                      $logoname = str_replace(" ","_",$logoname);// Add _ inplace of blank space in file name, you can remove this line
  25.  
  26.                      $add = "./company_logo/$logoname";
  27.                         echo "<font size='2' color='#FF0000'>your company is added successfully</font>";
  28.                   echo "<br>";
  29.  
  30.                      copy($_FILES['images']['tmp_name'][$key], $add);
  31.                      chmod("$add",0777);
  32.             }
  33.  
  34.                      $image = new SimpleImage();
  35.                      $image->load("$add");
  36.                      $image->resize(100,100);
  37.                      $image->save("$add"); 
  38.  
  39.             $fristname = ($_POST['fristname']);
  40.             $email = ($_POST['email']);
  41.             $category = ($_POST['category']);
  42.  
  43.                 mysql_query("INSERT INTO companylogourl (fristname, imagename, email, category) 
  44.     VALUES('$fristname', '$logoname', '$email', '$category')") or die(mysql_error());
  45.          } 
  46.         }
  47.     }
  48.  
  49.         ?>
  50.  
May 12 '09 #3
hoopy
88
Hi when you check for if a file exists to be uploaded, if its not then you should exit() or redirect or skip all the next part. Currently it looks like you are throwing an error back but still attempting to do the upload.

So here:

Expand|Select|Wrap|Line Numbers
  1. if($_FILES['images']['size'] == 0){ 
  2.   echo '<font size="2" color="#FF0000">Please upload your company logo</font>';
  3.  ///// DO SOMETHING HERE to stop the process....
  4. }
Post back if you are still stuck.
May 12 '09 #4

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

Similar topics

7
by: John | last post by:
How do I prevent blank data from being entered into a table? For instance, the user fills out a form, but if a field is left blank, then the entire entry won't be filled in. This isn't...
2
by: Ravi | last post by:
I have a webpage(with a text field and a button) which prompts for a local file and attempts to load the file in the window if the user clicks on the button. The javascript code onClick event uses...
1
by: Brian | last post by:
Hello - Is there a way to prevent the user from creating a blank row at the bottom of a datagrid? I want a read only grid that allows the user to view data but not select specific cells or...
24
by: Charles Law | last post by:
When I click a button I don't want the click event to fire. Is this possible? In fact, what I would really like is to be able to intercept the click event, perform some action, and then prevent...
2
by: Jake Barnes | last post by:
At a certain point in my code, I need to blank the value in a file input. The situation comes up when a user first thinks they want to upload something from their harddrive, but then they change...
15
by: Larry Bud | last post by:
I rarely crosspost, but this affects both ASP and Javascript REALLY odd bug that I ran across in ASP 3.0. I have an input type of file, user clicks browse, then places his cursor in the...
3
by: groups2 | last post by:
When you press the down key while in an input field the default behavior for some event creates a dropdown of the previously input text. What event creates that behavior and how do I stop it ? ...
2
by: DavidPr | last post by:
I'm creating (trying to create) a picture gallery for my website. The script is not working. I've been working on it now for about 80 hours with no success. My php skills aren't very good. This...
5
by: Mani | last post by:
Hi I have created one sample php script to upload excel sheets of very bigger sizes. Also the process happening with each records given in the excel sheets is bit complex. To allow bigger sizes,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?

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.