file.csv structure:
Before uploading to folder upload, I want to check the column.
The algorithm is like:
if ((column 1 = 'no') && (column 2 = 'product_name') && (column 3 = 'product_price') && (column 4 = 'qty'))
{
upload();
}
else
{
die;
}
Is there any coding for this case using php coding or sql to validate?
We have to load the csv file in location and fetch the csv files content and validate the content.
Here is my example:
$destination=C:\Users\AIT\Desktop\SC MArgins new.csv;
$contents = file ($destination);
for($i=1; $i<sizeof($contents); $i++)
{
$line = trim($contents[$i],'",');
$arr = explode(',',$line);
if($arr[0]=='')
{
$rows.='Item#,';
}
if($arr[1]=='')
{
$rows.='Product Name,';
}
if($arr[2]=='')
{
$rows.='Product Code,';
}
if($arr[3]=='')
{
$rows.='Product Grade,';
}
if($rows!=''){
$row1.='Row '.$i.'-'. $rows.'can not be empty'."<br>";}
$k.=$row1;
}
if($k!='' ){
$common->setMsg($row1, 'box_warning');
return false;
}
This code will help u to get which column is missing. Follow this code and let me know if any issues on processing the csv
Related
I have eight csv sheets, which where updated (every two hours) by an external service. I want to analyse these data - showing it in a php page for my boss....
I imagined, that it would be the easiest way to have the .csv in a GTT on my MsSQL Server. Maybe it is possible to create a GTT on ervery use (on commit preserve rows)? I still have no approach to make the csv to a normal format for insert syntax... is it even possible?
Or is there an other option? I try to catch some ideas for the solution...
Thank you!
EDIT
I have php code to read the .csv and put it into an array.
$csv_datei = "applicationData.csv";
$feler_trenner = ";";
$zeilen_trenner = "n";
if (#file_exists($csv_datei) == false) {
echo 'Die CSV Datei: '. $csv_datei.' gibt es nicht!';
}
else {
$datei_inhalt = #file_get_contents($csv_datei);
$zeilen = explode($zeilen_trenner, $datei_inhalt);
if (is_array($zeilen) == true) {
foreach($zeilen as $zeile) {
$felder = explode($feler_trenner, $zeile);
$i = 0;
if (is_array($felder) == true) {
foreach($felder as $felde) {
if ($felde != '') {
echo (($i != 0) ? ', ':'') . str_replace('"', '', $felde);
$i++;
}
}
}
echo '<br>';
}
}
}
source: PHP-space.info
I have search many time but i did not find any solution so in this case i can not post any code. sorry for this.
I have faced a problem that how can i check that 2 or more video same like i have media folder and many video upload in this folder then when i upload new video then need to check that video already exit or not.
1. if i have video demo.mp4 then when i will try to upload same video then give error
2. if i change video name like demo.mp4 to demo1.mp4 then i will give same error cause video name different but video content same
3. if i upload video demo5.mp4 then show me no error
i already checked image compare using
include('compareImages.php');
$new_image_name = $uploadfile_temp;
$compareMachine = new compareImages($new_image_name);
$image1Hash = $compareMachine->getHasString();
$files = glob("uploads/*.*");
$check_image_duplicate = 0;
for ($i = 0; $i < count($files); $i++) {
$filename = $files[$i];
$image2Hash = $compareMachine->hasStringImage($filename);
$diff = $compareMachine->compareHash($image2Hash);
if($diff < 10){
$check_image_duplicate = 1;
//unlink($new_image_name);
break;
}
}
but i can not compare video. someone help me
Tested and works fine, code taken from :http://php.net/manual/en/function.md5-file.php#94494 not mine.
<?php
define('READ_LEN', 4096);
if(files_identical('demo.mp4', 'demo1.mp4'))
echo 'files identical';
else
echo 'files not identical';
// pass two file names
// returns TRUE if files are the same, FALSE otherwise
function files_identical($fn1, $fn2) {
if(filetype($fn1) !== filetype($fn2))
return FALSE;
if(filesize($fn1) !== filesize($fn2))
return FALSE;
if(!$fp1 = fopen($fn1, 'rb'))
return FALSE;
if(!$fp2 = fopen($fn2, 'rb')) {
fclose($fp1);
return FALSE;
}
$same = TRUE;
while (!feof($fp1) and !feof($fp2))
if(fread($fp1, READ_LEN) !== fread($fp2, READ_LEN)) {
$same = FALSE;
break;
}
if(feof($fp1) !== feof($fp2))
$same = FALSE;
fclose($fp1);
fclose($fp2);
return $same;
}
?>
Trying to locate the first blank cell in a column. The idea is to pick a column that I know has to have a value (in this case, JOB_NUMBER) and scan through it until a blank cell is found. The code below, in my mind, should do that. However, it never stops. I imagine it is stuck in the while loop, but I don't understand why.
Code:
<?php
require('./Classes/PHPExcel/IOFactory.php');
ini_set('max_execution_time', 800);
ini_set('memory_limit', 2000000000);
$inputFileType = 'Excel2007';
$inputFileName = $_FILES['file']['tmp_name'];
class MyReadFilter implements PHPExcel_Reader_IReadFilter {
public function __construct($fromColumn, $toColumn) {
$this->columns = array();
$toColumn++;
while ($fromColumn !== $toColumn) {
$this->columns[] = $fromColumn++;
}
}
public function readCell($column, $row, $worksheetName = '') {
// Read columns from 'A' to 'AF'
if (in_array($column, $this->columns)) {
return true;
}
return false;
}
}
$filterSubset = new MyReadFilter('A', 'AF');
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
$objReader->setReadFilter($filterSubset);
$objReader->setLoadSheetsOnly( array("NORTH") );
$objPHPExcelReader = $objReader->load($inputFileName);
$r = 3500;
while(isset($maxrow_north) != 1){
$cellvalue = $objPHPExcelReader->getSheetByName('NORTH')->getCellByColumnAndRow(2, $r);
if(isset($cellvalue) != 1){
$maxrow_north = $r;
} elseif($r > 4000) {
echo "It's over 4000!";
} else {
$r = $r++;
}
}
echo $maxrow_north;
?>
Some more background
I am having admins upload .xlsx .xls or .csv files into an html form. The code, above, is the handler. I have limited the number of columns seen because the original creator of the .xlsx file thought it would be a great idea to have the columns go all the way out to XCF.
The rows also go all the way out to somewhere around 10,000. So, I want to find the first blank row and stop there.
TIA!
Don't use
if(isset($cellvalue) != 1){
A cell value always exists even if it's an empty string or a null: and you're not testing the actual cell value, but the existence of a cell.... simply get() ting a cell will create a new empty cell object if one didn't already exist
You need to test the actual value stored in the cell
if($cellvalue->getValue() === NULL || $cellvalue->getValue() === '') {
$maxrow_north = $r;
And if you're trying to find the first blank cell in the column, then break once you've found it rather than carry on iterating till you reach your max
(Note, doesn't check for rich text in cells)
EDIT
Example, that also allows for merged cells
function testInMergeRangeNotParent($objWorksheet, $cell)
{
$inMergeRange = false;
foreach($objWorksheet->getMergeCells() as $mergeRange) {
if ($cell->isInRange($mergeRange)) {
$range = PHPExcel_Cell::splitRange($mergeRange);
list($startCell) = $range[0];
if ($cell->getCoordinate() !== $startCell) {
$inMergeRange = true;
}
break;
}
}
return $inMergeRange;
}
$column = 2; // Column to check
$max = 4000;
echo 'Get First blank row in column ', $column, PHP_EOL;
$r = 3500; // Starting row
while(true){
$cell = $objPHPExcelReader->getSheetByName('NORTH')->getCellByColumnAndRow($column, $r);
if ($cell->getValue() === NULL &&
!testInMergeRangeNotParent($objPHPExcelReader->getSheetByName('NORTH'), $cell)) {
break;
}elseif($r > $max) {
echo "It's over $max !";
break;
}
$r++;
}
echo 'First blank row in column ', $column, ' is ', $r, PHP_EOL;
Before you asked, I did look at all the similar topics and did not not find the solution to my problem. When I try to upload multiple files, say 4, 3 files are uploaded. I am really sure the loop is correct, but i could be wrong. Self-taught PHP newbie.
The code is as follows:
if(isset($_POST['submit']) && $_POST['submit']=="Upload"){
if(count($_FILES['image_filename']['name']) == 0)
{
$form->seterror("fileupload"," * At least one file required.");
}
if($form->num_errors == 0)
{
$directory="../../images/properties";
$ref='';
//Loop through each file
for($i=0; $i<count($_FILES['image_filename']['name']); $i++)
{
if($_FILES['image_filename']['name'][$i] != "")
{
//Get the temp file path
$filename = $_FILES['image_filename']['name'][$i]; // filename stores the value
$filename = str_replace(" ","_",$filename);// Add _ inplace of blank space in file name, you can remove this line
$filename = stripslashes($filename); // strip file_name of slashes
$filename = str_replace("'","",$filename); //remove quotes
$filesize = $_FILES['image_filename']['size'][$i];
$filetype = $_FILES['image_filename']['type'][$i];
$filetemp = $_FILES['image_filename']['tmp_name'][$i];
//echo $filename.'<br>';
$filework->file_upload($_FILES['image_filename']['name'][$i],$directory,$_FILES['image_filename']['tmp_name'][$i]); // this is my File upload class
$ref=$_FILES['image_filename']['name'][$i];
if($countImg == 0) {
if($i==0) {
$main='1';
} else {
$main='0';
}
} else {
$main='0';
}
$q="INSERT INTO property_images(property_id,image_filename,image_reference,main_image)".
" VALUES (".$_GET['pid'].",'".$_FILES['image_filename']['name'][$i]."','".$ref."','".$main."')";
//echo $q.'<br>';
$database->query($q) or die(mysql_error());
$x++;
}
}
}
}
[/code]
Isn't the loop skipping the last file?
Try changing this:
for($i=0; $i<count($_FILES['image_filename']['name']); $i++)
to:
for($i=0; $i<=count($_FILES['image_filename']['name']); $i++)
I've been given this bit of code:
if(isset($_GET['viewevent'])) {
if(count($_SESSION['e_lastviewed']) == 0) {
$_SESSION['e_lastviewed'][0] = $_GET['viewevent'];
} else if(!in_array($_GET['viewevent'], $_SESSION['e_lastviewed'])) {
$_SESSION['e_lastviewed'][2] = $_SESSION['e_lastviewed'][1];
$_SESSION['e_lastviewed'][1] = $_SESSION['e_lastviewed'][0];
$_SESSION['e_lastviewed'][0] = $_GET['viewevent'];
}
}
if($_GET['show']) {
$_SESSION['show'] = $_GET['show'];
} else if($_SESSION['show']=='') {
$_SESSION['show'] = "all";
}
It apparently saves ID's of recently viewed items, so i need to put these id's into an array.
Would this work?
$my_array = array($_SESSION['e_lastviewed'][2],$_SESSION['e_lastviewed'][1],$_SESSION['e_lastviewed'][0]);
I've ran it but it displays blank results (not sure if thats due to me not doing it right or incomplete code...Have i missed something? I'm not sure if i completley understand the script i was given...
try this:
if ( !isset($_SESSION['e_lastviewed']) )
$_SESSION['e_lastviewed'] = array();
// alt: while(count($_SESSION['e_lastviewed']) > 2 ) {
if(count($_SESSION['e_lastviewed']) > 2 ) {
array_shift($_SESSION['e_lastviewed']); // drop off from 3
array_unshift($_SESSION['e_lastviewed'],$_GET['viewevent']); // insert in the beginning
if($_GET['show']) {
$_SESSION['show'] = $_GET['show'];
} else if($_SESSION['show']=='') {
$_SESSION['show'] = "all";
}