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]
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.
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 -
$_FILES['name_of_input']['type']
-
is a much better way to do it.
You can then check by doing: -
if((
-
($_FILES['name_of_input']['type'] == "application/msword")
-
|| ($_FILES['name_of_input']['type'] == "application/pdf")
-
|| ($_FILES['name_of_input']['type'] == "text/plain")
-
&&
-
($_FILES['name_of_input']['size'] < maxfilesizehere))
-
{
-
//code to execute here
-
}
-
:)
Remember! Uploading with
you need: -
<form action="upload_file.php" method="post"
-
enctype="multipart/form-data">
-
aswell.
Also, do not use -
$_POST['name'];
-
// use
-
$_FILES['name'];
-
mark
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.
thanks markusn00b
will try that.
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 :)
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]
pbmods 5,821
Recognized Expert Expert
Heya, Jonathan.
Try this: -
if((($_FILES['resume']['type'] == "application/msword")
-
|| ($_FILES['resume']['type'] == "application/pdf")
-
|| ($_FILES['resume']['type'] == "text/plain")
-
&& ($_FILES['resume']['size'] < 200000))
-
{
-
.
-
.
-
.
-
}
-
else
-
{
-
header('Content-type: text/plain');
-
print_r($_FILES);
-
exit;
-
}
-
unfortunately still no luck it came up with a blank page still.
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.
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
|
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
|
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???
|
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
|
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" %>
| |
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" %>
|
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...
|
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.
|
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...
|
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...
|
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...
| |
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,...
|
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...
|
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();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
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...
| |