ASP to PHP conversion, what did I do wrong? - php

I'm pretty new to PHP and trying to convert an asp file handling "function" to php, but for some reason the page just turns up blank when I try to run it with the PHP part (if I remove it the site fires up perfectly, without the functionallity ofcourse.
So my question is: What did I do wrong here?
<?php
if (!$_GET['page'] == "")
if (!$_GET['page'] == "gaestebog")
If (file_exists($_GET['page'] && ".html"))
$f=$file=fopen($_GET['page'] && ".html", r);
print $f;
fclose($file);
Else
print("Siden kunne ikke findes");
End If
elseif ($_GET['page'] == "gaestebog")
print "<a href='default.php?page=gaestebog'>Der kan i øjeblikket ikke oprettes nye indlæg</a><br /><br />"
end if
elseif ($_GET['page'] == "")
If (file_exists("forside.html"))
$f=$file=fopen("forside.html", r);
print $f;
fclose($file);
Else
print("Siden kunne ikke findes");
End If
end if
?>

if ( isset($_GET['page']) )
{
$page = $_GET['page'];
if ( $page != "gaestebog" )
{
$filename = sprintf('%s.html', $page);
if ( file_exists($filename) )
{
if( $handle = fopen($filename, 'r') )
{
echo fread($handle, filesize($filename));
fclose($handle);
}
}
else
{
echo "Siden kunne ikke findes";
}
}
else if ( empty($page) )
{
if ( file_exists("forside.html") )
{
if( $handle = fopen("forside.html", 'r') )
{
echo fread($handle, filesize("forside.html"));
fclose($handle);
}
}
else
{
echo "Siden kunne ikke findes";
}
}
}

Try this:
<?php
if (isset($_GET['page']) && !empty($_GET['page'])) {
if ($_GET['page'] !== "gaestebog") {
if (file_exists($_GET['page'] . ".html")) {
$file = fopen($_GET['page'] . ".html", "r");
$contents = fread($file, filesize($_GET['page'] . ".html"));
echo $contents;
fclose($file);
} else {
echo "Siden kunne ikke findes";
}
} else if ($_GET['page'] == "gaestebog") {
echo "<a href='default.php?page=gaestebog'>Der kan i øjeblikket ikke oprettes nye indlæg</a><br /><br />";
} else if (empty($_GET['page'])) {
if (file_exists("forside.html")) {
$file = fopen("forside.html", "r");
$contents = fread($file, filesize("forside.html"));
echo $contents;
fclose($file);
}
}
echo "Siden kunne ikke findes";
}
?>

Related

How to download attachment on email using php

I have tried to save attachment but i couldn't download attachment with this code .it create a folder for downloading file. any one? How to save a attachment on email in php?
thanks in advance
$struct = imap_fetchstructure($mbox,$msgno);
$contentParts = count($struct->parts);
if ($contentParts >= 2)
{
for ($i=2;$i<=$contentParts;$i++)
{
$att[$i-2] = imap_bodystruct($mbox,$msgno,$i);
}
for ($k=0;$k<count($att);$k++)
{
if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value == "US-ASCII")
{
if ($att[$k]->parameters[1]->value != "")
{
$selectBoxDisplay[$k] = $att[$k]->parameters[1]->value;
}
}
elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1")
{
$selectBoxDisplay[$k] = $att[$k]->parameters[0]->value;
}
}
}
//print_r($att);
if (count($selectBoxDisplay) > 0)
{
for ($j=0;$j<sizeof($selectBoxDisplay);$j++)
{
$strFileName = $att[$j]->parameters[0]->value;
$strFileType = strrev(substr(strrev($strFileName),0,4));
$fileContent = imap_fetchbody($mbox,$msgno,$j+2);
$ret = downloadFile($strFileType,$strFileName,$fileContent);
$selectBoxDisplay[$j] = str_replace($search,'',$selectBoxDisplay[$j]);
$filenametmp = $selectBoxDisplay[$j];
$handle = fopen($filenametmp, "w+");
fwrite($handle, $ret);
fclose($handle);
//chmod($filenametmp, 777);
}
}

Only first elseif in if/elseif/else statement is working

Only the elseif (isset($foo['read'])) on line 37 is working, the if and first elseif work, when I try to call the second elseif, it returns the first elseif statements output. I have even swapped the two elseif statements and it still only returned the first elseif statement output when calling the second elseif statement.
<?php
$nh = "true";
$foo = file_get_contents("php://input");
//$stuff = json_decode($foo, true);
function savetxt($sttext)
{
$filename = 'test.txt';
$somecontent = $sttext;
if (is_writable($filename)) {
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
}
else {
echo "The file $filename is not writable";
}
}
if (strpos($foo, 'write:') !== false) {
echo 'Attempting to write!';
$stext = substr($foo, 6);
savetxt($stext);
}
elseif ($foo == "read"){
echo file_get_contents( "test.txt" );
}
elseif ($foo == "test){
echo 'Working!';
}
else {
echo 'Didn't get test.';
}
?>
try this(you must replace else if with elseif)
else if ($foo && $foo=='read')
and
else if ($foo && $foo=='test')
and at end
else if (!$foo || $foo=='')

