?
Solved

Limit PHP Uploaded Files to CSV

Posted on 2008-09-30
2
Medium Priority
?
343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 48

Assisted Solution

by:hernst42
hernst42 earned 400 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 1600 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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

This article discusses four methods for overlaying images in a container on a web page
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses

800 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