Solved

Limit PHP Uploaded Files to CSV

Posted on 2008-09-30
2
336 Views
Last Modified: 2013-12-12
How can you limit & verify that an uploaded file is a .csv file using PHP.  Using the code for  
//This Works

if (!($uploaded_type=="image/gif")) {

echo "You may only upload .Gif files.<br>";

$ok=0;

} 

//This is ineffective

if (!($uploaded_type=="text/txt")) {

echo "You may only upload .CSV files.<br>";

$ok=0;

}

Open in new window

0
Comment
Question by:ITApprendista
2 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 100 total points
ID: 22611405
Depending on the install the type might be "text/csv" and "text/comma-separated-values" But the best would be to look into the file, parse the first line and check the content
0
 
LVL 2

Accepted Solution

by:
idealws earned 400 total points
ID: 22611470
The below code will check to make sure the file has the .csv extension only if not it will print a error. If it does then it is uploaded and moved to the directory specified in the $datadir variable. You can then proceed to do what you need after the file is uploaded.
// Directory to upload to

$datadir = "data/";

// Lets get the file name

$uploadfile = $datadir.$_FILES['userfile']['name'];

$ext = strrchr($_FILES['userfile']['name'],'.');

if($ext != ".csv") {

    echo "Only CSV files are allowed to be uploaded";

    $error = 1;

}else{

    // Ad your upload code here

    if (move_uploaded_file($_FILES['userfile']['tmp_name'], $uploadfile)) {

        // If this file is moved (uploaded do this

    }

}

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now