Limiting the checking condition while uploading SWF files

I am creating an application of uploading swf files in a folder using PHP.
My script is all working except for the first if condition where I'm checking whether the extension is swf or not, but I seems to have some error.
I'm not sure whether video/swf is a valid checking parameter for SWF files or not. My full script is below. I'm checking the size of the SWF using getimagesize(). Some people may wonder that getimagesize works for image, but I saw some examples where getimagesize() has been used for getting size of SWF files.
It's giving me the message "invalid swf file", that means its not satisfying the first checking condition at all.
<?php
foreach($_FILES['item_swf']['tmp_name'] as $key=>$val)
{
list($width, $height) = getimagesize($_FILES['item_swf']['tmp_name'][$key]);
if (( ($_FILES["item_swf"]["type"][$key] == "video/swf") || ($_FILES["item_swf"]["type"][$key] == "video/SWF") )
&& ($_FILES["item_swf"]["size"][$key] < 800000))
{
if ($_FILES["item_swf"]["error"][$key] > 0)
{
echo "Error: " . $_FILES["item_swf"]["error"][$key] . "<br />";
}
else if($width==1000 && $height==328)
{
if (file_exists('../../swf_folder/header_swf/' . $_FILES["item_swf"]["name"]))
{
echo $_FILES["item_swf"]["name"][$key] . " already exists. ";
}
else
{
move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);
echo "done";
}
}
else
{
echo "size doest permit";
}
}
else
{
echo "Not a valid swf file::";
}
}
?>
The line given below
move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);
is working perfectly as it is uploading files to the dedicated folder, it somehow seems that the checking parameters for SWF only files are not set properly.
Edit
I got my answer. Instead of using video/swf I need to use application/x-shockwave-flash.
So the ultimate code will be:
<?php
foreach($_FILES['item_swf']['tmp_name'] as $key=>$val)
{
list($width, $height) = getimagesize($_FILES['item_swf']['tmp_name'][$key]);
if (($_FILES["item_swf"]["type"][$key] == "application/x-shockwave-flash")
&& ($_FILES["item_swf"]["size"][$key] < 800000))
{
if ($_FILES["item_swf"]["error"][$key] > 0)
{
echo "Error: " . $_FILES["item_swf"]["error"][$key] . "<br />";
}
else if($width==1000 && $height==328)
{
if (file_exists('../../swf_folder/header_swf/' . $_FILES["item_swf"]["name"]))
{
echo $_FILES["item_swf"]["name"][$key] . " already exists. ";
}
else
{
move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);
echo "done";
}
}
else
{
echo "size doest permit";
}
}
else
{
echo "Not a valid swf file::";
}
}
?>
you can try
$savePath = "PATH_TO_SAVE";
$errors = array ();
$output = array ();
//
if (isset ( $_FILES ['item_swf'])) {
foreach ( $_FILES ['item_swf'] ['tmp_name'] as $key => $val ) {
$fileName = $_FILES ['item_swf'] ['name'] [$key];
$fileSize = $_FILES ['item_swf'] ['size'] [$key];
$fileTemp = $_FILES ['item_swf'] ['tmp_name'] [$key];
$fileExtention = pathinfo ( $fileName, PATHINFO_EXTENSION );
$fileExtention = strtolower ( $fileExtention );
if ($fileExtention != ".swf") {
$errors [$fileName] [] = "Invalid File Extention";
continue;
}
if ($fileSize > 800000) {
$errors [$fileName] [] = "File Too large";
continue;
}
list ( $width, $height ) = getimagesize ( $fileTemp );
if ($width != 1000 && $height != 328) {
$errors [$fileName] [] = "Wrong File dimention ";
continue;
}
if (file_exists ( $savePath . DIRECTORY_SEPARATOR . $fileName )) {
$errors [$fileName] [] = "File Exist";
continue;
}
if(!is_writable($savePath ))
{
$errors [$fileName] [] = "File Destination not writeable";
}
if(count($errors [$fileName]) == 0)
{
if(#move_uploaded_file ( $fileTemp, $savePath . DIRECTORY_SEPARATOR . $fileName))
{
$output[$fileName] == "OK" ;
}
else
{
$errors [$fileName] [] = "Error Saving File";
}
}
}
var_dump($errors, $output);
}
Let me know if you have any more challenge
ok, i got my answer....
instead of using video/swf i need to use application/x-shockwave-flash
so the ultimate code will be
<?php
foreach($_FILES['item_swf']['tmp_name'] as $key=>$val)
{
list($width, $height) = getimagesize($_FILES['item_swf']['tmp_name'][$key]);
if (( ($_FILES["item_swf"]["type"][$key] == "application/x-shockwave-flash") || ($_FILES["item_swf"]["type"][$key] == "video/SWF") )
&& ($_FILES["item_swf"]["size"][$key] < 800000))
{
if ($_FILES["item_swf"]["error"][$key] > 0)
{
echo "Error: " . $_FILES["item_swf"]["error"][$key] . "<br />";
}
else if($width==1000 && $height==328)
{
if (file_exists('../../swf_folder/header_swf/' . $_FILES["item_swf"]["name"]))
{
echo $_FILES["item_swf"]["name"][$key] . " already exists. ";
}
else
{
move_uploaded_file($val, '../../swf_folder/header_swf/'.$_FILES['item_swf']['name'][$key]);
echo "done";
}
}
else
{
echo "size doest permit";
}
}
else
{
echo "Not a valid swf file::";
}
}
?>

FTP_DELETE not working?

Hey guys I have my script here that is supposed to do some stuff then delete a file, unfortunetly my files never unlink. I"m wondering what the reason for this might be? Permissions was the only thing I could think of, or maybe the output buffer is messing up? I really don't know, but would appreciate some advice on how to handle it. Issue in question is that last IF() block.
public function remoteFtp() {
$enabled = Mage::getStoreConfig('cataloginventory/settings/use_ftp');
$remove = Mage::getStoreConfig('cataloginventory/settings/ftp_remove_file');
if ($enabled == 0) {
return true;
}
$base_path = Mage::getBaseDir('base');
$ftp_url = Mage::getStoreConfig('cataloginventory/settings/ftp_url');
$ftp_user = Mage::getStoreConfig('cataloginventory/settings/ftp_user');
$ftp_pass = Mage::getStoreConfig('cataloginventory/settings/ftp_password');
$ftp_remote_dir = Mage::getStoreConfig('cataloginventory/settings/ftp_remote_dir');
$ftp_filename_filter = Mage::getStoreConfig('cataloginventory/settings/ftp_remote_filename');
$ftp_file = $base_path . '/edi/working/working.edi';
$handle = fopen($ftp_file, 'w');
$conn_id = ftp_connect($ftp_url);
ftp_login($conn_id, $ftp_user, $ftp_pass) or die("unable to login");
if ($ftp_remote_dir) {
ftp_chdir($conn_id, $ftp_remote_dir);
}
//is there a file
$remote_list = ftp_nlist($conn_id, ".");
$exists = count($remote_list);
if ($exists > 0) {
$len = strlen($ftp_filename_filter) - 1;
foreach ($remote_list as $name) {
if (substr($ftp_filename_filter, 0, 1) == "*") {
if (substr($name, '-' . $len) == substr($ftp_filename_filter, '-' . $len)) {
$ftp_remote_name = $name;
}
}
if (substr($ftp_filename_filter, strlen($name) - 1) == "*") {
if (substr($ftp_filename_filter, 0, $len) == substr($name, 0, $len)) {
$ftp_remote_name = $name;
}
}
if ($ftp_filename_filter == $name) {
$ftp_remote_name = $name;
}
}
}
if (ftp_fget($conn_id, $handle, $ftp_remote_name, FTP_ASCII, 0)) {
echo "successfully written to $ftp_file <br />";
if ($remove == 1) {
ftp_delete($conn_id, $ftp_remote_name);
}
} else {
echo "There was a problem while downloading $ftp_remote_name to $ftp_file <br />";
}
ftp_close($conn_id);
}
The answer was that the system variable $remove = Mage::getStoreConfig('cataloginventory/settings/ftp_remove_file'); was set to BOOL(false)

Delete and update file if 1 day old

I'm writing a code in PHP which deletes a file if it's more than a day old.
but i t does not do so, and append line after it :(
$fileName = 'news/'.$_COOKIE['sign'];
if (isset($_COOKIE['sign']))
{
if ((file_exists($fileName)) && (date("d",filemtime($fileName))==date("d")))
{
$data = file_get_contents($fileName);
if ($data == '')
{
$data = 'Temporary network problem !';
unlink($fileName);
}
echo $data;
}
else
echo 'Fetch and put new news';
$fileName = 'news/'.$_COOKIE['sign'];
if (isset($_COOKIE['sign'])) {
if ((file_exists($fileName)) && (date("d", filemtime($fileName)) == date("d"))) {
$data = file_get_contents($fileName);
if ($data == '') {
$data = 'Temporary network problem !';
unlink($fileName);
}
echo $data;
}
elseif(file_exists($fileName)) { //if not above, then delete it!
unlink($fileName);
}
else {
echo 'Fetch and put new news';
}
}

Categories