cannot upload the file through admin panel - php

Column count doesn't match value count at row 1
public function file_upload($file,$dir)
{
$uploads_dir = $dir;
$original_file = addslashes($file['name']); // storing file name
$buffer_path = $file['tmp_name']; // storing temp buffer path
$unique_name = time().$original_file; // creating unique file name by appending time stamp
$path = $uploads_dir.$unique_name;
// uploading file using default function
if(move_uploaded_file($buffer_path,$path))
{
return $path;
}
else
{
return "No";
}
}
function multiple_image_upload($file,$path)
{
//echo "<pre>";print_r($file);
if(empty($file['name'][0]))
{
return "No file selected";
}
else
{
$cnt = 0 ;
foreach($file['name'] as $filename)
{
$buffer_path = $file['tmp_name'][$cnt];
$unique_file_name = $path.date('Y-m-d H-i-s').time().$filename;
move_uploaded_file($buffer_path,$unique_file_name);
$final_file_path[] = $unique_file_name;
$cnt++;
}
return $final_file_path;
}
}
function multiple_thumb($msg,$filedata,$thumb_folder)
{
if(!is_dir($thumb_folder))
{
mkdir($thumb_folder,0755);
}
$cnt=0;
foreach($msg as $val)
{
/* echo $val;
echo "<br />"; */
//echo "<pre>";
//print_r($filedata['name']);
$o_image = imagecreatefromjpeg($val);
$o_width = imageSX($o_image);
$o_height = imageSY($o_image);
$n_width = 230;
$n_height = 250;
$new_image = imagecreatetruecolor($n_width,$n_height);
//print_r($new_image);
imagecopyresampled($new_image,$o_image,0,0,0,0,$n_width,$n_height,$o_width,$o_height);
$thumb_unique_path = $thumb_folder.date('Y-m-d H-i-s').time().$filedata['name'][$cnt];
imagejpeg($new_image,$thumb_unique_path);
$thumb_final_data[] = $thumb_unique_path;
$cnt++;
}
//print_r($thumb_final_data);
return $thumb_final_data;
}

Related

How to save the changes made with a script

With this script, change the order of the lines in a text file, but I'd like to save the file as a new file. How can I do that?
<?php
$file = "menu2.txt";
$righe = file($file);
$numrighe = count($righe);
$portieri = array();
$difensori = array();
$aladestra = array();
$alasinistra = array();
$attaccante = array();
for($i=0; $i < $numrighe;$i++) {
$riga = $righe[$i];
list($giocatore, $ruolo) = preg_split("[>]", $riga);
if(strcmp($ruolo,"Portiere")) { array_push($portieri, $giocatore); }
else if(strcmp($ruolo,"Difensore")) { array_push($difensori, $giocatore); }
else if(strcmp($ruolo,"Ala destra")) { array_push($aladestra, $giocatore); }
else if(strcmp($ruolo,"Ala sinistra")) { array_push($alasinistra, $giocatore); }
else if(strcmp($ruolo,"Attaccante")) { array_push($attaccante, $giocatore); }
}
?>
Excuse me but probably because of google translator or because they are prevented, but you probably will not understand it, I did it:
<?php
$file = "menu2.txt";
$righe = file($file);
$numrighe = count($righe);
$portieri = array();
$difensori = array();
$aladestra = array();
$alasinistra = array();
$attaccante = array();
for($i=0; $i < $numrighe;$i++) {
$riga = $righe[$i];
list($giocatore, $ruolo) = preg_split("[>]", $riga);
if(strcmp($ruolo,"Portiere")) { array_push($portieri, $giocatore); }
else if(strcmp($ruolo,"Difensore")) { array_push($difensori, $giocatore); }
else if(strcmp($ruolo,"Ala destra")) { array_push($aladestra, $giocatore); }
else if(strcmp($ruolo,"Ala sinistra")) { array_push($alasinistra, $giocatore); }
else if(strcmp($ruolo,"Attaccante")) { array_push($attaccante, $giocatore); }
}
// open your new file
$newFile = fopen('newmenu2.txt', 'w');
// write to your file using $newFile file handler,
// with the imploded data you want to write into your file
fwrite($newFile, implode("\n", $data));
// close your file handler
fclose($newFile);
?>
Notice: Undefined variable: data on line 29
Warning: implode(): Invalid arguments passed on line 29

