473,800 Members | 2,418 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

uploaded file validation is not working

154 New Member
Hi I am tryin to validate certian types of files to be uploaded and file size and if this follows this correctly then insert in db

this is the code i did so far. So far I commented out the parts that are not working.


Could somebody help me out please.


[PHP]<?php
;

//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($fil ename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}

//This applies the function to our file
$ext = findexts ($_FILES['resume']['name']) ;

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$ran = 'resume_'.rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "/var/www/virtual/domain/uploads/";
//This combines the directory, the random file name, and the extension


$target = $target . $ran2.$ext;

$ok=1;

/* //Check file types
if ($ext == "doc") {
$ok=1;
}
elsif ($ext == "pdf") {
$ok=1;
}
elsif ($ext == "txt") {
$ok=1;
}
else {
$ok=0;
Echo "You may only upload MS Word, PDF or Text files. Please click the back button and try again.<br>";

}*/

//Here we check that $ok was not set to 0 by an error
if ($ok==0)
{
Echo "You may only upload MS Word, PDF or Text files. Please click the back button and try again.<br>";
}


//This is our size condition
//if ($uploaded_size > 2000000)
//{
//echo "Your file is over the size limit (MAX SIZE ALLOWED = 2 MB). Please click the back button and correct this.<br>";

//}



//If everything is ok we try to upload it

//Writes the resume to the server
if(move_uploade d_file($_FILES['resume']['tmp_name'], $target))
{

############### ## Insert in Database ############### ######
$con = mysql_connect(" localhost","dbu sr","dbpass") ;
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_d b("db", $con);
$sql="INSERT INTO apps (applicationID, salutation, first_name, last_name, dob, nationality, address, work, home, mobile, email, marital_status, salary, position, resume, createddate)
VALUES (NULL, '$_POST[salutation]', '$_POST[first_name]', '$_POST[last_name]', '$_POST[dob]', '$_POST[nationality]', '$_POST[address]', '$_POST[work]', '$_POST[home]', '$_POST[mobile]', '$_POST[email]', '$_POST[marital_status]', '$_POST[salary]', '$_POST[position]', '$target', NOW())";

if (!mysql_query($ sql,$con))

{
die('Error: ' . mysql_error());
}


echo "Thank you $_POST[first_name] $_POST[last_name] for submitting your application ";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file. Please click the back button and correct the file then try again.";
}


mysql_close($co n)




?>[/PHP]
Nov 16 '07 #1
10 2561
pbmods
5,821 Recognized Expert Expert
Heya, Jonathan.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Nov 17 '07 #2
Markus
6,050 Recognized Expert Expert
I can't see what's wrong, but i'm sure that's not all the upload script.

Anyway, the 'findexts()' function is completely unnecessary

Expand|Select|Wrap|Line Numbers
  1. $_FILES['name_of_input']['type']
  2.  
is a much better way to do it.

You can then check by doing:
Expand|Select|Wrap|Line Numbers
  1. if((
  2.    ($_FILES['name_of_input']['type'] == "application/msword")
  3. || ($_FILES['name_of_input']['type'] == "application/pdf")
  4. || ($_FILES['name_of_input']['type'] == "text/plain")
  5. && 
  6.   ($_FILES['name_of_input']['size'] < maxfilesizehere))
  7. {
  8. //code to execute here
  9. }
  10.  
:)
Remember! Uploading with
Expand|Select|Wrap|Line Numbers
  1. <input type="file">
  2.  
you need:
Expand|Select|Wrap|Line Numbers
  1. <form action="upload_file.php" method="post"
  2. enctype="multipart/form-data">
  3.  
aswell.

Also, do not use
Expand|Select|Wrap|Line Numbers
  1. $_POST['name']; 
  2. // use
  3. $_FILES['name'];
  4.  
mark
Nov 17 '07 #3
jonathan184
154 New Member
Hi

The script works withthe commented , right all it does is upload and rename the file to resume_random number.txt or what ever format.

The commented part i was trying to use was trying to vaildate the extensions so i only wanted to accept txt, pdf and doc files only all others error out.

I also wanted to validate the file size , do not send anything over 2MB

Thants it really i am trying to accomplish at this point so far i cannot validate the extensions or the file size.

This is all the php code the rest is in html that only controls the design.

Heya, Jonathan.

What do you want your code to do? Give an example.
What is your code doing that you don't want it to do? Give an example.
What is your code *not* doing that it is supposed to? Give an example.
Nov 17 '07 #4
jonathan184
154 New Member
thanks markusn00b

will try that.
Nov 17 '07 #5
Markus
6,050 Recognized Expert Expert
thanks markusn00b

will try that.
No problem, let me know how things go!

And post up your full code, so we can have a closer look :)
Nov 17 '07 #6
jonathan184
154 New Member
Hi guys I am still getting the same problem. I put the if statements for the type and size validation but the script does not work when i put it in. If i remove the if statements on the top here and comment out the else the script works fine but there is validation. Where am i going wrong?

[PHP] <?php

