PHP error handling- Undefined offset - php

I've checked my apache error logs, and I see multiple messages like this:
client 108.162.246.190] PHP Notice: Undefined offset: 4 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 5 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 5 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 6 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 6 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 7 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 7 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 8 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 8 in /var/www/html/search.php on line 143
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 9 in /var/www/html/search.php on line 142
[Tue Dec 31 00:17:48 2013] [error] [client 108.162.246.190] PHP Notice: Undefined offset: 9 in /var/www/html/search.php on line 143
And code around 142-143 lines looks like this (the actual 142-143 lines are implode ones):
if ($nuorodoshut != NULL)
{
foreach ($nuorodoshut as $key => $nuorodahut)
{
$keywords = explode(' ', $qsvarus);
$title[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $title[$key]);
$infoo[$key] = preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $infoo[$key]);
echo '<tr><td><h3>';
echo str_replace('<a href="/', '<a href="/host/', $nuorodahut->innertext) . '</h3>';
echo $aprasymashut[$key]->innertext . '<br>';
}
}
I just can't handle this error. Any help would be appreciated

The error messages are telling you that there's no $title[$key] and $infoo[$key] when $key is 5 - 9. If these two arrays should have all the same indexes as $nuorodoshut, then something is wrong in the creation of the arrays.
If it's OK that they shouldn't have these elements, you need to check this before you try using the values, e.g.
$title[$key] = isset($title[$key]) ? preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $title[$key]) : '';
$infoo[$key] = isset($infoo[$key]) ? preg_replace('/\b('.implode('|', $keywords).')\b(?![^<]*[>])/i', '<b>$0</b>', $infoo[$key]) : '';

Related

PHP Merging files not working

Merging files isn't working anymore and I'm not sure why. I'm not getting any on-screen errors so I checked my error log and this is what I see:
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP 1. {main}() /home/toolplas/public_html/main/phplib/edit_workorder.php:0, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Warning: Invalid argument supplied for foreach() in /home/toolplas/public_html/main/phplib/edit_workorder.php on line 50, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Stack trace:, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP 1. {main}() /home/toolplas/public_html/main/phplib/edit_workorder.php:0, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Notice: Undefined index: affect in /home/toolplas/public_html/main/phplib/edit_workorder.php on line 80, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Stack trace:, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP 1. {main}() /home/toolplas/public_html/main/phplib/edit_workorder.php:0, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Notice: Undefined index: file in /home/toolplas/public_html/main/phplib/edit_workorder.php on line 282, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Stack trace:, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP 1. {main}() /home/toolplas/public_html/main/phplib/edit_workorder.php:0, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Warning: Invalid argument supplied for foreach() in /home/toolplas/public_html/main/phplib/edit_workorder.php on line 282, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Stack trace:, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP 1. {main}() /home/toolplas/public_html/main/phplib/edit_workorder.php:0, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Warning: mysqli_fetch_assoc() expects parameter 1 to be mysqli_result, boolean given in /home/toolplas/public_html/main/edit_workorder.php on line 176, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP Stack trace:, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
[Tue Mar 18 09:06:08 2014] [error] [client 10.0.50.12] PHP 1. {main}() /home/toolplas/public_html/main/edit_workorder.php:0, referer: https://new.tps.com/edit_workorder.php?job_num=1000&id=4173
Its not specific to one Work Order as its happening to all of them and I'm not sure why its no longer working.
Here's the code:
if(isset($wo['files_to_merge'])){
//$prev_files = $_POST['file'];
//$files = $_FILES['file'];
$folder = "/home/toolplas/public_html/main/pdf/temp/WO$wo_id/";
if(!file_exists($folder)){
mkdir($folder);
}
$files = array();
if(isset($_POST['file'])){
foreach($_POST['file'] as $key => $item){
$files[] = array('order' => $key, 'filename' => $item);
}
}
/*foreach($_FILES['file']['name'] as $key => $item){
$name = date('U') . preg_replace('/[^\w\._]+/', '', basename($item));
if(move_uploaded_file($_FILES['file']['tmp_name'][$key], $folder . $name)){
$files[] = array('order' => $key, 'filename' => $name);
} else {
debug('File Not Uploaded ' . $name);
}
}*/
foreach($_FILES['file']['error'] as $key => $error){
$tmp_name= $_FILES['file']['tmp_name'][$key];
$name = date('U') . preg_replace('/[^\w\._]+/', '', basename($_FILES['file']['name'][$key]));
if($error == UPLOAD_ERR_OK){
move_uploaded_file($tmp_name, $folder . $name);
$files[] = array('order' => $key, 'filename' => $name);
} else {
debug("Cannot Move File: $tmp_name -> $name");
debug("File Error: $error");
}
}
$sql = "DELETE FROM workorder_files WHERE workorder_id = $wo_id;";
echo $sql;
mysqli_query($dbc3, $sql) or die("Query 11: " . mysqli_error($dbc3));
foreach($files as $file){
$sql = "INSERT INTO workorder_files(workorder_id, filename, `order`) VALUES($wo_id, '" . $file['filename'] . "', " . $file['order'] . ");";
mysqli_query($dbc3, $sql) or die("Query 12: " . mysqli_error($dbc3));
}
//print_r($_FILES);
}
Employees told me it was working fine Friday and over the weekend.
This is what the form looks like,
HTML:
<tr>
<th>Files To Merge?</th>
<td><input type="checkbox" class='files_to_merge' id="files_to_merge" name='workorder[files_to_merge]' {% if files is not empty %}checked='checked'{% endif %} />{{ functions.alert('More to the right') }}</td>
</tr>
<tr>
<th>Posted By</th>
<td>{{ user.name }}<input readonly="readonly" type="hidden" name="workorder[user]" value="{{ user.id }}" /></td>
</tr>