base64 in PHP Otp Library

I trying to make some simple library for encrypting files in PHP with OTP method. My problem is that some chars in decrypted code are different than original. I worked on it almost one week but without result. Is there problem with base64 chars or with encoding/decoding mechanism ?
Many thanks for the answers.
final class Otp
{
private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z');
public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath)
{
if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) {
if($originalFileData = self::existsFile($originalFilePath)) {
$originalFileBase64Data = base64_encode($originalFileData);
$originalFileBase64DataLength = strlen($originalFileBase64Data) - 1;
$originalFileBase64DataArray = str_split($originalFileBase64Data);
$encryptedData = NULL;
$encryptedDataKey = NULL;
for ($i = 0; $i <= $originalFileBase64DataLength; $i++) {
$randKey = rand(0, sizeOf(self::$charSet) - 1);
$arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet);
if($randKey > $arrayKey) {
$str = '-' . ($randKey - $arrayKey);
} elseif($randKey < $arrayKey) {
$str = ($randKey + $arrayKey);
} else {
$str = $randKey;
}
$encryptedData .= self::$charSet[$randKey];
$encryptedDataKey .= $str. ';';
}
$encryptedDataString = $encryptedData;
$encryptedDataKeyString = $encryptedDataKey;
if(!self::existsFile($keyFilePath)) {
file_put_contents($keyFilePath, $encryptedDataKeyString);
}
if(!self::existsFile($encryptedFilePath)) {
file_put_contents($encryptedFilePath, $encryptedDataString);
}
return 'OK';
} else {
return 'Source file not exists';
}
} else {
return 'Encrypted data already exists';
}
}
public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath)
{
$keyFileData = self::existsFile($keyFilePath);
$encryptedFileData = self::existsFile($encryptedFilePath);
$encryptedFileDataLength = strlen($encryptedFileData) - 1;
if($encryptedFileData && $keyFileData) {
$encryptedFileDataArray = str_split($encryptedFileData);
$keyFileDataArray = explode(';', $keyFileData);
$decryptedData = NULL;
for ($i = 0; $i <= $encryptedFileDataLength; $i++) {
$poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet);
$poziciasifrovana = $keyFileDataArray[$i];
if($poziciasifrovana < 0) {
$move = $poziciasifrovana + $poziciaaktualneho;
} elseif($poziciasifrovana > 0) {
$move = $poziciasifrovana - $poziciaaktualneho;
} else {
$move = '0';
}
$decryptedData .= self::$charSet[$move];
}
if(!self::existsFile($decryptedFilePath)) {
file_put_contents($decryptedFilePath, base64_decode($decryptedData));
return 'OK';
} else {
return 'Decrypted data already exists';
}
}
}
private static function existsFile($filePath)
{
$fileData = #file_get_contents($filePath);
if($fileData) {
return $fileData;
}
return FALSE;
}
}
$originalFilePath = 'original.jpg';
$keyFilePath = 'Otp_Key_' . $originalFilePath;
$encryptedFilePath = 'Otp_Data_' . $originalFilePath;
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath;
echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath);
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath);
The problem seems to be only happening when $poziciaaktualneho is equal to $poziciasifrovana and so by adding another if statement on line 78 to check for this and instead set $move equal to $poziciasifrovana I was able to fix the problem. The below script should work:
final class Otp
{
private static $charSet = array('+','/','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L',
'M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r',
's','t','u','v','w','x','y','z');
public static function encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath)
{
if(!self::existsFile($keyFilePath) || !self::existsFile($encryptedFilePath)) {
if($originalFileData = self::existsFile($originalFilePath)) {
$originalFileBase64Data = base64_encode($originalFileData);
$originalFileBase64DataLength = strlen($originalFileBase64Data) - 1;
$originalFileBase64DataArray = str_split($originalFileBase64Data);
$encryptedData = NULL;
$encryptedDataKey = NULL;
for ($i = 0; $i <= $originalFileBase64DataLength; $i++) {
$randKey = rand(0, sizeOf(self::$charSet) - 1);
$arrayKey = array_search($originalFileBase64DataArray[$i], self::$charSet);
if($randKey > $arrayKey) {
$str = '-' . ($randKey - $arrayKey);
} elseif($randKey < $arrayKey) {
$str = ($randKey + $arrayKey);
} else {
$str = $randKey;
}
$encryptedData .= self::$charSet[$randKey];
$encryptedDataKey .= $str. ';';
}
$encryptedDataString = $encryptedData;
$encryptedDataKeyString = $encryptedDataKey;
if(!self::existsFile($keyFilePath)) {
file_put_contents($keyFilePath, $encryptedDataKeyString);
}
if(!self::existsFile($encryptedFilePath)) {
file_put_contents($encryptedFilePath, $encryptedDataString);
}
return 'OK';
} else {
return 'Source file not exists';
}
} else {
return 'Encrypted data already exists';
}
}
public static function decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath)
{
$keyFileData = self::existsFile($keyFilePath);
$encryptedFileData = self::existsFile($encryptedFilePath);
$encryptedFileDataLength = strlen($encryptedFileData) - 1;
if($encryptedFileData && $keyFileData) {
$encryptedFileDataArray = str_split($encryptedFileData);
$keyFileDataArray = explode(';', $keyFileData);
$decryptedData = NULL;
for ($i = 0; $i <= $encryptedFileDataLength; $i++) {
$poziciaaktualneho = array_search($encryptedFileDataArray[$i], self::$charSet);
$poziciasifrovana = $keyFileDataArray[$i];
if ($poziciasifrovana == $poziciaaktualneho) {
$move = $poziciasifrovana;
} elseif($poziciasifrovana < 0) {
$move = $poziciasifrovana + $poziciaaktualneho;
} elseif($poziciasifrovana > 0) {
$move = $poziciasifrovana - $poziciaaktualneho;
} else {
$move = '0';
}
$decryptedData .= self::$charSet[$move];
}
if(!self::existsFile($decryptedFilePath)) {
file_put_contents($decryptedFilePath, base64_decode($decryptedData));
return 'OK';
} else {
return 'Decrypted data already exists';
}
}
}
private static function existsFile($filePath)
{
$fileData = #file_get_contents($filePath);
if($fileData) {
return $fileData;
}
return FALSE;
}
}
$originalFilePath = 'original.jpg';
$keyFilePath = 'Otp_Key_' . $originalFilePath;
$encryptedFilePath = 'Otp_Data_' . $originalFilePath;
$decryptedFilePath = 'Otp_Decrypted_' . $originalFilePath;
echo Otp::encryptFile($originalFilePath, $encryptedFilePath, $keyFilePath);
echo Otp::decryptFile($encryptedFilePath, $keyFilePath, $decryptedFilePath);
Warning: I would not recommend using my solution in an enterprise setting if at all since I do not know why this fixes your script or what was
originally wrong with it and it is most likely not air tight.

