|
Hello experts. Is it possible to create file headers in PHP? In C, I could rely on size of data types to create file headers so that if I want a specific information from the file header, I had to seek to the appropriate byte location and read what I wanted.
I am seeing this to be difficult in PHP. I don't know how I can save, for instance, the length of a string in a file header and later retrieve it at a known byte location.
I started with something like the following:
class FileHeader
{
private $contentSize;
private $titleLength;
private $headerLength;
}
Because the variables can take different data types in PHP, I don't know how I can read the header information before dealing with the file. I can't tell the length that will take me to, for instance, $headerLength in the file so that I can read it since I wouldn't know the length of a particular data. It seems the number of bytes can vary for different headers of different files. Actually, I am a newbie in PHP and I don't know if there is any other way that will make such a task possible. Please help.
|
|
|
|
|
I am not sure you can read the header with any php based functions. But maybe you can use the system(), exec(), or passthru() to call a system resource to return what you are looking for.
Chris J
www.redash.org
|
|
|
|
|
If you really need to work with binary files in PHP you need to use the pack and unpack functions, along with the C like file functions (fopen, fseek, fread etc.), however if you need to work with binary files you probably really need to consider whether PHP is an appropriate language for your application.
Niall
|
|
|
|
|
I think I cannot use PHP for my task. I tried to write an integer value which could have occupied 4 bytes (or 2 bytes) of memory but the value was written as two sequence of characters. This means that I cannot rely on writing such values in PHP. 27 was saved as it is of two characters '2' and '7'. So 274 will also be saved as 3 characters where in C I would have assumed that to be of 4 sequence of bytes. I think PHP is unsuitable for such a task. Or maybe there are other ways around which I don't know and need to be taught.
modified on Monday, January 31, 2011 7:58 PM
|
|
|
|
|
This bit of code shows you how to write an integer to an ASCII text file, then (as a four byte long) to a binary file. The key thing is that pack is used to convert the integer into a character array equivalent to its binary representation before writing it. The default behavior of PHP when writing variables to a file is to convert them to strings.
<?php
$myinteger = 234;
$fp1 = fopen("ascii_text_file.txt", "w");
fwrite($fp1,$myinteger);
fclose($fp1);
$fp2 = fopen("binary_file.dat", "w");
$data = pack("l",$myinteger);
fwrite($fp2, $data);
fclose($fp2);
?>
|
|
|
|
|
Narr, it works. Now I believe reading the file requires
unpack()
function.
I tried to read about this function but I get confused with the format that is to be specified to it but I am trying to understand it. Thanks a lot.
|
|
|
|
|
Hi all. I am a beginner in php programming. Given a web page with links, I would like to know how I can send the selected link to a php script so that it will return the appropriate page requested. I don't if this can be done in an anchor ( <a> ) tag. Please help.
|
|
|
|
|
Ok I'm not quite sure what you mean. A link on a page already loads a file from the webserver (eg: <a href="http://www.somesite.com/mypage.php">MyLink</a> ). The link I gave in the sample would load mypage.php from the webserver.
If you mean you wish to use one single entry point (eg: index.php) for all page requests then you could do this using the querystring. Just set a parameter with the page you want to load and put the entire url in the A tag.
So in the second case you would end up with something like:
<a href="http://www.somesite.com/index.php?page=mypage">MyLink</a>
|
|
|
|
|
Ok been working on this for about 2 hours and I'm officially stuck.
What I'm trying to do is bild a calendar (got that part working how I want) where I can change the month.
I have the following code:
function startTable2($currentMonth,$currentYear) {
$month = date("F", mktime(0,0,0, date($currentMonth), date("t"), date($currentYear)));
echo "<table border=1 align=\"center\">";
echo "<form action=\"Calendar2.php\" method=\"post\">";
echo "<tr>";
echo "<td align=\"left\"><input type=\"submit\" name=\"previous\" id=\"previous\" value=\"Previous Month\" />";
if ($currentMonth == 1) {
$currentYear = $currentYear - 1;
$currentMonth = 12;
}
else
{
$currentMonth = date("m", mktime(0,0,0, date($currentMonth)-1, date("1"), date($currentYear)));
}
echo "<input type=\"hidden\" name=\"month\" id=\"month\" value=$currentMonth /></td>";
echo "<input type=\"hidden\" name=\"year\" id=\"year\" value=$currentYear /></td>";
echo "<input type=\"hidden\" name=\"day\" id=\"day\" value=\"01\" /></td>";
echo "</form>";
echo "<td colspan=5 align=center>$month $currentYear</td>";
echo "<form action=\"Calendar3.php\" method=\"post\">";
if ($currentMonth == 12) {
$currentYear = $currentYear + 1;
$currentMonth = 1;
}
else
{
$currentMonth = date("m", mktime(0,0,0, date($currentMonth)+1, date("1"), date($currentYear)));
}
echo "<td align=\"right\"><input type=\"submit\" name=\"next\" id=\"next\" value=\"Next Month\" />";
echo "<input type=\"hidden\" name=\"month1\" id=\"month1\" value=$currentMonth /></td>";
echo "<input type=\"hidden\" name=\"year1\" id=\"year1\" value=$currentYear /></td>";
echo "<input type=\"hidden\" name=\"day1\" id=\"day1\" value=\"01\" /></td>";
echo "</td></form></tr>";
echo "<tr><td align=center>Sunday</td><td align=center>Monday</td><td align=center>Tuesday</td><td align=center>Wednesday</td><td align=center>Thursday</td><td align=center>Friday</td><td align=center>Saturday</td></tr>";
}
As of now the "previous" works for every other month (i.e. December, October, August... etc) and the "next" is not working at all (blank screen). I've tried different methods for getting the new date and month but with no luck. Any help is greatly appreciated.
Thank You!
|
|
|
|
|
I've made some code changes and the next no longer comes up blank, it's not right but it's not blank... I also tried to make it more readable.
function startTable2($currentMonth,$currentYear) {
$month = date("F", mktime(0,0,0, date($currentMonth), date("t"), date($currentYear)));
$year = $currentYear;
echo "<table border=1 align=\"center\">";
echo "<tr>";
echo "<td align=\"left\">";
echo "<form action=\"Calendar2.php\" method=\"post\">";
echo "<input type=\"submit\" name=\"previous\" id=\"previous\" value=\"Previous Month\" />";
if ($currentMonth == 1) {
$currentYear = $currentYear - 1;
$currentMonth = 12;
}
else
{
$currentMonth = date("m", mktime(0,0,0, date($currentMonth)-1, date("1"), date($currentYear)));
}
echo "<input type=\"hidden\" name=\"month\" id=\"month\" value=$currentMonth />";
echo "<input type=\"hidden\" name=\"year\" id=\"year\" value=$currentYear />";
echo "<input type=\"hidden\" name=\"day\" id=\"day\" value=\"01\" />";
echo "</form>";
echo "</td>";
echo "<td colspan=5 align=center>$month $year";
echo "</td>";
echo "<td align=\"right\">";
echo "<form action=\"Calendar3.php\" method=\"post\">";
echo "<input type=\"submit\" name=\"next\" id=\"next\" value=\"Next Month\" />";
if ($currentMonth == 12) {
$currentYear = $currentYear + 1;
}
else
{
$currentMonth = $currentMonth + 1;
}
echo "<input type=\"hidden\" name=\"month1\" id=\"month1\" value=$currentMonth />";
echo "<input type=\"hidden\" name=\"year1\" id=\"year1\" value=$currentYear />";
echo "<input type=\"hidden\" name=\"day1\" id=\"day1\" value=\"01\" />";
echo "</form>";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td align=center>Sunday</td><td align=center>Monday</td><td align=center>Tuesday</td><td align=center>Wednesday</td><td align=center>Thursday</td><td align=center>Friday</td><td align=center>Saturday</td>";
echo "</tr>";
}
hope that helps a little.
|
|
|
|
|
|
For your information, I doubt anyone on this forum is gonna click a link for a file from a person they don't know.
Anyway, what is preventing you from opening the file? Is it encrypted?
Even Notepad* is able to open PHP files:
- Right-click on the file, point to "Open with..."
- Select "Notepad" from the list (if it is not visible, click "Choose standard application" and find Notepad in the list)
If that doesn't solve your issue, you're going to have to elaborate a bit.
*Instead of Notepad, I recommend Notepad++[^] which is a great text editor with line numbering, syntax coloring and a lot of other neat features.
|
|
|
|
|
<?php
$to = "someone@domain.com";
$subject = "Test";
$message = "Hello";
$from = "someonelse@domain.com";
$headers = "From: $from";
mail($to,$subject,$message,$headers);
echo "Mail Sent.";
?>
as far as i understand this is a valid mail function, but on the server it won't send anything to anyone, even my hotmail account .....What am i doing wrong ? this is all the code i have for this particular function so that i can test if it will work to submit feedback results to another mailbox....
(obviously the mail $to email is the one i am only using for this forum post )
i have asked the server support team and they say that this should work?
any ideas what else to try ...
|
|
|
|
|
I have a similar setup and I don't see anything wrong with what you have, so unless I'm blind it looks like it should indeed work. In fact what you have is almost identical to the php documentation.
Are you sure the server email system is working and has the correct settings in the php.ini file?
CQ de W5ALT
Walt Fair, Jr., P. E.
Comport Computing
Specializing in Technical Engineering Software
|
|
|
|
|
turns out the server people lied to me, thanks for your help anyway .... any idea what i could do to get an SMTP?
could i send the information to another website easily or ?
|
|
|
|
|
|
look for class.smtp.php from Chris Ryan.
that s good one for start to send mails using your smtp server.
|
|
|
|
|
class SafeEmail<br />
30 {<br />
31 function create($user, $host, $suffix)<br />
32 {<br />
33 $at = "@";<br />
34 $dot = ".";<br />
35 <br />
36 $start = $user . at . $host;<br />
37 $end = $dot . $suffix;<br />
38 $address = $start . end<br />
39 <br />
40 return $address;<br />
41 }<br />
42 <br />
43 $se = new SafeEmail();<br />
44 $se->create($user, $host, $suffix);<br />
45 }
More php email scripts listed on the page!
|
|
|
|
|
I have few urls like
http://www.mydomain.com/statecode1-folder1
http://www.mydomain.com/statecode2-folder1
I want to redirect it to http://www.mydomain.com/folder1/ page but I want to show on browser url the original urls likes
http://www.mydomain.com/statecode1-folder1
http://www.mydomain.com/statecode2-folder1
Please note that I do not have folders statecode1-folder1 and statecode2-folder1
Currently I use the below .htaccess which redirects but it changes the address bar
#Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/ statecode1-folder1 $
RewriteRule ^(.+) / folder1 [L]
RewriteCond %{REQUEST_URI} ^/ statecode2-folder1 $
RewriteRule ^(.+) / folder1 [L]
Thanks in Advance
Regards
Salman Ansari
|
|
|
|
|
Why the below code doesn't upload file to http://xyz.com/uvw/upload_data.php[^] automatically ?
I get error = uploaded file is either missing, empty or was not uploaded via the POST method
But am sure uploading file is not missing or empty as i have echo it and i think i am uploading via post method ?
function datapost($URLServer,$postdata)
{
$agent = "Mozilla/5.0";
$cURL_Session = curl_init();
curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);
curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);
curl_setopt($cURL_Session, CURLOPT_POST, 1);
curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($cURL_Session);
return $result;
}
$postdata = array();
$postdata['Id'] = '12340';
$postdata['profileName'] = 'tsunami';
$postdata['file'] = file_get_contents('http://www.abc.co.uk/def/ghi.csv');
$postdata['submit'] = urlencode('submit');
$source= datapost("http://xyz.com/uvw/upload_data.php",$postdata);
Any suggestion devs ??
|
|
|
|
|
Hi,
You need to set CURLOPT_HTTPGET to false explicitly - the PHP documentation doesn't make this clear.
curl_setopt($cURL_Session, CURLOPT_HTTPGET, FALSE);
Niall
|
|
|
|
|
Thanks,
I added the line you said but still getting the same error ..
Anything else i can add/edit to make it working ?
Cheers
|
|
|
|
|
No - that works on my system, as long as the CSV file isn't too big. (OK with 150k, not with 30M)
This is the version of your code I tried out.
<?php
function datapost($URLServer,$postdata)
{
$agent = "Mozilla/5.0";
$cURL_Session = curl_init();
curl_setopt($cURL_Session, CURLOPT_URL,$URLServer);
curl_setopt($cURL_Session, CURLOPT_USERAGENT, $agent);
curl_setopt($cURL_Session, CURLOPT_HTTPGET, FALSE);
curl_setopt($cURL_Session, CURLOPT_POSTFIELDS,$postdata);
curl_setopt($cURL_Session, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($cURL_Session, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($cURL_Session);
return $result;
}
$postdata = array();
$postdata['Id'] = '12340';
$postdata['profileName'] = 'tsunami';
$postdata['file'] = file_get_contents('http://localhost/tmp/test.csv');
$postdata['submit'] = urlencode('submit');
$source= datapost("http://localhost/tmp/posthere.php",$postdata);
echo $source;
?>
My test posthere.php is
<?php
foreach($_POST as $k=>$v)
{
echo "$k => ".strlen($v)."<br/>";
}
?>
|
|
|
|
|
Niall Barr wrote: (OK with 150k, not with 30M)
Initially i thought this could be the case as i was uploading around 12mb csv but then i tried with just 4kb but still getting the same error.
I don't really understand what could be the problem - the uploading works fine if i try using normal html form submit but as we don't want to manually browse the file - we decided to go this route and it's not working.. What a pain.
On the other hand why do you say it will not work for size in MB ? Is there any other auto upload way for such big files ?
|
|
|
|
|
I think the only issue was the amount of memory available to PHP on my dev box. I tried with a file that was too big for it.
Are you able to get the other post data transferred if you comment out the file_get_contents line?
Niall
|
|
|
|
|