• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 346
  • Last Modified:

Limit PHP Uploaded Files to CSV

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
ITApprendista
Asked:
ITApprendista
2 Solutions
 
hernst42Commented:
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
 
idealwsCommented:
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now