gallery with next and prev function

I'm working on an image gallery as my first real project as of learning php.. The problem I'm not able to get around somehow is using the "previous" button. My "next" function grabs the next 6 images from the folder and posts them to the page, my prev function does this in reverse.. but that's incorrect, can you help? oh, and this code is sloppy beginner code so i'm happy for tips :)
<?php
session_start();
if(session_id() == '') {
$_SESSION["count"] = 0;
}
function PostHTML($id, $file) {
if($id==0) {
echo "<div class=\"item\"><div class=\"well\"><img
class=\"img-responsive\" src=\"$file\" alt=\"\"><p>blahblahblah xxxxxx</p></div></div>";
}else{
echo "<div class=\"item\"><div class=\"well\"><video autoplay
loop class=\"img-responsive\"><source src=\"$file\" type=\"video/mp4\"></video></div></div>";
}
}
function next_img() {
$dir = "images/src/*.*";
$files = array();
$start = $_SESSION["count"];
$end = $_SESSION["count"]+6;
$y=0;
foreach(glob($dir) as $file) {
$files[$y] = $file;
$y++;
}
for($i=$start; $i < $end; $i++){
if(pathinfo($files[$i], PATHINFO_EXTENSION)=="jpg") {
PostHTML(0,$files[$i]);
} else {
PostHTML(1,$files[$i]); }
}
$_SESSION["count"]+=6;
}
function prev_img() {
$dir = "images/src/*.*";
$files = array();
$start = $_SESSION["count"]-1;
$end = $_SESSION["count"]-6;
$y=0;
foreach(glob($dir) as $file) {
$files[$y] = $file; //100 files
$y++;
}
for ($i = $start; $i > $end; $i--) {
if(pathinfo($files[$i], PATHINFO_EXTENSION)=="jpg") {
PostHTML(0,$files[$i]);
} else {
PostHTML(1,$files[$i]);
}
}
$_SESSION["count"]-=6;
}
?>
If i were to do something like this I might create a File Controller class to manage all the file operations i might need.
<?php
class FileController
{
private $_dir = "DIR";
private $files;
private $current;
function__construct()
{
$current = 0;
$files = scandir($_dir); // scans the dir to get a list of file names
if(!$files) echo "Failed To Load Files"; // failed to load files
else
{
$this->file = $files; // objects propery contains all file names in array;
}
}
function PostHTML($fileName) {
$src = $this->_dir + "/" + $fileName;
if(!$fileName)
{
return "<div class=\"item\"><div class=\"well\"><img
class=\"img-responsive\" src=\"$src\" alt=\"\"><p>blahblahblah xxxxxx</p></div></div>";
}
else
{
return "<div class=\"item\"><div class=\"well\"><video autoplay
loop class=\"img-responsive\"><source src=\"$src\" type=\"video/mp4\"></video></div></div>";
}
}
function nextImg()
{
$current = $this->getCurrent();
$end = $current + 6;
$this->setCurrent($end);
$files = $this->files;
$html = "";
for($i = $current; $i<= $end; $i++)
{
$html += postHtml($files[$i]);
}
echo $html;
}
function prevImg()
{
$current = $this->getCurrent();
$end = $current - 6;
$this->setCurrent($end);
$files = $this->files;
$html = "";
for($i = $current; $i>= $end; $i--)
{
$html += postHtml($files[$i]);
}
echo $html;
}
function getCurrent()
{
return $_SESSION['current'];
}
function setCurrent($current)
{
$_SESSION['current'] = $current;
}
}
?>