Unable to send email using PHP Pear

I tried to send email using pear. Here is my code
<?php
require_once "/usr/share/pear/Mail.php";
require_once "/usr/share/pear/Mail/mime.php";
$to = "Info <info#mydomain.com>";
$subject = "Contact Form - mydomain.com\r\n\r\n";
$host = "smtp.zoho.com";
$username = "noreply#mydomain.com";
$password = "abc#123";
$port = "465";
//Sender Details
$sender_name = $_POST['name'];
$from = $_POST['name']." <".$_POST['email'].">";
$sender_phone = $_POST['phone'];
//Create Message
$body = $_POST['message'];
//$body = wordwrap($body, 70, "\r\n");
$body = $body . "\r\n" . "Phone: " .$sender_phone;
if($sender_name != "" && $_POST['email'] != "" && $body != "" && $sender_phone != "")
{
$headers = array ('From' => $from,
'To' => $to,
'Subject' => $subject,
'Reply-To: ' => $from);
$smtp = Mail::factory('smtp',
array ('host' => $host,
'port' => $port,
'auth' => true,
'username' => $username,
'password' => $password));
$mail = $smtp->send($to, $headers, $body);
if (PEAR::isError($mail)) {
echo("<p>" . $mail->getMessage() . "</p>");
} else {
header("Location:contact-us.php?mcode=1");
}
}
else
{
header("Location:contact-us.php?mcode=2");
}
?>
Now this code gives me the following error in browser:
When I checked my php error_log i found this:
[Tue Dec 17 08:46:56 2013] [notice] child pid 7416 exit signal Segmentation fault (11)
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: name in /var/www/html/contact-us-process.php on line 14
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Stack trace:
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: name in /var/www/html/contact-us-process.php on line 15
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Stack trace:
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: email in /var/www/html/contact-us-process.php on line 15
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Stack trace:
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: phone in /var/www/html/contact-us-process.php on line 16
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Stack trace:
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Notice: Undefined index: message in /var/www/html/contact-us-process.php on line 19
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP Stack trace:
[Tue Dec 17 08:47:44 2013] [error] [client 117.198.137.57] PHP 1. {main}() /var/www/html/contact-us-process.php:0
[Tue Dec 17 08:47:45 2013] [notice] child pid 6887 exit signal Segmentation fault (11)
[Tue Dec 17 08:50:26 2013] [notice] child pid 7414 exit signal Segmentation fault (11)
[Tue Dec 17 09:35:48 2013] [error] [client 117.198.124.73] File does not exist: /var/www/html/favicon.ico
[Tue Dec 17 09:39:46 2013] [notice] child pid 8617 exit signal Segmentation fault (11)
I am using Rackspace cloud with Zoho email system.
When I did:
[root#mydomain /]# find -name Mail.php
./usr/share/pear/Mail.php
Thats why I directly included it in php file,
Also when I did,
[root#mydomain /]# find -name mime.php
./usr/share/pear/Mail/mime.php
So I used require_once "/usr/share/pear/Mail/mime.php", However this was there in an example, I am not so sure why I used this line.
What could be the possible reason for this and how i can solve it ?
$mail = new PHPMailer();
$mail->IsSMTP();
$mail->CharSet = 'UTF-8';
$mail->Host = "mail.example.com"; // SMTP server example
$mail->SMTPDebug = 0; // enables SMTP debug information (for testing)
$mail->SMTPAuth = true; // enable SMTP authentication
$mail->Port = 25; // set the SMTP port for the GMAIL server
$mail->Username = "username"; // SMTP account username example
$mail->Password = "password"; // SMTP account password example
You can find more about PHPMailer here: https://code.google.com/a/apache-extras.org/p/phpmailer/

php error_log weirdness

i have a global variable debug_smart404 in my smart404.php code:
$debug_smart404 = intval(get_option('debug_smart404' ));
error_log("smart404_redirect: debug_smart404 =" . ( $debug_smart404 ) );
which i use numerous places:
if ( $debug_smart404 > 4 ) # lotsa debug!
{
error_log("smart404_redirect: take_1st_match=" . ( $take_1st_match ? "yes" : "no" ) );
error_log("smart404_redirect: take_exact_match=" . ( $take_exact_match ? "yes" : "no" ) );
error_log("smart404_redirect: search_whole_uri=" . ( $search_whole_uri ? "yes" : "no" ) );
error_log("smart404_redirect: walk_uri=" . ( $walk_uri ? "yes" : "no" ) );
error_log("smart404_redirect: ignored_patterns_input=" . $patterns );
error_log("smart404_redirect: ignored_patterns_array=" . join($patterns_array,",") );
error_log("smart404_redirect: search_groups=" . join($search_groups,","));
}
while ( TRUE )
{
error_log("smart404_redirect:inside while: debug_smart404 =" . ( $debug_smart404 ) );
...
if ( $debug_smart404 > 3 ) # less debug
{
error_log("smart404_redirect: search_words=" . $search_words);
}
...
if ( $debug_smart404 > 0 ) {
error_log("smart404_redirect: uri=" . $uri . "= #matches=" . $mct);
}
...
} #end while...
so when i set debug_smart404 == 5, i get the expected output for the instances outside the while:
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: debug_smart404 =5
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: take_1st_match=yes
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: take_exact_match=yes
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: search_whole_uri=no
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: walk_uri=yes
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: ignored_patterns_input=wp-.*\r\n(ing|s|er|est)$\r\nprivate/.*
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: ignored_patterns_array=#wp-.*#i,#(ing|s|er|est)$#i,#private/.*#i,#/(trackback|feed|(comment-)?page-?[0-9]*)/?$#i,#\\.(html|php)$#i,#/?\\?.*#i
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect: search_groups=pages,posts,categories
[Thu Mar 14 08:32:42 2013] [error] [client xx.xx.xx.xx] smart404_redirect:inside while: debug_smart404 =5
but note that the error_log output inside the while loop's if statements is missing...but only on my production server (a netsol vps)
it works fine on my test server (lighttpd):
2013-03-14 08:26:40: (mod_fastcgi.c.2711) FastCGI-stderr: smart404_redirect: debug_smart404 =5
smart404_redirect: take_1st_match=no
smart404_redirect: take_exact_match=no
smart404_redirect: search_whole_uri=yes
smart404_redirect: walk_uri=yes
smart404_redirect: ignored_patterns_input=wp-.*
(ing|s|er|est)$
private/.*
smart404_redirect: ignored_patterns_array=#wp-.*#i,#(ing|s|er|est)$#i,#private/.*#i,#/(trackback|feed|(comment-)?page-?[0-9]*)/?$#i,#\.(html|php)$#i,#/?\?.*#i
smart404_redirect: search_groups=pages,posts,categories
smart404_redirect:inside while: debug_smart404 =5
smart404_redirect: search_words=cavitronix.com xxx zzzxxxx
smart404_redirect: uri=/cavitronix.com//xxx/zzzxxxx= #matches=0
wtf??? running php5 on both, and it used to work on vps (debug=1 output here)-:
[Thu Mar 14 06:32:55 2013] [error] [client xx.xx.xx.xx] smart404_redirect: uri=/how-it-works= #matches=1
[Thu Mar 14 06:41:32 2013] [error] [client xx.xx.xx.xx] smart404_redirect: uri=/videos/e2c-overview/private/awstats/current/new.html= #matches=10
[Thu Mar 14 06:53:38 2013] [error] [client xx.xx.xx.xx] smart404_redirect: uri=/current/previous.html= #matches=3
[Thu Mar 14 06:54:47 2013] [error] [client xx.xx.xx.xx] smart404_redirect: uri=/press-releases= #matches=3
or is this some feature of netsol's vps hackery?
The error log inside the while IS printed; your own paste of the error log from production server shows it "inside while: debug_smart404 =5"
You have missed a lot of code behind that '...'. Is it possible that your code gets inside the while loop, but then (on production) either quickly breaks out/or continues, BEFORE it reaches the other error log statements. Can you rule that out positively?

php move_uploaded_file forcely gives new name

I am trying to upload file using php, code snippt
$upload_path = "/var/upload/" . $_FILES['mfile']['name'];
if(move_uploaded_file($_FILES['mfile']['tmp_name'], $upload_path))
echo "file uploaded";
else
echo "upload failed";
Note: that /var/upload directory exists and has 777 permission.
The code prints "upload failed"
When I check /var/upload directory, there is file f_505bf77bd8a0f_mypdf.pdf. What went wrong?
Added log ( as per requested by loler):
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined variable: showdebug in /Volumes/data/htdocs/ebeu/upload.php on line 558, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined variable: showdebug in /Volumes/data/htdocs/ebeu/upload.php on line 559, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined index: accessories in /var/www/my/web/folder/myupload.php on line 21, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined index: pmaterial in /var/www/my/web/folder/myupload.php on line 27, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined index: occupation_bwc in /var/www/my/web/folder/myupload.php on line 33, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined index: occupation_bwc in /var/www/my/web/folder/myupload.php on line 75, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined index: occupation_spec in /var/www/my/web/folder/myupload.php on line 75, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined index: babycarrier in /var/www/my/web/folder/myupload.php on line 78, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined variable: accessories_all in /var/www/my/web/folder/myupload.php on line 79, referer: http://localhost/upload/test/upload.php
[Fri Sep 21 10:58:31 2012] [error] [client ::1] PHP Notice: Undefined variable: pmaterials_all in /var/www/my/web/folder/myupload.php on line 80, referer: http://localhost/upload/test/upload.php
Added var_dump($_FILES)
array(1) {
["mfile"]=>
array(5) {
["name"]=>
string(23) "mypdf.pdf"
["type"]=>
string(15) "application/pdf"
["tmp_name"]=>
string(26) "/var/tmp/phpUl6k50"
["error"]=>
int(0)
["size"]=>
int(478704)
}
}
Try this code for upload your files..
if (file_exists("/var/upload/" . $_FILES["file"]["name"]))
{
echo $_FILES["file"]["name"] . " already exists. ";
}
else
{
move_uploaded_file($_FILES["file"]["tmp_name"],
"/var/upload/" . $_FILES["file"]["name"]);
echo "Stored in: " . "/var/upload/" . $_FILES["file"]["name"];
}
In your var_dump() there $_FILES["file"]["name"] length is 23, but it should be 9. so try to trim this string. instead of
$upload_path = "/var/upload/" . $_FILES['mfile']['name'];
try to write
$upload_path = "/var/upload/" . trim($_FILES['mfile']['name']);

windows 7, php 5.3, when fetching binary data with sqlsrv from mssql the data is doubled

ok, so I have images on a MSSQL 2005 (express) server which I want to write to files.
with the same piece of code, on linux it works fine, on windows it writes the data twice in the file
file_put_contents($file, $val);
$val = basename($file);
I know it is not a file_put_contents() problem because I also tried with fwrite
the output file in windows has double the size it has in linux
-rw-rw-r-- 1 dimitris dimitris 891768 2011-11-22 16:13 eshop_products__2201.jpg
-rw-rw-r-- 1 dimitris dimitris 445884 2011-11-21 19:15 eshop_products__2201_linux.jpg
I am using the freetds driver for linux and php_pdo_sqlsrv_53_nts_vc9 in windows
any ideas on something I could do to get the correct data in windows? maybe some configuration I missed?
topmost bytes of each file:
windows file:
ASCII (php substr):
FFD8FFE000104A46494600010100000100010000FFDB0043000302020302020303030304030304050805050404050A070706080C0...etc...
hex:
00000000: 46 46 44 38 46 46 45 30 30 30 31 30 34 41 34 36 FFD8FFE000104A46
00000010: 34 39 34 36 30 30 30 31 30 31 30 30 30 30 30 31 4946000101000001
00000020: 30 30 30 31 30 30 30 30 46 46 44 42 30 30 34 33 00010000FFDB0043
00000030: 30 30 30 33 30 32 30 32 30 33 30 32 30 32 30 33 0003020203020203
00000040: 30 33 30 33 30 33 30 34 30 33 30 33 30 34 30 35 0303030403030405
00000050: 30 38 30 35 30 35 30 34 30 34 30 35 30 41 30 37 0805050404050A07
00000060: 30 37 30 36 30 38 30 43 30 41 30 43 30 43 30 42 0706080C0A0C0C0B
linux file:
ASCII (php substr):
����JFIF��C
hex:
00000000: ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 ......JFIF......
00000010: 00 01 00 00 ff db 00 43 00 03 02 02 03 02 02 03 .......C........
00000020: 03 03 03 04 03 03 04 05 08 05 05 04 04 05 0a 07 ................
00000030: 07 06 08 0c 0a 0c 0c 0b 0a 0b 0b 0d 0e 12 10 0d ................
00000040: 0e 11 0e 0b 0b 10 16 10 11 13 14 15 15 15 0c 0f ................
00000050: 17 18 16 14 18 12 14 15 14 ff db 00 43 01 03 04 ............C...
00000060: 04 05 04 05 09 05 05 09 14 0d 0b 0d 14 14 14 14 ................
00000070: 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 14 ................
The code extracting the file from the database is base on the yii framework, using yii's commands feature (php cli)
Connection strings:
linux
'connectionString' => 'dblib:host=192.168.56.101;port=1433;dbname=mssqldb',
windows
'connectionString' => 'sqlsrv:Server=192.168.56.101;Database=mssqldb',
Code:
$rows = $this->db->createCommand("SELECT * FROM $viewName WHERE 1=1")->queryAll();
/*
* Convert charset and extract photos from view
*/
foreach ($rows as $row) {
// convert charset / export photos
foreach ($row as $key => &$val) {
// charset conversion, on fields that do not contain photos
// $viewCfg is an array I maintain to know which field does what
if (empty($viewCfg['photos']) || !in_array($key, $viewCfg['photos'])) {
$val = #iconv("Windows-1253", "UTF-8", $val);
// grab image in file
} else {
$id = '';
foreach ($viewCfg['keys'] as $fieldName) {
$id .= '_' . $row[$fieldName];
}
$file = Yii::app()->params['pathPhotos'] . '/' . $viewName . '_' . $id . '.jpg';
if ($val) {
if (file_exists($file) && (file_get_contents($file)!=$val))
unlink($file);
file_put_contents($file, $val);
$val = basename($file);
}
}
}
// ... do the rest
Good to hear you found some solution, if not a nice one : /
I had that situation while testing Mssql on a PHP app but never got any further. Another hint you may check is the PHP doc on PDO Large Objects (like varbinary and image SQL Datatypes): http://de2.php.net/manual/en/pdo.lobs.php
Maybe the result is consistent when you read the image as a stream?

Categories