if((($_FILES['resume']['type'] == "applicatio n/msword")
|| ($_FILES['resume']['type'] == "applicatio n/pdf")
|| ($_FILES['resume']['type'] == "text/plain")
&& ($_FILES['resume']['size'] < 200000))
{
//This function separates the extension from the rest of the file name and returns it
function findexts ($filename)
{
$filename = strtolower($fil ename) ;
$exts = split("[/\\.]", $filename) ;
$n = count($exts)-1;
$exts = $exts[$n];
return $exts;
}
$today = date("Y-m-d_");
//This applies the function to our file
$exts = findexts ($_FILES['resume']['name']) ;

//This line assigns a random number to a variable. You could also use a timestamp here if you prefer.
$ran = 'resume_'.$toda y.rand () ;

//This takes the random number (or timestamp) you generated and adds a . on the end, so it is ready of the file extension to be appended.
$ran2 = $ran.".";

//This assigns the subdirectory you want to save into... make sure it exists!
$target = "/var/www/virtual/ansaauto.com/htdocs/resumes/";
//This combines the directory, the random file name, and the extension


$target = $target . $ran2.$exts;

} else {
echo "Your file is over the size limit (MAX SIZE ALLOWED = 2 MB). Please click the back button and correct this.<br>";
exit;
}


//If everything is ok we try to upload it

//Writes the resume to the server
if(move_uploade d_file($_FILES['resume']['tmp_name'], $target))
{

############### ## Insert in Database ############### ######
$con = mysql_connect(" localhost","use r","pass");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}mysql_select_d b("dbname", $con);
$sql="INSERT INTO applications (applicationID, salutation, first_name, last_name, marital_status, dob, nationality, address, work, home, mobile, email, salary, position, resume, createddate)
VALUES (NULL, '$_POST[salutation]', '$_POST[first_name]', '$_POST[last_name]', '$_POST[marital_status]', '$_POST[dob]', '$_POST[nationality]', '$_POST[address]', '$_POST[work]', '$_POST[home]', '$_POST[mobile]', '$_POST[email]', '$_POST[salary]', '$_POST[position]', '$target', NOW())";

if (!mysql_query($ sql,$con))

{
die('Error: ' . mysql_error());
}


echo "Thank you $_POST[first_name] $_POST[last_name] for submitting your application";
}
else {

//Gives and error if its not
echo "Sorry, there was a problem uploading your file. Please click the back button and correct the file then try again.";
exit;
}


mysql_close($co n)




?>[/PHP]
Nov 20 '07 #7
pbmods
5,821 Recognized Expert Expert
Heya, Jonathan.

Try this:
Expand|Select|Wrap|Line Numbers
  1. if((($_FILES['resume']['type'] == "application/msword")
  2. || ($_FILES['resume']['type'] == "application/pdf")
  3. || ($_FILES['resume']['type'] == "text/plain")
  4. && ($_FILES['resume']['size'] < 200000))
  5. {
  6.     .
  7.     .
  8.     .
  9. }
  10. else
  11. {
  12.     header('Content-type: text/plain');
  13.     print_r($_FILES);
  14.     exit;
  15. }
  16.  
Nov 20 '07 #8
jonathan184
154 New Member
unfortunately still no luck it came up with a blank page still.
Nov 20 '07 #9
pbmods
5,821 Recognized Expert Expert
Heya, Jonathan.

If you're getting a blank page, your script is probably generating an error. Check out this article to find out what is going on.
Nov 20 '07 #10

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

Similar topics

7
15916
by: smilesinblues | last post by:
Hi, I have to allow my visitors to upload image on my site. I am using the follwoing code to do that: $uploaddir = 'admin/'; $uploadfile = $uploaddir . basename($_FILES); move_uploaded_file($_FILES, $uploadfile); My problem is that I expect them to use same file name, like all of
18
9119
by: Dino | last post by:
dear all, i've created an application for a customer where the customer can upload ..csv-files into a specified ftp-directory. on the server, a php-script, triggered by a cronjob, reads all the data, imports it into a mySQL database and deletes the .csv-file after the import. so far, so good. but in some cases the cronjobs starts running when the file is not completely
0
2223
by: Mark | last post by:
Hi all, I want to be able to resize an image that a user has uploaded. I have the image upload working as well as the image resize. When I go to delete the original uploaded file (I want to replace the uploaded file with the new resized version) I get a "file being used by another process" error. I therefore cannot replace the uploaded image with the new thumbnail. How can I get around this???
1
6933
by: j | last post by:
Hi, I've been trying to do line/character counts on documents that are being uploaded. As well as the "counting" I also have to remove certain sections from the file. So, firstly I was working with uploaded MS WORD .doc files. Using code like that below: strLine = sr.ReadLine While Not IsNothing(strLine) 'Not eof If Trim(strLine) <> "" Then 'Not blank
5
2737
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
0
1216
by: IkBenHet | last post by:
Hello, I use this script to upload image files to a folder on a IIS6 server: ******************* START UPLOAD.ASPX FILE ********************** <%@ Page Language="VB" Debug="true" %>
4
2420
by: riteshjain82 | last post by:
Hi, Please go through this: I am having a file (default.asp) on which i am taking many details from a user before mailing it to someone. I have also provided the user with a facility of uploading files on a server. I am uploading files from a page upload.asp which has a link on default.asp. Now my problem is that, how can i send the file name that is uploaded, in a mail along with other details that the user enters on default.asp?? In...
4
6320
sumittyagi
by: sumittyagi | last post by:
If we want to upload file, we you the tag: <input type="file" name="fileName"/> Now is it possible to get the size of the uploaded file before submitting it.(at onsubmit) or we would have to do that at server side only.
2
1493
by: agarwalsrushti | last post by:
Hi, I have made a registration form for job site in which the user uploads a .doc or .txt file through form along with the other details stored in the database. The uploaded files is sored in the upload folder that i created in working directory and not in the database. Now when someone enters the search page and enters the search criteria the name of the users with that criteria are listed. When i ckick on the name of any user then his details...
0
9690
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10275
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10253
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
7576
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5606
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4149
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3764
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2945
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.