Corrupt .docx files when using a PHP IMAP class

I'm Using this PHP IMAP Class: http://code.google.com/p/php-imap/source/browse/trunk/ImapMailbox.php on a current project. After a few modifications the class is working. However whenever the class downloads .docx files they are always corrupt and have to be recovered by office.
protected function initMailPart(IncomingMail $mail, $partStruct, $partNum) {
$data = $partNum ? $this->imap_fetchbody($this->mbox, $mail->mId, $partNum, FT_UID) : $this->imap_body($this->mbox, $mail->mId, FT_UID);
if($partStruct->encoding == 1) {
$data = $this->imap_utf8($data);
}
elseif($partStruct->encoding == 2) {
$data = $this->imap_binary($data);
}
elseif($partStruct->encoding == 3) {
$data = $this->imap_base64($data);
}
elseif($partStruct->encoding == 4) {
$data = $this->imap_qprint($data);
}
$data = trim($data);
$params = array();
if(!empty($partStruct->parameters)) {
foreach($partStruct->parameters as $param) {
$params[strtolower($param->attribute)] = $param->value;
}
}
if(!empty($partStruct->dparameters)) {
foreach($partStruct->dparameters as $param) {
$params[strtolower($param->attribute)] = $param->value;
}
}
if(!empty($params['charset'])) {
$data = iconv($params['charset'], $this->serverEncoding, $data);
}
// attachments
if($this->attachmentsDir) {
$filename = false;
$attachmentId = $partStruct->ifid ? trim($partStruct->id, " <>") : null;
if(empty($params['filename']) && empty($params['name']) && $attachmentId) {
$filename = $attachmentId . '.' . strtolower($partStruct->subtype);
}
elseif(!empty($params['filename']) || !empty($params['name'])) {
$filename = !empty($params['filename']) ? $params['filename'] : $params['name'];
$filename = $this->decodeMimeStr($filename);
$filename = $this->quoteAttachmentFilename($filename);
}
if($filename) {
if($this->attachmentsDir) {
$filepath = rtrim($this->attachmentsDir, '/\\') . DIRECTORY_SEPARATOR . $filename;
file_put_contents($filepath, $data);
$mail->attachments[$filename] = $filepath;
}
else {
$mail->attachments[$filename] = $filename;
}
if($attachmentId) {
$mail->attachmentsIds[$filename] = $attachmentId;
}
}
}
if($partStruct->type == 0 && $data) {
if(strtolower($partStruct->subtype) == 'plain') {
$mail->textPlain .= $data;
}
else {
$mail->textHtml .= $data;
}
}
elseif($partStruct->type == 2 && $data) {
$mail->textPlain .= trim($data);
}
if(!empty($partStruct->parts)) {
foreach($partStruct->parts as $subpartNum => $subpartStruct) {
$this->initMailPart($mail, $subpartStruct, $partNum . '.' . ($subpartNum + 1));
}
}
}
protected function decodeMimeStr($string, $charset = 'UTF-8') {
$newString = '';
$elements = $this->imap_mime_header_decode($string);
for($i = 0; $i < count($elements); $i++) {
if($elements[$i]->charset == 'default') {
$elements[$i]->charset = 'iso-8859-1';
}
$newString .= iconv($elements[$i]->charset, $charset, $elements[$i]->text);
}
return $newString;
}
Try wading into the binary files with a good editor, both before and after the round-trip from IMAP, to see if there's something obvious. I've had similar problems where whitespace made its way into the PHP script (e.g. at the end of a file after the close ?> tag); most formats won't blink but .docx may get kicked into a recovery if there's whitespace left over at the end.

Select Random Distinct Images from a folder php

I want to select four random images from a folder which contains a number of images.
What i want to display is a table (2x2) where i can display 4 random distinct images.
Can someone tell me how can i select random distinct files from a folder so that i can store their path in a variable and then can use these variables to display images randomly in the table!
Is there any particular function which can select random files from a folder or something like that?
Use glob() to get all files from directory.
Use array_rand() to get random entries from an array.
<?php
function get_imagess($root) {
$r = array();
$r = array();
$k = glob("$root/*");
shuffle($k);
foreach($k as $n) {
if (is_dir($n)) {
break;
//$r = array_merge($r, get_imagesss($n));
print_r("xxx");
} else {
$r[] = $n;
//print_r($n + "yeah");
}
}
return $r;
}
function get_images($root) {
$r = array();
$k = glob("$root/*");
shuffle($k);
$n = $k[0];
// foreach($k as $n) {
if (is_dir($n)) {
$r = array_merge($r, get_imagess($n));
} else {
$r[] = $n;
}
// }
return $r;
}
$files = get_images('.');
//print_r($files);
//break;
shuffle($files);
$true=true;
$extList = array();
$extList['gif'] = 'image/gif';
$extList['jpg'] = 'image/jpeg';
$extList['jpeg'] = 'image/jpeg';
$extList['png'] = 'image/png';
$ass=0;
while($true)
{
$ass=$ass+1;
if($ass>100){$true=false;}
$imageInfo = pathinfo($files[0]);
if (isset( $extList[ strtolower( $imageInfo['extension'] ) ] )){
$temp = substr($files[0], -5);
$temp = strtolower($temp);
if( $temp == "p.jpg"){shuffle($files);} // checking if jpg has a preview file
else{ // no preview found
$true=false;
//print_r($temp);
}}
else
{
shuffle($files);
//print_r('bad' + $temp);
}
}
//print_r($files[0]);
echo '<HTML><meta HTTP-EQUIV="Refresh" CONTENT="5; URL=./ImageRotateMaybe.php"><img src="';
echo $files[0];
echo ' " width=100%>';
?>

Categories